Operators in Python & Operator Precedence
๐น What are Operators in Python?
Operators are symbols that perform operations on variables and values.
Example:
x = 10 + 5 # '+' is an operator
๐น Types of Operators in Python
Python provides the following categories of operators:
Category | Operators | Example |
---|---|---|
Arithmetic | + - * / % // ** |
5 + 2 = 7 |
Comparison | == != > < >= <= |
10 > 5 โ True |
Logical | and or not |
True and False โ False |
Bitwise | `& | ^ ~ << >>` |
Assignment | `= += -= *= /= %= //= **= &= | = ^= >>= <<= ` |
Identity | is, is not |
x is y |
Membership | in, not in |
'a' in 'apple' โ True |
1๏ธโฃ Arithmetic Operators
These are used for mathematical operations.
Operator | Description | Example |
---|---|---|
+ |
Addition | 5 + 3 = 8 |
- |
Subtraction | 5 - 3 = 2 |
* |
Multiplication | 5 * 3 = 15 |
/ |
Division (Float) | 5 / 2 = 2.5 |
// |
Floor Division | 5 // 2 = 2 |
% |
Modulus (Remainder) | 5 % 2 = 1 |
** |
Exponentiation | 2 ** 3 = 8 |
โ Example:
a = 10
b = 3
print(a + b) # Output: 13
print(a // b) # Output: 3 (Floor Division)
print(a ** b) # Output: 1000 (10^3)
2๏ธโฃ Comparison Operators
These return True or False based on conditions.
Operator | Description | Example |
---|---|---|
== |
Equal to | 5 == 5 โ True |
!= |
Not equal to | 5 != 3 โ True |
> |
Greater than | 5 > 3 โ True |
< |
Less than | 5 < 3 โ False |
>= |
Greater than or equal | 5 >= 5 โ True |
<= |
Less than or equal | 5 <= 3 โ False |
โ Example:
x = 10
y = 20
print(x < y) # Output: True
print(x == y) # Output: False
3๏ธโฃ Logical Operators
These combine multiple conditions.
Operator | Description | Example |
---|---|---|
and |
Returns True if both conditions are True | (5 > 3) and (10 > 5) โ True |
or |
Returns True if at least one condition is True | (5 > 3) or (10 < 5) โ True |
not |
Negates a condition | not(5 > 3) โ False |
โ Example:
a = 10
b = 5
c = 20
print(a > b and c > a) # Output: True
print(a > c or b < c) # Output: True
print(not(a > b)) # Output: False
4๏ธโฃ Bitwise Operators
Used to manipulate bits.
Operator | Description | Example (5 = 0101, 3 = 0011) |
---|---|---|
& |
AND (Bitwise) | 5 & 3 โ 0001 (1) |
` | ` | OR (Bitwise) |
^ |
XOR (Bitwise) | 5 ^ 3 โ 0110 (6) |
~ |
NOT (Bitwise) | ~5 โ -6 |
<< |
Left Shift | 5 << 1 โ 1010 (10) |
>> |
Right Shift | 5 >> 1 โ 0010 (2) |
โ Example:
x = 5 # 0101 in binary
y = 3 # 0011 in binary
print(x & y) # Output: 1 (0001)
print(x | y) # Output: 7 (0111)
print(x ^ y) # Output: 6 (0110)
print(x << 1) # Output: 10 (1010)
print(x >> 1) # Output: 2 (0010)
5๏ธโฃ Assignment Operators
Used to assign values to variables.
Operator | Example | Equivalent To |
---|---|---|
= |
x = 5 |
x = 5 |
+= |
x += 3 |
x = x + 3 |
-= |
x -= 2 |
x = x - 2 |
*= |
x *= 4 |
x = x * 4 |
/= |
x /= 2 |
x = x / 2 |
%= |
x %= 3 |
x = x % 3 |
โ Example:
x = 10
x += 5 # x = x + 5
print(x) # Output: 15
6๏ธโฃ Identity Operators
Used to compare memory locations of objects.
Operator | Example | Description |
---|---|---|
is |
x is y |
True if both refer to the same object |
is not |
x is not y |
True if not the same object |
โ Example:
a = [1, 2, 3]
b = a
c = [1, 2, 3]
print(a is b) # True (same object)
print(a is c) # False (different objects)
print(a == c) # True (same values)
7๏ธโฃ Membership Operators
Check if a value exists in a sequence (list
, tuple
, string
, etc.).
Operator | Example | Description |
---|---|---|
in |
'a' in 'apple' |
True if exists |
not in |
'x' not in 'abc' |
True if not exists |
โ Example:
fruits = ["apple", "banana", "cherry"]
print("banana" in fruits) # True
print("grape" not in fruits) # True
๐น Operator Precedence
Python follows precedence rules when multiple operators are used.
Operator | Precedence (Highest to Lowest) |
---|---|
() |
Parentheses (Highest) |
** |
Exponentiation |
* / // % |
Multiplication, Division, Modulus |
+ - |
Addition, Subtraction |
<< >> |
Bitwise shifts |
& |
Bitwise AND |
^ |
Bitwise XOR |
` | ` |
== != > < |
Comparison |
and or not |
Logical Operators |
= += -= |
Assignment (Lowest) |
Would you like more examples on operator precedence? ๐
Leave a Reply