Operators and type of operators
An Operator is a symbol that performs an operation on one or more Operands, i.e. numeric
values.
For example, in expression 10 + 5, the + is the operator, and 10 and 5 are the operands.
1. Arithmetic Operators
Used to perform common mathematical operations.
Operator Name Example Result (if x=10, y=3)
+ Addition x + y 13
- Subtraction x - y 7
* Multiplication x * y 30
/ Division x / y 3.3333 (Always returns a float)
% Modulus x % y 1 (The remainder)
** Exponentiation x ** y 1000 (10^3)
3 (Removes the decimal, useful to
// Floor Division x // y
find the remainder value)
2. Comparison (Relational) Operators
Used to compare two values. They always return a Boolean (True or False).
• == : Equal to
• != : Not equal to
• > : Greater than
• < : Less than
• >= : Greater than or equal to
• <= : Less than or equal to
3. Logical Operators
Used to combine conditional statements.
• and: Returns True if both statements are true. (e.g., x < 5 and x < 10)
• or: Returns True if at least one statement is true. (e.g., x < 5 or x < 4)
• not: Reverse the result. Returns False if the result is true. (e.g., not(x < 5))
4. Assignment Operators
Used to assign values to variables.
• = : Standard assignment (x = 5)
• += : Add and Assign (x += 3 is the same as x = x + 3)
• -= : Subtract and Assign (x -= 3)
• *= : Multiply and Assign (x *= 3)
5. Bitwise Operators
These act on bits and perform bit-by-bit operations.
• & (AND), | (OR), ^ (XOR), ~ (NOT), << (Zero fill left shift), >> (Signed right shift).
6. Special Operators
Python includes two unique sets of operators that make code more readable:
A. Identity Operators
Used to check if two objects are actually the same object in memory.
• is: Returns True if both variables point to the same object.
• is not: Returns True if they are different objects.
B. Membership Operators
Used to test if a sequence (like a string or list) is present in an object.
• in: Returns True if the value is found.
• not in: Returns True if the value is not found.
Order of Operations (Operator Precedence)
When we use more than one operator in a Python expression, Python needs to know which one
to solve first. This is called operator precedence. For example, in 2 + 3 * 4, Python does the
multiplication first, not the addition, because * has higher precedence than +.
Python follows the PEMDAS rule, which is the same as standard mathematics, but adapted for
programming symbols and which helps us solve arithmetic expressions in the correct order:
P – Parentheses ()
E – Exponents **
M – Multiplication *
D – Division /
A – Addition +
S – Subtraction -
This rule helps us write expressions without confusion. Operations in parentheses are done
first, followed by exponents, then multiplication and division (left to right), and finally, addition
and subtraction (left to right).
The Precedence Hierarchy (Highest to Lowest)
If multiple operators are used in one line, Python executes them in this specific order:
Order Operator Description
1 () Parentheses (Always highest priority)
2 ** Exponentiation (Powers)
3 +x, -x, ~x Unary Plus, Unary Minus, Bitwise NOT
4 *, /, //, % Multiplication, Division, Floor Division, Modulus
5 +, - Addition, Subtraction
6 <<, >> Bitwise Shifts
7 & Bitwise AND
8 ^ Bitwise XOR
9 | Bitwise OR
10 ==, !=, >, <, >=, <= Comparisons (Relational)
11 not Logical NOT
12 and Logical AND
13 or Logical OR
Rule of Associativity (Left-to-Right)
What happens when two operators have the same precedence? For example: 10 / 2 * 5.
• Both / and * are at Level 4.
• In this case, Python uses left-to-right associativity.
• It calculates (10 / 2) first to get 5.0, then multiplies by 5 to get 25.0.
Exception: The Exponentiation (**) operator uses right-to-left associativity.
• 2 ** 3 ** 2 is evaluated as 2 ** (3 ** 2), which is 2 ** 9 = 512.
e.g.1.
v1 = (10 + 5) * 2
# 1. Parentheses first: (10 + 5) = 15
# 2. Multiplication next: 15 * 2 = 30
print(v1) # Output: 30
e.g. 2.
v1 = 5 + 2 * (3 ** 2)
# 1. calculate the exponent 3 ** 2 = 9.
# 2. multiply 2 * 9 = 18.
# 3. Finally, add 5 + 18 = 23 to get the result. Here, Parentheses help us decide the order clearly.
print(v1) # Output: 23
e.g. 3.
v1 = 10 > 5 and 3 == 3
# 1. Comparisons first: (10 > 5) is True, (3 == 3) is True
# 2. Logical 'and' next: True and True
print(v1) # Output: True
e.g. 4.
v1 = 5 + 3 - 1 # Evaluated as (5 + 3) - 1 → 8 - 1 = 7
# 1. In this example, we evaluate the expression from left to right.
# 2. Python adds 5 and 3 first, then subtracts 1, giving the final result of 7.
print(v1) # Output: 7
e.g. 5.
v1 = 3 ** 2 ** 1
# Evaluated as 3 ** (2 ** 1) → 3 ** 2 = 9
print(v1) # Output: 9