is
, in
)Operators are symbols or keywords that perform operation on variables and values. Python supports a wide range of operator types.
Operator | Description | Example | Output |
---|---|---|---|
+ | Addition | 5 + 2 | 7 |
- | Subtraction | 5 - 2 | 3 |
* | Multiplication | 5 * 2 | 10 |
/ | Division | 5 / 2 | 2.5 |
// | Floor Division | 5 // 2 | 2 |
% | Modulus (remainder) | 5 % 2 | 1 |
** | Exponentiation | 2 ** 3 | 8 |
a = 10
b = 3
print(a + b) # 13
print(a ** b) # 1000
Used to compare two values. Returns True
or False
.
Operator | Meaning | Example |
---|---|---|
== | Equal to | 5 == 5 → True |
!= | Not equal to | 5 != 3 → True |
> | Greater than | 5 > 3 → True |
< | Less than | 5 < 3 → False |
>= | Greater or equal | 5 >= 5 → True |
<= | Less or equal | 3 <= 5 → True |
x = 5
print(5 == x) # True
print(2 == x) # False
print(2 != x) # True
Used to combine conditional statements.
Operator | Description | Example |
---|---|---|
and | True if both are True | True and True → True |
or | True if any is True | True or False → True |
not | Inverts the result | not True → False |
x = 5
print(x > 2 and x < 10) # True
Assign values to variables.
Operator | Example | Same As |
---|---|---|
= | x = 5 | x = 5 |
+= | x += 3 | x = x + 3 |
-= | x -= 2 | x = x - 2 |
*= | x *= 2 | x = x * 2 |
/= | x /= 2 | x = x / 2 |
%= | x %= 2 | x = x % 2 |
**= | x **= 2 | x = x ** 2 |
x = 10
x += 5
print(x) # 15
Used to check if two variables refer to the same object.
Operator | Meaning | Example |
---|---|---|
is | True if both refer to same obj | x is y |
is not | True if not same object | x is not y |
a = [1, 2]
b = a
print(a is b) # True
print(a is not b) # False
Used to test if a sequence contains a value.
Operator | Meaning | Example |
---|---|---|
in | True if value exists | 3 in [1,2,3] |
not in | True if value doesn’t | 4 not in [1,2,3] |
Perform bit-level operations.
Operator | Meaning | Example |
---|---|---|
& | AND | 5 & 3 → 1 |
` | ` | OR |
^ | XOR | 5 ^ 3 → 6 |
~ | NOT | ~5 → -6 |
<< | Left Shift | 2 << 2 → 8 |
>> | Right Shift | 8 >> 2 → 2 |
Some operators are evaluated before others.
result = 2 + 3 * 4 # Output: 14, not 20!
Precedence (from high to low):
1. ** (Exponent)
2. *, /, %, //
3. +, -
4. Comparison (>, <, ==)
5. Logical (not → and → or)
Use parentheses ()
to control evaluation order.
Operator Type | Examples |
---|---|
Arithmetic | + , - , * , / |
Comparison | == , != , < , > |
Logical | and , or , not |
Assignment | = , += , *= |
Identity | is , is not |
Membership | in , not in |
Bitwise | & , ` |
and
, or
is
, in
with lists and integers**=
, //=
Leave a Reply