python operators enable manipulation on variables and value(operands)
- Arithmetic Operators
- Assignment Operators
- Logical Operators
- Bitwise Operators
- Membership Operators
- Identity Operators
1. Arithmetic Operators
Operator | Description | Syntax | Example |
---|---|---|---|
+ | Addition | x + y | 1 + 2 = 3 |
– | Subtraction | x - y | 3 - 1 = 2 |
* | Multiplication | x * y | 2 * 3 = 6 |
/ | Division | x / y | 6 / 3 = 2.0 |
% | Modulus (returns the remainder of division) | x % y | 7 % 3 = 1 |
** | Exponentiation (raise to the power of) | x ** y | 2 ** 3 = 8 |
// | Floor division (rounds down to the nearest whole number) | x // y | 7 // 3 = 2 |
2. Assignment Operators
Operator | Description | Syntax | Example |
---|---|---|---|
== | Equal to | x == y | 2 == 2 returns True |
!= | Not equal to | x != y | 2 != 3 returns True |
> | Greater than | x > y | 3 > 2 returns True |
< | Less than | x < y | 2 < 3 returns True |
>= | Greater than or equal to | x >= y | 3 >= 3 returns True |
<= | Less than or equal to | x <= y | 2 <= 2 returns True |
3. Logical Operators
Operator | Description | Syntax | Example |
---|---|---|---|
and | Logical AND | x and y | True and False returns False |
or | Logical OR | x or y | True or False returns True |
not | Logical NOT | not x | not False returns True |
4. Bitwise Operators
Bitwise Operators | Description | Syntax | Example |
---|---|---|---|
& | Bitwise AND | x & y | 5 & 3 returns 1 |
Bitwise OR | `x | ||
^ | Bitwise XOR | x ^ y | 5 ^ 3 returns 6 |
~ | Bitwise NOT | ~x | ~5 returns -6 |
<< | Bitwise Left Shift | x << y | 5 << 2 returns 20 |
>> | Bitwise Right Shift | x >> y | 5 >> 2 returns 1 |
5. Membership Operators
Membership Operators | Description | Syntax | Example |
---|---|---|---|
in | Returns True if a value is found in a sequence (e.g. list, tuple, string, etc.) | x in y | 2 in [1, 2, 3] returns True |
not in | Returns True if a value is not found in a sequence (e.g. list, tuple, string, etc.) | x not in y | 2 not in [4, 5, 6] returns True |
6. Identity Operators
Identity Operators | Description | Syntax | Example |
---|---|---|---|
is | Returns True if two variables refer to the same object in memory | x is y | a = [1, 2, 3] and b = [1, 2, 3] and a is b returns False |
is not | Returns True if two variables do not refer to the same object in memory | x is not y | a = [1, 2, 3] and b = [1, 2, 3] and a is not b returns True |
These operators can be used in various combinations to perform more advanced operations in Python.
Example code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# Arithmetic Operators a = 9 b = 4 # Addition print(a + b) # Output: 13 # Subtraction print(a - b) # Output: 5 # Multiplication print(a * b) # Output: 36 # Division print(a / b) # Output: 2.25 # Floor Division print(a // b) # Output: 2 # Modulo print(a % b) # Output: 1 # Exponent print(a ** b) # Output: 6561 # Comparison Operators c = 10 d = 20 # Equal print(c == d) # Output: False # Not Equal print(c != d) # Output: True # Greater than print(c > d) # Output: False # Less than print(c < d) # Output: True # Greater than or equal to print(c >= d) # Output: False # Less than or equal to print(c <= d) # Output: True # Bitwise Operators e = 5 f = 3 # Bitwise AND print(e & f) # Output: 1 # Bitwise OR print(e | f) # Output: 7 # Bitwise XOR print(e ^ f) # Output: 6 # Bitwise NOT print(~e) # Output: -6 # Bitwise Left Shift print(e << 2) # Output: 20 # Bitwise Right Shift print(e >> 2) # Output: 1 # Membership Operators g = [1, 2, 3] h = [4, 5, 6] # in print(2 in g) # Output: True # not in print(2 not in h) # Output: True # Identity Operators i = [1, 2, 3] j = [1, 2, 3] # is print(i is j) # Output: False # is not print(i is not j) # Output: True |