Python Operators - Important Points
Python is a widely used high-level programming language known for its readability and simplicity. It has a variety of operators that allow programmers to perform various operations on data types such as numbers, strings, and lists. In this write-up, we will cover all the important points about Python operators that a beginner needs to know.
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations such as addition, subtraction, multiplication, and division. The following table summarizes the arithmetic operators in Python:
Operator |
Description |
+ |
Addition |
- |
Subtraction |
* |
Multiplication |
/ |
Division |
// |
Floor Division |
% |
Modulus |
** |
Exponentiation |
For example:
x = 10
y = 5
print(x + y) # Output: 15
print(x - y) # Output: 5
print(x * y) # Output: 50
print(x / y) # Output: 2.0
print(x // y) # Output: 2
print(x % y) # Output: 0
print(x ** y) # Output: 100000
Comparison Operators
Comparison operators are used to compare two values and return a Boolean value (True or False). The following table summarizes the comparison operators in Python:
Operator |
Description |
== |
Equal to |
!= |
Not equal to |
< |
Less than |
> |
Greater than |
<= |
Less than or equal to |
>= |
Greater than or equal to |
For example:
x = 10
y = 5
print(x == y) # Output: False
print(x != y) # Output: True
print(x < y) # Output: False
print(x > y) # Output: True
print(x <= y) # Output: False
print(x >= y) # Output: True
Logical Operators
Logical operators are used to combine two or more conditions and return a Boolean value. The following table summarizes the logical operators in Python:
Operator |
Description |
and |
Returns True if both conditions are True |
or |
Returns True if at least one condition is True |
not |
Reverses the result of the condition |
For example:
x = 10
y = 5
z = 15
print(x < y and y < z) # Output: False
print(x < y or y < z) # Output: True
print(not(x < y or y < z)) # Output: False
Assignment Operators
Assignment operators are used to assign values to variables. The following table summarizes the assignment operators in Python:
Operator |
Example |
Equivalent to |
= |
x = 10 |
x = 10 |
+= |
x += 5 |
x = x + 5 |
-= |
x -= 5 |
x = x - 5 |
*= |
x *= 5 |
x = x * 5 |
/= |
x /= 5 |
x = x / 5 |
%= |
x %= 5 |
x = x % 5 |
//= |
x //= 5 |
x = x // 5 |
**= |
x **= 5 |
x = x ** 5 |
For example:
x = 10
x += 5
print(x) # Output: 15
x %= 3
print(x) # Output: 0
Bitwise Operators
Bitwise operators are used to perform operations on binary representations of numbers. The following table summarizes the bitwise operators in Python:
Operator |
Description |
& |
Bitwise AND |
| |
Bitwise OR |
^ |
Bitwise XOR |
~ |
Bitwise NOT |
<< |
Left shift |
>> |
Right shift |
For example:
x = 10 # Binary representation: 1010
y = 6 # Binary representation: 0110
print(x & y) # Output: 2 (Binary representation: 0010)
print(x \| y) # Output: 14 (Binary representation: 1110)
print(x ^ y) # Output: 12 (Binary representation: 1100)
print(~x) # Output: -11 (Binary representation: 11111111111111111111111111110101)
print(x << 1) # Output: 20 (Binary representation: 10100)
print(y >> 1) # Output: 3 (Binary representation: 0011)
Membership Operators
Membership operators are used to test if a value is present in a sequence such as a string, list, or tuple. The following table summarizes the membership operators in Python:
Operator |
Description |
in |
Returns True if the value is present in the sequence |
not in |
Returns True if the value is not present in the sequence |
For example:
x = [1, 2, 3, 4, 5]
print(3 in x) # Output: True
print(6 not in x) # Output: True
Identity Operators
Identity operators are used to compare the memory locations of two objects. The following table summarizes the identity operators in Python:
Operator |
Description |
is |
Returns True if both variables are the same object |
is not |
Returns True if both variables are not the same object |
For example:
x = [1, 2, 3]
y = [1, 2, 3]
z = x
print(x is y) # Output: False
print(x is not y) # Output: True
print(x is z) # Output: True