We already know that operators are used to perform some operations on variables and values and those variable or values are called operands. In this article we will look at different operators available in Python and their examples.
There are different types or groups of operators in Python and those are:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Identity operators
- Membership operators
- Bitwise operators
So, let’s discuss each of these one by one.
Arithmetic operators are used to perform various arithmetic operations like addition, multiplication, division, etc. Here is the list of arithmetic operators:
|+||Addition||Performs addition on two operands|
|-||Subtraction||Performs subtraction on two operands|
|*||Multiplication||Performs multiplication on two operands|
|/||Division||Performs division on two operands|
|//||Floor Division||Gives floor of the result of division of two operands|
|%||Modulus||Gives remainder after division|
|**||Exponentiation||a**b calculate ab|
Let’s look at an example to see working of each of these.
print (5+2) print (5-2) print (5*2) print (5/2.0) print (5//2) print (5**2) print (5%2)
Assignment operators are used to assign values to variables. For example,
= is an assignment operator which assigns the value at the right side of it to the variable at the left side of it i.e.,
x = y will make the value of x equal to the value of y and not vice versa.
+= is an another assignment operator which adds the value at the right side to the left side and assigns to the variable at the left side i.e.,
x += y is same as
x = x+y.
In the expression
x = x+y, we have two operators,
+. The + operator has higher precedence than = operator (precedence chart is given at the last of this post), so
x+y will be calculated first and then
= will make the value of x equal to that summation. For example,
x = 5
y = 2
x = x+y
x+y is calculated first (5+2 = 7) and then
= assigned this value to x i.e.,
x = 7. So,
x += y will also make the value of x equal to 7 because it is same as
x = x+y.
Take a note that this is little different from regular mathematics.
Similarly, we have
x -= y is same as
x = x-y),
x*=y is same as
x = x*y), etc. operators. Some of the important assignment operators are:
|+=||x+=y is same as x = x+y|
|-=||x-=y is same as x = x-y|
|*=||x*=y is same as x = x*y|
|/=||x/=y is same as x = x/y|
|//=||x//=y is same as x = x//y|
|**=||x**=y is same as x = x**y|
|%=||x%=y is same as x = x%y|
x = 5 y = 2 x += y print(x) x -= y print(x) x *= y print(x) x /= y print(x) x //= y print(x) x **= y print(x) x %= y print(x)
Comparison operators are used to compare the values of the operands. Let’s have a look at them.
|==||Equal||a==b is true is a and b have same value|
|>||Greater than||a>b is true is a if greater than b|
|<||Less than||a<b is true if a is less than b|
|>=||Greater than or equal to||a>=b is true if a is either greater than or equal to b|
|<=||Less than or equal to||a<= is true if a is either smaller than or equal to b|
|!=||Not equal||a!=b is true if a and b have different values|
print(5==2) print(5>2) print(5<2) print(5>=2) print(5<=2) print(5!=2)
not operators discussed in the Boolean chapter are logical operators.
|and||a and b is True if both a and b are True|
|or||a or b is True if either of a and b is True|
|not||not(a) reverses the value of a i.e., True becomes False and vice versa|
print(True and False) print(True or False) print(not(True))
We know that each variable is assigned a memory in our computer. Identity operators check if the memory location of two objects is same or not i.e., they are same or not (not just values).
|is||True if both the operands are same|
|is not||True if both the operands are different|
x = [5, 2] y = [5, 2] z = y print(z is y) print(x is z) print(x == y) print(x is not y)
x and y are lists with same value but different memory locations. z and y are the same i.e., with the same value and same memory location also. So,
z is y is true.
If you are reading this post after the boolean chapter, you will read about lists in later chapters.
Membership operators are used to check if a value is present in a sequence like tuple, list, etc. or not.
|in||True if the value is present in the sequence|
|not in||True if the value is not present in the sequence|
x = (5, 2) print(5 in x) print(10 not in x)
We have only discussed some important operators in this course, you can check https://python-reference.readthedocs.io/en/latest/docs/operators/ for more.
In the case of an expression with multiple operations like,
x = 5+3/2-1, the order of execution of operators is decided by precedence order given below (highest priority at top and least at bottom).
|*, /, //, %|
|<=, <, >, >=|
|=, %=, /=, //=, -=, +=, *=, **=|
|is, is not|
|in, not in|
|and, or, and|
In the case of have same precedence, the expression is evaluated from left to right.
For example, in the expression,
x = a+b-c,
- have higher precedence than
= but also equal, so a+b-c will be evaluated first from left to right i.e., first b-c will be evaluated and then its result will be added to a and at last,
= operator set the value of x to the final result.