Just like in mathematics, programming languages like Python have operators. We can think of them as extremely simple functions that lie at the basis of computer science. These are the simplest operations to which a computer program can be reduced. They are essential knowledge for any aspiring Data Scientist or Software Engineer.

In this tutorial, we’ll learn about:

  • The different types of operators: Arithmetic, Assignment, Comparison & Logical
  • Operator Overloading
  • Precedence
  • Associativity

Arithmetic Operators

The arithmetic operators are addition (+), subtraction (-), multiplication (*), division (/), exponent (**), floor division (//) & modulus (%). The first four are fairly simple; they are addition, subtraction, multiplication & division.

The other three are a bit more complicated. The exponent operator means “raised to the power of”. For example, 2 ** 3 means that 2 is multiplied with itself 3 times, so 2 ** 3 is the same as 2 * 2 * 2, which is equal to 8.

Floor division is the same as normal division, except that the result is always rounded off to the bottom. In the example below, 5 is divided by 2, resulting in 2.5. But since it’s the floor division, 2.5 is rounded off to 2.

Finally, the modulus is the remainder after division. In the case of 5%2, the number 2 only fits twice in 5, which means that the remained is 1.

In Python, the addition operator can also be used to concatenate strings. This means that they will be assembled into a single string, for example:

Assignment Operators

If we want to increment the value of balance with 10, we can use balance += 10. This is the equivalent of balance = balance + 10, but it's much shorter.

The same holds for -=, balance -= 10 is the same as balance = balance - 10:

The other assignment operators work in exactly the same way. These are:

  • *=
  • /=
  • **=
  • //=
  • %=

Comparison Operators

  • Equal: ==
  • Not equal: !=
  • Greater than: >
  • Less than: <
  • Greater than or equal: >=
  • Less than or equal: <=

Here are some example comparisons:

== & != can also be used to compare strings instead of numbers. The other operators are not compatible with strings, since a string can't really be greater than another string.

Logical Operators

and will only return True if both operands (the two boolean objects you are comparing) are True. For example:

To understand the behavior of this logical operator, we can make use of a truth table:

or is True, whenever any (one or more) operand is True.

Notice that these also work with more than two operands, for example:

not will return the opposite of its operand, so True if False is given & vice-versa.

Operator Overloading

Here’s an example. Say we have two Dog objects, ozzy & filou. These belong to the Dog class below. We can think of the class as a blueprint for what the objects must look like & how they should behave. In this case, the Dog objects have a name, age & height. After the class is defined, we can instantiate objects. One of them is Ozzy, with an age of 3 & height of 30 centimeters. The other one is Filou, he is 5 years old & 65 centimeters tall.

Say we wanted to compare both dogs with the greater than: >. This will not work. We will get an error saying '>' not supported between instances of 'Dog' and 'Dog'. This means that > does not know how to compare objects of the Dog class.

Luckily, with operator overloading, we can define the operator’s behavior for when it is called on Dog objects. In this case, a method called __gt__, meaning 'greater than', has to be defined, taking in two arguments. Since we want to compare our dogs on their height, the method returns self.height > other.height.

Precedence

How do we know which one will be executed first? How should we read a line of code a lot of these symbols? Is it executed from left to right or is there some specific order?

This is where precedence comes in. Python operators are executed in a very specific order, & some have priority over others.

In the following table, we can see the precedence for operators, in order from top to bottom. These rules are the same in mathematics, where certain calculations in a formula have to be performed before others.

Associativity

Take a look at the following example. In this case, the operators will be executed from left to right, & the output is 3.

By adding parentheses, however, we can change the order in which the operators are executed. That is associativity, which works the same way as associativity in maths. Whatever is between brackets will be calculated first. In this case, the floor division will be performed first, followed by the multiplication.

Conclusion

RELATED LINKS:

Data Scientist & Machine Learning Engineer