# Operators In Python

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

These will probably me the most familiar with us, since they come from basic mathematics.

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 have some programming experience, we will certainly have used assignment operators before. They assign a certain value to a variable. The most used one is `=`

. To use it, simply type the name of the variable we want to assign a new value to, the `=`

, & the value we want to be assigned. For example, if we want to assign the value 100 to the variable `balance`

, we can do the following:

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

We can use comparison operators to compare the values of variables. They will always return a boolean value, which is either `True`

or `False`

. There are six of them:

- 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

We might already be familiar with these! In logic, they are called logical connectives. These are also widely used in human language. They are: `and`

, `or`

& `not`

.

`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

Remember that earlier, we saw how `+`

can be used to add up numbers, but that it can also be used to concatenate, or combine strings. So `+`

works differently, depending on what type of object we use it on. But this will not always be the case. Sometimes, the operator we’re working with just doesn't support the type of object we want to use it on.

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

When we’re combining lots of operators, things can get confusing. Take a look at the example below:

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

We have seen that precedence matters! But what about the ones that are on the same precedence level, like multiplication & division? Are they executed at the same time? No. `*`

, `\`

, `%`

& `\\`

are executed from left to right, while `**`

is executed from right to left. This is called 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

We now know what operators are, how we should use them & how they work. We also learned about overloading, precedence & associativity.

**RELATED LINKS:**