In this tutorial you will learn:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Bitwise operators
- Membership operators
- Identity operators
Introduction:
Before we jump to the operators lets define what is an operator. An operator is a special symbol that python or any other programming language use to
make some arithmetic, assignment or logical computation.
1 Arithmetic operators:
Arithmetic operators are used to perform mathematical operations that we are familliar with, like addition, subtraction, division and multiplication etc.
| Operator | Explanation |
|---|---|
| + | Addition operator: add two operands (10 + 3) 10 and 3 are the operands |
| - | Subtruction operator: Subtract the right operand from the left operand |
| / | Division operator: Devide left operand by the right operand (the result of this division is always a float number) |
| * | Multiplication operator: Multiply two operands |
| // | Floor division operator: Devide left operand by the right operand (the result of this division is always an int number) |
| % | Modulus operator: Devide left operand by the right operand (the result is the remainder of the division) |
| ** | Exponent operator: Perfoms exponential calculation by rasing the left operand to the power of the right operand |
Lets see some examples:
0#Addition
1a = 4 + 5
2print("a =", a)
3
4#Subtraction
5b = 9 - 5
6print("b =", b)
7
8#Division
9c = 20 / 3
10print("c =", c)
11
12#Multiplication
13d = 2 * 4
14print("d =", d)
15
16#Floor division
17e = 20 // 3
18print("e =", e)
19
20#Modulus
21f = 10 % 2
22print("f =", f)
23
24#Exponent
25g = 5 ** 2
26print("g =", g)OUTPUT: a = 9
OUTPUT: b = 4
OUTPUT: c = 6.66666666667
OUTPUT: d = 8
OUTPUT: e = 6
OUTPUT: f = 0
OUTPUT: g = 25
As you see this is how we can use these operators in python.
Lets take a look at the division operator, you see when we devide 20 by 3 (20 / 3) we get a floating point number : 6.66666666667, but
when we use the Floor division operator we get an integer number: 6, this effect is the same when we use the int() built-in function to convert a
float number.
Now lets look at the modulus operators:
When we devide 10 by 2 the result will be 5 which mean there is zero remainder, and this is the job of the modulus it devides two numbers and return the remainder of the
division.
2.Assignment operators:
We have already used an assignment operator which is the ‘=’ symbol.
We use the assignment operators to assign values to variables.
| Operator | Explanation |
|---|---|
| = | Assign value from right side operand to left side (x = 5) |
| += | Add and assign: (x += 5) this is equivalent to x = x + 5 |
| -= | Subtract and assign: (x -= 5) this is equivalent to x = x - 5 |
| /= | Devide and assign: (x /= 5) this is equivalent to x = x / 5 |
| *= | Multiply and assign: (x *= 5) this is equivalent to x = x * 5 |
| %= | Modulus and assign: (x %= 5) this is equivalent to x = x % 5 |
| //= | Floor division and assign: (x //= 5) this is equivalent to x = x // 5 |
| **= | Exponent and assign: (x **= 2) this is equivalent to x = x ** 2 |
| >>= | Bitwise right shift and assign |
| <<= | Bitwise left shift and assign |
| ^= | Bitwise XOR and assign |
| ~= | Bitwise NOT and assign |
| |= | Bitwise OR and assign |
| &= | Bitwise AND and assign |
Lets see some code examples:
0#Assign 5 to x
1x = 5
2
3#Add 5 and assign
4x += 8
5print("x :", x)
6
7#Subtract and assign
8x -= 2
9print("x :", x)
10
11#Devide and assign
12x /= 2
13print("x :", x)
14
15#Multiply and assign
16x *= 5
17print("x :", x)
18
19#Modulus and assign
20x %= 2
21print("x :", x)
22
23#Exponent adn assign
24x **= 5
25print("x :", x)
26
27#Floor division and assign
28x //= 5
29print("x :", x)OUTPUT: x : 13
OUTPUT: x : 11
OUTPUT: x : 5.5
OUTPUT: x : 27.5
OUTPUT: x : 1.5
OUTPUT: x : 7.59375
OUTPUT: x : 1.0
3 Comparison operators:
We use comparison operators to compare between values, the ruslt of the comparison will be a bool (True or False).
| Operator | Explanation |
|---|---|
| > | Greater than: if the left operand is greater than the right operand the result will be True (5 > 2) |
| < | Less than: if the left operand is less than the right operand the result will be True (2 < 5) |
| == | Equal to: if both the left and right operands are equal the result will be True (5 == 5) |
| >= | Greater than or equal: if the left operand is greater than or equal to the right operand the result will be True(5 >= 2) (5 >= 5) both are True |
| <= | Less than or equal: if the left operand is less than or equal to the right operand the result will be True (5 <= 2) (5 <= 5) both are True |
| != | Not equal to: if the left operand is different from the right operand the result will be True (5 != 2) |
Lets see these operators in code:
0#OUTPUT True
1print(5 > 2)
2
3#OUTPUT False
4print(5 > 10)
5
6#OUTPUT True
7print(3 < 5)
8
9#OUTPUT False
10print(3 < 1)
11
12#OUTPUT True
13print(5 == 5)
14
15#OUTPUT False
16print(5 == 6)
17
18#OUTPUT True
19print(8 >= 8)
20
21#OUTPUT False
22print(8 >= 9)
23
24#OUTPUT True
25print(12 <= 15)
26
27#OUTPUT False
28print(12 <= 10)
29
30#OUTPUT True
31print(5 != 10)
32
33#OUTPUT False
34print(5 != 5)4 Logical operators:
In python we have three logical operators “and”, “or”, “not”, these operators return a boolean result based on the condition.
| Operator | Explanation |
|---|---|
| and | This will return True when both left and right operand are True |
| or | This will return True if one the operand is True |
| not | This will return the inverse of the operand, if the operand is True it will return False |
Lest see some examples:
0#OUTPUT True
1x = (5 > 2 and 3 > 1)
2print("x:", x)
3
4#OUTPUT True
5y = (5 == 5 or 5 > 8)
6print("y:", y)
7
8#OUTPUT False
9z = (not 5 > 2)
10print("z:", z)Lets take a look at this code:
In the first example we have assigned to “x” a logical operation, the first part is (5 > 2) this will return True, and the second part
is (3 > 1) which also return True, and by using the logical “and” operator: True and True will return a True. If one of the comparison operator returned False, then
the result of our logical operation will be False.
The second example is also devided in two part, the first is (5 == 5) which returns True and the second part is (5 > 8) which returns False, by using the “or” logical
operator the result will be True, because the “or” operator needs one of the two operand to be True to return True.
The last example is very simple the comparison operation (5 > 2) return True, and by using the “not” operator we get the inverse of the result which is False in our case.
5 Bitwise operators:
Bitwise operators are used to perform a bit by bit operation.
| Operator | Explanation |
|---|---|
| >> | Bitwise right shift |
| << | Bitwise left shift |
| & | Bitwise AND |
| | | Bitwise OR |
| ~ | Bitwise NOT |
| ^ | Bitwise XOR |
Lets see some examples:
0#x = 3 in binary
1x = 0b00000011
2
3#y = 242 in binary
4y = 0b11110010
5
6a = x & y
7
8'''
9a = x & y
10
11Lets see how that happened
12
13x = 00000011
14y = 11110010
15-----------------
16x & y = 00000010
17'''
18
19#OUTPUT 2
20print(a)As you see the “&” operator works like this: when it finds 1 1 it returns 1, if it finds 1 0 or 0 1 or 0 0 it returns 0.
To read more about bitwise operations click here
6 Membership operators:
Membership operators are used to check whether a value is in a sequence or not (like string, or list etc.)
| Operator | Explanation |
|---|---|
| in | It will return True if the value is in the sequence |
| not in | It will return True if the value is not in the sequence |
0x = "Hello World"
1
2#OUTPUT True
3print('H' in x)
4
5#OUTPUT True
6print('h' not in x)In the first example returns True because the uppercase ‘H’ is in the sequence, and the second example returns True because the lowecase ‘h’ is not in the sequence.
This mean that you need to be careful with this because it’s case sensitive, ‘h’ is different from ‘H’.
7 Identity operators:
Identity operators are used to check if two variables are located in the same memory location.
| Operator | Explanation |
|---|---|
| is | It will return True if both left and right operand are located on the same location in memory |
| is not | It will return True if both left and right operand are not located on the same location in memory |
0x = 5
1y = 5.4
2
3#OUTPUT False
4print(x is y)
5
6#OUTPUT True
7print(x is not y)
8
9a = "hello world"
10b = "hello world"
11
12#OUTPUT False
13print(a is b)
14
15#OUTPUT True
16print(a is not b)In this example we see that the variables “a” and “b” are both identical but when we check “a is b” we get False this mean that each one is located in a different location in memory. You can check the location of your variables using the id() built-in function:
0x = 5
1
2#OUTPUT 49301269 THIS NUMBER WILL BE DIFFERENT ON YOUR MACHINE
3print(id(x))This is all for this tutorial, I hope you enjoyed it.