Operators in Python
It’s been long time ago I published the last article. I had exams at school and worked hard to pass and have a good grades. Now I have some days off and decided to continue the coding adventures with Python.
Sometimes you need to perform some kind of operations in any programming languages. Python is not different. What is an operation? The operation can be multiplication, division or any other arithmetical operation. This is not all you can do. Let’s start from assignment operation.
1. Assignment operator
Assignment operator (“=”) is when you assign a value from the right side of this operator to the variable on the left side of the operator. Looks complicated? No! Take a look at the code below.
It is no much to say more than that. Let’s move to another category of operators.
2. Arithmetic operators
I bet you know that you have learnt the arithmetic at school. Same like me. If you were good at math, Python would not surprise you at all, but if you were not feeling comfortable with math, then Python would support you here!
Python has a five basics operators: addition (‘+’), subtraction (‘-‘), multiplication (‘*’), division (‘/’), modulo (‘%’). Here you have examples:
A few words about the modulo operator. It gives you the reminder from the division operation. For example, if you divide 9 by 3 than the modulo returns 0, because there is no reminder part in this operation.
However, if you divide 10 by 3 than the reminder is 1.
3. Logical operators
There are three Logical Operators in Python. I will explain all of them in a very simple way. Imagine that a = 10>3. We are not interested in a numerical result of the operation. Rather, we are interested in logical result. In the example above, ten is greater than three so the result is true. True is often represented as 1 (in a logical way). In a contrast, if we type b = 10<3, the result is false. False is represented by 0 (in a logical way).
Now, you are ready to analyze the information that I have included in the table below. a and b are the results of the operations I have already described. Now, we can combine them, for example, a and b, a or b, not a.
The and operator works that way that if on both sides it has true values than it also produces true. In any other combination the result will be false.
The or operator works that way that if at least one value on its left or right isde is true values than it also produces true. For both false values it produces false.
The not operator just negates the value so it turns true to false and false to true.
There are more logical operations you can do like xor or nor but my dad says we should keep it simple. And of course you can introduce NULL or unknown values which definitelly is not simple in that case.
4. Comparison operators
The last type of operators I am going to cover are the comaparison operators. You have already seen them in the previous example when I compared if 10 was greater than 3. A comparison operator returns true or false depending of left or right side of the operator and the type of the operation
There are six comparison operators in Python:
- greater than
- less than
- greater or equal than
- less or equal than
- not equal to
The for the equal operator you need to use == like on the code below
The other operators (apart from the last one) use >=, <=, > and < signs.
The not equal operator is a combination of ! and =.
This is it! That is all about the operators in Python!