26 julio, 2024

6 magical comparison methods in Python

In the classes of a program in Python code, They can perform various operations to play with the code and the object being created. Usually, programmers decide to compare the objects they have created with their code project. And for this, as in most of the Python programming language, There are tools for comparisons.

These are magical comparison methods in Python, whose functionality is to compare two different values ​​of the same class. In this article, therefore, we present 6 magical comparison methods in Python to compare two numbers in Python.

Magic comparison methods in Python

Next, we present to you 6 magical comparison methods in Python so that you can compare values ​​within the class objects of your code programs.

Method _ _lt_ _( )

This comparison method is used to compare two numbers in Python and confirm if the first one is less than the second value entered into the method. For your invocationfirst define the method _ _lt_ _ and, as parameters, we will find the self. In this rule for creating classes, self should be the first parameter, and the other value the second parameter. In this case, it returns the comparison operation with the sign less than: <.

def _ _ lt_ _ (self, other):

return self.value < other.value

Method _ _gt_ _( )

This comparisons method in Python is used to compare two values ​​and confirm if the first is greater than the second value entered into the method. For your invocationfirst define the method _ _gt_ _. As parameters we will find the selfbeing a rule for creating classes. self must be the first parameter and the other value, the second. In this case, it returns the comparison operation with the greater than sign: >.

def _ _ gt_ _ (self, other):

return self.value > other.value

Method _ _le_ _( )

This comparison method is used to compare two values ​​and confirm whether the first value is less than or equal to the second value entered into the method. For your invocationfirst define the method _ _le_ _ and, as in the previous cases, we will find the self as parameters. Being a rule for class creation, self should be the first parameter and the other value the second parameter. In this case, it returns the operation of comparing values ​​in Python with the sign less than or equal to: <=.

def _ _ le_ _ (self, other):

return self.value <= other.value

Method _ _ge_ _( )

This comparison method is used to compare values ​​in Python and confirm if the first value is greater than or equal to the second value entered into the method. For your invocationit defines the _ _ge_ _ method and, as parameters, we find ourselves again at self (it’s a rule for creating classes, where self must be the first parameter), and the other value, which will be the second parameter. In this case it returns the comparison operation with the sign greater than or equal to: >=.

def _ _ ge_ _ (self, other):

return self.value >= other.value

Method _ _ne_ _( )

This comparison method is used to compare two values ​​and confirm whether the first value is different from the second value entered into the method. For your invocationfirst define the method _ _ne_ _ and, as in the situations explained previously, we find the parameter selfbeing a rule for class creation, where self should be the first parameter and the other value the second parameter. In this case it returns the comparison operation with the different sign in Python, that is: !=.

def _ _ne_ _ (self, other):

return self.value != other.value

Method _ _eq_ _( )

This comparison method is used to compare two values ​​and confirm whether the first value is the same as the second value that is entered into the method. For your invocationit defines the _ _eq_ _ method. As parameters we will find the selfbeing a rule for class creation, where self should be the first parameter and the other value will be the second parameter. In this case, it returns the comparison operation with the equal sign in Python: ==.

def _ _ eq _ _ (self, other):

return self.value == other.value

In this way, you will have known some of the comparisons in Python.

What is the next step?

Although you already know 6 magical comparison methods in Python, in this programming language there are other methods to learn, such as arithmetic operations methods. All of these allow you to give other functionalities to your program and add a layer of versatility to what you are building from computational thinking.

If you want to continue learning more magic methods or other tools to perfect your code programs, we recommend taking a look at our Bootcamp Learn to Program from Scratch. Here you will start on your path in programming in the most in-depth and accessible way possible, learning about concepts such as comparisons in Python. Sign up and start your path as a programmer!

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *