Operators in Python
Python operators are tools used to perform various actions with variables, as well as numerical and other values called operands—objects on which operations are performed. There are several types of Python operators:
- Arithmetic
- Comparison
- Assignment
- Identity
- Membership
- Logical
- Bitwise
This article will examine each of them in detail and provide examples.
Arithmetic Operators Copy link
For addition, subtraction, multiplication, and division, we use the Python operators +, -, *, and / respectively:
>>> 24 + 28
52
>>> 24 - 28
-4
>>> 24 * 28
672
>>> 24 / 28
0.8571428571428571
For exponentiation, ** is used:
>>> 5 ** 2
25
>>> 5 ** 3
125
>>> 5 ** 4
625
For floor division (integer division without remainder), // is used:
>>> 61 // 12
5
>>> 52 // 22
2
>>> 75 // 3
25
>>> 77 // 3
25
The % operator returns the remainder (modulo division):
>>> 62 % 6
2
>>> 65 % 9
2
>>> 48 % 5
3
>>> 48 % 12
0
Comparison Operators Copy link
Python has six comparison operators: >, <, >=, <=, ==, !=.
Note that equality in Python is written as ==, because a single = is used for assignment. The != operator is used for "not equal to." When comparing values, Python will return True or False depending on whether the expressions are true or false.
>>> 26 > 58
False
>>> 26 < 58
True
>>> 26 >= 26
True
>>> 58 <= 57
False
>>> 50 == 50
True
>>> 50 != 50
False
>>> 50 != 51
True
Assignment Operators Copy link
A single = is used for assigning values to variables:
>>> b = 5
>>> variants = 20
Python also provides convenient shorthand operators that combine arithmetic operations with assignment: +=, -=, *=, /=, //=, %=. For example:
>>> cars = 5
>>> cars += 7
>>> cars
12
This is equivalent to:
>>> cars = cars + 7
>>> cars
12
The first version is more compact. Other assignment operators work similarly:
>>> train = 11
>>> train -= 2
>>> train
9
>>> moto = 3
>>> moto *= 7
>>> moto
21
>>> plain = 8
>>> plain /= 4
>>> plain
2.0
Notice that in the last case, the result is a floating-point number (float), not an integer (int).
Identity Operators Copy link
Python has two identity operators: is and is not. The results are True or False, similar to comparison operators.
>>> 55 is 55
True
>>> 55 is 56
False
>>> 55 is not 55
False
>>> 55 is not 56
True
>>> 55 is '55'
False
>>> '55' is "55"
True
In the last two examples:
55 is '55'returnedFalsebecause an integer and a string were compared.'55' is "55"returnedTruebecause both operands are strings.
Python does not differentiate between single and double quotes, so the identity check was successful.
Membership Operators Copy link
There are only two membership operators in Python: in and not in. They check whether a certain value exists within a sequence. For example:
>>> wordlist = ('assistant', 'streetcar', 'fraudster', 'dancer', 'heat', 'blank', 'compass', 'commerce', 'judgment', 'approach')
>>> 'house' in wordlist
False
>>> 'assistant' in wordlist
True
>>> 'assistant' and 'streetcar' in wordlist
True
In the last case, a logical operator (and) was used, which leads us to the next topic.
Logical Operators Copy link
Python has three logical operators: and, or, and not.
-
andreturnsTrueonly if all operands are true. It can process any number of values. Using an example from the previous section:
>>> wordlist = ('assistant', 'streetcar', 'fraudster', 'dancer', 'heat', 'blank', 'compass', 'commerce', 'judgment', 'approach')
>>> 'assistant' and 'streetcar' in wordlist
True
>>> 'fraudster' and 'dancer' and 'heat' and 'blank' in wordlist
True
>>> 'fraudster' and 'dancer' and 'heat' and 'blank' and 'house' in wordlist
False
Since 'house' is not in the sequence, the result is False. These operations also work with numerical values:
>>> numbers = 54 > 55 and 22 > 21
>>> print(numbers)
False
One of the expressions is false, and and requires all conditions to be true.
-
orworks differently: it returnsTrueif at least one operand is true. If we replace and with or in the previous example, we get:
>>> numbers = 54 > 55 or 22 > 21
>>> print(numbers)
True
Here, 22 > 21 is true, so the overall expression evaluates to True, even though 54 > 55 is false.
-
notreverses logical values:
>>> first = True
>>> second = False
>>> print(not first)
False
>>> print(not second)
True
As seen in the example, not flips True to False and vice versa.
Bitwise Operators Copy link
Bitwise operators are used in Python to manipulate objects at the bit level. There are five of them (shift operators belong to the same type, as they differ only in shift direction):
&(AND)|(OR)^(XOR)~(NOT)<<and>>(shift operators)
Bitwise operators are based on Boolean logic principles and work as follows:
-
&(AND) returns1if both operands contain1; otherwise, it returns0:
>>> 1 & 1
1
>>> 1 & 0
0
>>> 0 & 1
0
>>> 0 & 0
0
-
|(OR) returns1if at least one operand contains1, otherwise0:
>>> 1 | 1
1
>>> 1 | 0
1
>>> 0 | 1
1
>>> 0 | 0
0
-
^(XOR) returns1if the operands are different and0if they are the same:
>>> 1 ^ 1
0
>>> 1 ^ 0
1
>>> 0 ^ 1
1
>>> 0 ^ 0
0
-
~(NOT) inverts bits, turning positive values into negative ones with a shift of one:
>>> ~5
-6
>>> ~-5
4
>>> ~7
-8
>>> ~-7
6
>>> ~9
-10
>>> ~-9
8
-
<<and>>shift bits by a specified number of positions:
>>> 1 << 1
2
>>> 1 >> 1
0
To understand shifts, let’s break down values into bits:
0 = 00
1 = 01
2 = 10
Shifting 1 left by one bit gives 2, while shifting right results in 0. What happens if we shift by two positions?
>>> 1 << 2
4
>>> 1 >> 2
0
1 = 001
2 = 010
4 = 100
Shifting 1 two places to the left results in 4 (100 in binary). Shifting right always results in zero because bits are discarded.
For more details, refer to our article on bitwise operators.
Difference Between Operators and Functions Copy link
You may have noticed that we have included no functions in this overview. The confusion between operators and functions arises because both perform similar actions—transforming objects. However:
- Functions are broader and can operate on strings, entire blocks of code, and more.
- Operators work only with individual values and variables.
In Python, a function can consist of a block of operators, but operators can never contain functions.