Operator Types
- Arithmetic Operators
- Assignment Operators
- Comparison (Relational) Operators
- Logical Operators
- Bitwise Operators
- Membership Operators
- Identity Operators
Python operators are special symbols used to perform specific operations on one or more operands.
The variables, values, or expressions can be used as operands. For example, Python’s addition operator (+) is used to perform addition operations on two variables, values, or expressions.
Python operators are categorized in the following categories −
Operator Types
Python Arithmetic operators
are used to perform basic mathematical operations such as addition, subtraction, multiplication, division, etc. The following table contains all arithmetic operators with their symbols, names, and examples (assume that the values of a and b are 10 and 20, respectively).
Operator | Name | Example |
---|---|---|
+ | Addition | a + b = 30 |
- | Subtraction | a - b = -10 |
* | Multiplication | a * b = 200 |
/ | Division | b / a = 2 |
% | Modulus | b % a = 0 |
** | Exponent | a ** b = 10**20 |
// | Floor Division | 9 // 2 = 4 |
a = 7b = 4c = a + bprint(f"The sum of {a} and {b} is: {c}")
The sum of 7 and 4 is: 11
Python Assignment operators
are used to assign values to variables. Following is a table which shows all Python assignment operators.
Operator | Example | Same As |
---|---|---|
= | x = 5 | x = 5 |
+= | x += 3 | x = x + 3 |
-= | x -= 3 | x = x - 3 |
*= | x *= 3 | x = x * 3 |
/= | x /= 3 | x = x / 3 |
%= | x %= 3 | x = x % 3 |
//= | x //= 3 | x = x // 3 |
**= | x **= 3 | x = x ** 3 |
&= | x &= 3 | x = x & 3 |
^= | x ^= 3 | x = x ^ 3 |
>>= | x >>= 3 | x = x >> 3 |
<<= | x <<= 3 | x = x << 3 |
:= | print(x := 3) | x = 3 then print(x) |
a = 4-2 # Assign 4-2 in aprint(a)b = 6# b += 3 # Increment the value of b by 3 and then assign it to bb *= 3 # Multiplies the value of b by 3 and then assign it to bprint(b)
218
Comparison or Relational operators are used to compare two values.
Operator | Name | Example |
---|---|---|
== | Equal | x == y |
!= | Not equal | x != y |
> | Greater than | x > y |
< | Less than | x < y |
>= | Greater than or equal to | x >= y |
<= | Less than or equal to | x <= y |
d = 5 == 5print(d)# Output: True
Python logical operators
are used to combine two or more conditions and check the final result. There are following logical operators supported by Python language.
Operator | Description | Example |
---|---|---|
and | Returns True if both statements are true | x < 5 and x < 10 |
or | Returns True if one of the statements is true | x < 5 or x < 4 |
not | Reverse the result | not(x < 5 and x < 10) |
# Truth table of 'or'print("True or False is ", True or False)print("True or True is ", True or True)print("False or True is ", False or True)print("False or False is ", False or False)
# Truth table of 'and'print("True and False is ", True and False)print("True and True is ", True and True)print("False and True is ", False and True)print("False and False is ", False and False)
print(not(True))print(not(False))
True or False is TrueTrue or True is TrueFalse or True is TrueFalse or False is FalseTrue and False is FalseTrue and True is TrueFalse and True is FalseFalse and False is FalseFalseTrue
Python Bitwise operator
works on bits and performs bit by bit operation. These operators are used to compare binary numbers.
Operator | Name | Description | Example |
---|---|---|---|
& | AND | Sets each bit to 1 if both bits are 1 | x & y |
| | OR | Sets each bit to 1 if one of two bits is 1 | x | y |
^ | XOR | Sets each bit to 1 if only one of two bits is 1 | x ^ y |
~ | NOT | Inverts all the bits | ~x |
<< | Zero fill left shift | Shift left by pushing zeros in from the right and letting leftmost bits fall off | x << 2 |
>> | Signed right shift | Shift right by pushing copies of the leftmost bit in from the left, letting rightmost bits fall off | x >> 2 |
a = 10 # 1010 in binaryb = 4 # 0100 in binary
print(f"a & b = {a & b}") # 1010 & 0100 = 0000 (0)print(f"a | b = {a | b}") # 1010 | 0100 = 1110 (14)print(f"a ^ b = {a ^ b}") # 1010 ^ 0100 = 1110 (14)
print(f"~a = {~a}") # Inverts the bits of 1010, gives -(a+1), i.e., -11print(f"a << 1 = {a << 1}") # Left shifts bits of 1010 to 10100 (20)print(f"a >> 1 = {a >> 1}") # Right shifts bits of 1010 to 0101 (5)
a & b = 0a | b = 14a ^ b = 14~a = -11a << 1 = 20a >> 1 = 5
Python’s membership operators
test for membership in a sequence, such as strings, lists, or tuples.
Operator | Description | Example |
---|---|---|
in | Returns True if a sequence with the specified value is present in the object | x in y |
not in | Returns True if a sequence with the specified value is not present in the object | x not in y |
my_list = [1, 2, 3, 4, 5]my_string = "Hello, Akash"
print(6 in my_list)print("Akash" in my_string)
print(10 not in my_list)print("Python" not in my_list)
FalseTrueTrueTrue
Identity operators
are used to compare the objects, not if they are equal
, but if they are actually the same object, with the same memory location.
Operator | Description | Example |
---|---|---|
is | Returns True if both variables are the same object | x is y |
is not | Returns True if both variables are not the same object | x is not y |
x = 5y = 5z = [1, 2, 3]w = [1, 2, 3]
print(x is y) # True, since x and y point to the same object in memoryprint(z is w) # False, since z and w are different objects, even though their contents are the same
print(x is not y) # False, because x and y are the same objectprint(z is not w) # True, since z and w are different objects
TrueFalseFalseTrue
Practice Questions on Python Operators
Task: Write a Python program that takes two integers
a
andb
, and a listlst
of integers. Using only operators and perform the following:
a
and b
and store the results.a
using at least three different assignment operators.a
and b
using all six comparison operators and store the results as booleans.and
, or
, not
) with the comparison results.a
and b
.a
and b
in the list lst
.Output: Print all the results in a clean, labeled format (e.g.,
Sum: 30
,a in lst: True
, etc.).
a = 10b = 20lst = [5, 10, 15, 20, 25]