In Python, operators are used to perform operations on variables and values. Python supports a wide variety of operators, which can be categorized as follows:
These operators are used to perform basic mathematical operations such as addition, subtraction, multiplication, etc.
Operator | Description | Example |
---|---|---|
+ | Addition | 5 + 3 results in 8 |
- | Subtraction | 5 - 3 results in 2 |
* | Multiplication | 5 * 3 results in 15 |
/ | Division | 5 / 3 results in 1.666... |
// | Floor Division | 5 // 3 results in 1 |
% | Modulus (remainder) | 5 % 3 results in 2 |
** | Exponentiation | 5 ** 3 results in 125 |
x = 10
y = 3
print(x + y) # Addition: 10 + 3 = 13
print(x - y) # Subtraction: 10 - 3 = 7
print(x * y) # Multiplication: 10 * 3 = 30
print(x / y) # Division: 10 / 3 = 3.3333...
print(x // y) # Floor Division: 10 // 3 = 3
print(x % y) # Modulus: 10 % 3 = 1
print(x ** y) # Exponentiation: 10 ** 3 = 1000
Comparison operators are used to compare two values and return a Boolean result (True
or False
).
Operator | Description | Example |
---|---|---|
== | Equal to | 5 == 3 results in False |
!= | Not equal to | 5 != 3 results in True |
> | Greater than | 5 > 3 results in True |
< | Less than | 5 < 3 results in False |
>= | Greater than or equal to | 5 >= 3 results in True |
<= | Less than or equal to | 5 <= 3 results in False |
x = 10
y = 5
print(x == y) # False
print(x != y) # True
print(x > y) # True
print(x < y) # False
print(x >= y) # True
print(x <= y) # False
Logical operators are used to combine conditional statements.
Operator | Description | Example |
---|---|---|
and | Returns True if both statements are true | True and False results in False |
or | Returns True if one of the statements is true | True or False results in True |
not | Reverses the result, returns True if the statement is false | not True results in False |
x = 10
y = 5
print(x > y and y > 3) # True
print(x < y or y > 3) # True
print(not(x > y)) # True
Assignment operators are used to assign values to variables.
Operator | Description | Example |
---|---|---|
= | Assigns a value | x = 10 |
+= | Adds and assigns the value | x += 5 (equivalent to x = x + 5 ) |
-= | Subtracts and assigns the value | x -= 5 (equivalent to x = x - 5 ) |
*= | Multiplies and assigns the value | x *= 5 (equivalent to x = x * 5 ) |
/= | Divides and assigns the value | x /= 5 (equivalent to x = x / 5 ) |
//= | Floor divides and assigns the value | x //= 5 (equivalent to x = x // 5 ) |
%= | Modulus and assigns the value | x %= 5 (equivalent to x = x % 5 ) |
**= | Exponentiates and assigns the value | x **= 5 (equivalent to x = x ** 5 ) |
x = 10
x += 5 # x = x + 5 -> 15
x -= 3 # x = x - 3 -> 12
x *= 2 # x = x * 2 -> 24
x /= 4 # x = x / 4 -> 6.0
Membership operators are used to test if a sequence (like a list, tuple, or string) contains a value.
Operator | Description | Example |
---|---|---|
in | Returns True if the value is found in the sequence | 'a' in 'apple' results in True |
not in | Returns True if the value is not found in the sequence | 'b' not in 'apple' results in True |
x = [1, 2, 3, 4, 5]
print(3 in x) # True
print(6 not in x) # True
Identity operators are used to compare the memory locations of two objects.
Operator | Description | Example |
---|---|---|
is | Returns True if both variables point to the same object | x is y |
is not | Returns True if both variables do not point to the same object | x is not y |
x = [1, 2, 3]
y = x
z = [1, 2, 3]
print(x is y) # True
print(x is z) # False
print(x is not z) # True