1. Introduction to Operators
An operator is a symbol that tells the compiler or interpreter to perform a specific mathematical, relational, or logical operation on one or more operands.
Python provides the following categories of operators:
| Category | Operators |
|---|---|
| Arithmetic | +, -, *, /, //, %, ** |
| Relational (Comparison) | ==, !=, >, <, >=, <= |
| Logical | and, or, not |
| Assignment | =, +=, -=, *=, /=, %=, //=, **= |
| Bitwise | &, |, ^, ~, <<, >> |
| Identity | is, is not |
| Membership | in, not in |
2. Arithmetic Operators
| Operator | Name | Example | Result |
|---|---|---|---|
| + | Addition | 10 + 3 | 13 |
| - | Subtraction | 10 - 3 | 7 |
| * | Multiplication | 10 * 3 | 30 |
| / | Division (float) | 10 / 3 | 3.333... |
| // | Floor Division | 10 // 3 | 3 |
| % | Modulus (remainder) | 10 % 3 | 1 |
| ** | Exponentiation | 10 ** 3 | 1000 |
a, b = 15, 4
print(a + b) # 19
print(a - b) # 11
print(a * b) # 60
print(a / b) # 3.75
print(a // b) # 3 (floor division)
print(a % b) # 3 (remainder)
print(a ** b) # 50625 (15^4)3. Relational (Comparison) Operators
Relational operators compare two values and return a boolean result (True or False).
| Operator | Meaning | Example | Result |
|---|---|---|---|
| == | 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 to | 5 >= 5 | True |
| <= | Less than or equal to | 5 <= 3 | False |
4. Logical Operators
Logical operators combine conditional statements.
| Operator | Description | Example | Result |
|---|---|---|---|
| and | True if both operands are True | True and False | False |
| or | True if at least one operand is True | True or False | True |
| not | Inverts the boolean value | not True | False |
marks = 85
attendance = 90
if marks >= 80 and attendance >= 75:
print("Eligible for exam")5. Assignment Operators
The = operator assigns a value to a variable.
x = 10 # Simple assignment
y = x + 5 # Expression assignment
a = b = c = 20 # Chained assignment
p, q, r = 1, 2, 3 # Multiple assignment6. Augmented Assignment Operators
These operators combine an arithmetic operation with assignment.
| Operator | Meaning | Example | Equivalent |
|---|---|---|---|
| += | Add and assign | x += 5 | x = x + 5 |
| -= | Subtract and assign | x -= 3 | x = x - 3 |
| *= | Multiply and assign | x *= 2 | x = x * 2 |
| /= | Divide and assign | x /= 4 | x = x / 4 |
| //= | Floor divide and assign | x //= 2 | x = x // 2 |
| %= | Modulus and assign | x %= 3 | x = x % 3 |
| **= | Exponent and assign | x **= 2 | x = x ** 2 |
7. Bitwise Operators
Bitwise operators perform operations on binary representations of integers.
| Operator | Name | Example (a=5=101, b=3=011) | Result |
|---|---|---|---|
| & | Bitwise AND | a & b | 1 (001) |
| | | Bitwise OR | a | b | 7 (111) |
| ^ | Bitwise XOR | a ^ b | 6 (110) |
| ~ | Bitwise NOT | ~a | -6 (inverts bits) |
| << | Left shift | a << 1 | 10 (1010) |
| >> | Right shift | a >> 1 | 2 (010) |
8. Identity Operators
Identity operators check whether two objects are the same object (same memory location).
a = [1, 2, 3]
b = [1, 2, 3]
c = a
print(a is c) # True (same object)
print(a is b) # False (different objects)
print(a is not b) # True
print(a == b) # True (same value)9. Membership Operators
Membership operators test whether a value is present in a sequence.
fruits = ["apple", "banana", "cherry"]
print("apple" in fruits) # True
print("grape" not in fruits) # True
text = "Hello, World!"
print("World" in text) # True10. Operator Precedence
Operator precedence determines the order in which operators are evaluated in an expression. Higher precedence operators are evaluated first.
| Precedence (High to Low) | Operators | Description |
|---|---|---|
| 1 | () | Parentheses (highest) |
| 2 | ** | Exponentiation |
| 3 | ~, +, - | Unary operators |
| 4 | *, /, //, % | Multiplication, Division, Modulus |
| 5 | +, - | Addition, Subtraction |
| 6 | <<, >> | Bitwise shift |
| 7 | & | Bitwise AND |
| 8 | ^ | Bitwise XOR |
| 9 | | | Bitwise OR |
| 10 | ==, !=, >, <, >=, <= | Comparison |
| 11 | not | Logical NOT |
| 12 | and | Logical AND |
| 13 | or | Logical OR (lowest) |
result = 5 + 3 * 2 ** 2 # 5 + 3 * 4 = 5 + 12 = 17
result2 = (5 + 3) * 2 ** 2 # 8 * 4 = 3211. Associativity
When operators have the same precedence, associativity determines the order of evaluation.
- Most operators have left-to-right associativity.
- Exponentiation (**) has right-to-left associativity.
# Left to right
print(10 - 5 - 2) # (10 - 5) - 2 = 3
print(10 / 5 * 2) # (10 / 5) * 2 = 4.0
# Right to left (exponentiation)
print(2 ** 3 ** 2) # 2 ** (3 ** 2) = 2 ** 9 = 51212. Expressions and Evaluation
An expression is a combination of values, variables, operators, and function calls that evaluates to a single value.
# Arithmetic expressions
area = 3.14 * radius ** 2
average = (a + b + c) / 3
# Boolean expressions
is_pass = marks >= 33
can_vote = age >= 18 and citizenship == "Indian"
# Mixed expressions
result = 10 + 20 * 3 - 5 / 2 # 10 + 60 - 2.5 = 67.513. Revision Questions and Answers
Very Short Answer Questions
1. Name any four types of operators in Python.
Arithmetic, Relational, Logical, Assignment.
2. What is the difference between = and ==?
= is assignment operator, == is equality comparison operator.
3. What does the // operator do?
Floor division - divides and returns the integer quotient.
4. Which operator is used for exponentiation?
** (double asterisk).
5. What is the result of 10 % 3?
1 (remainder when 10 is divided by 3).
Short Answer Questions
1. Explain operator precedence with an example.
Operator precedence determines evaluation order. Example: 5 + 3 * 2 = 11 because * has higher precedence than +. Parentheses override precedence: (5 + 3) * 2 = 16.
2. Differentiate between is and == operators.
== compares values (equality), while is compares object identity (memory location). Two different objects with the same value are == equal but not is equal.
Long Answer Questions
1. Write a Python program that takes three numbers as input and finds the largest using logical operators.
a = float(input("Enter first number: ")); b = float(input("Enter second: ")); c = float(input("Enter third: ")); if a >= b and a >= c: print("Largest:", a); elif b >= a and b >= c: print("Largest:", b); else: print("Largest:", c)
2. Explain all augmented assignment operators with examples.
Augmented operators combine arithmetic and assignment: += (x += 3 means x = x + 3), -=, *=, /=, //=, %=, **=. They provide a shorter way to modify and assign in one step.