Here is a complete, clear, and exam-oriented explanation of Operators in Python, suitable for BCA/MCA/B.Tech students.
Operators in Python
Operators are special symbols used to perform operations on variables and values.
Python supports several types of operators for arithmetic, comparison, logic, assignment, and more.
An operand is a value on which the operator acts:
a = 10 # a is operand
b = 5 # b is operand
Types of Operators in Python
Python provides the following major categories:
- Arithmetic Operators
- Relational (Comparison) Operators
- Logical Operators
- Assignment Operators
- Bitwise Operators
- Membership Operators
- Identity Operators
Let’s discuss each in detail with examples.
1. Arithmetic Operators
Used for basic mathematical operations.
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ | Addition | 10 + 5 | 15 |
- | Subtraction | 10 - 5 | 5 |
* | Multiplication | 10 * 5 | 50 |
/ | Division | 10 / 5 | 2.0 |
% | Modulus (remainder) | 10 % 3 | 1 |
** | Exponent | 2 ** 3 | 8 |
// | Floor division | 10 // 3 | 3 |
Example:
a = 10
b = 3
print(a + b)
print(a / b)
print(a // b)
2. Relational (Comparison) Operators
Used to compare two values. They return True or False.
| Operator | Meaning |
|---|---|
== | Equal to |
!= | Not equal to |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
Example:
print(10 > 5) # True
print(10 == 5) # False
3. Logical Operators
Used to combine conditional statements.
| Operator | Meaning |
|---|---|
and | True if both are True |
or | True if at least one is True |
not | Reverses boolean |
Example:
x = 10
print(x > 5 and x < 20) # True
print(x > 15 or x == 10) # True
print(not(x == 10)) # False
4. Assignment Operators
Used to assign values to variables.
Also used for shorthand operations.
| Operator | Meaning | Example |
|---|---|---|
= | Assign | a = 5 |
+= | Add and assign | a += 3 โ a = a + 3 |
-= | Subtract and assign | a -= 3 |
*= | Multiply and assign | a *= 3 |
/= | Divide and assign | a /= 2 |
//= | Floor divide and assign | a //= 2 |
%= | Modulus and assign | a %= 5 |
**= | Power and assign | a **= 2 |
Example:
a = 10
a += 5 # a = 15
5. Bitwise Operators
Work at the binary (bit) level.
| Operator | Meaning |
|---|---|
& | Bitwise AND |
| ` | ` |
^ | Bitwise XOR |
~ | Bitwise NOT |
<< | Left shift |
>> | Right shift |
Example:
a = 5 # 0101
b = 3 # 0011
print(a & b) # 1 โ 0001
print(a | b) # 7 โ 0111
6. Membership Operators
Used to test whether a value is in a sequence (string, list, tuple, etc.).
| Operator | Meaning |
|---|---|
in | True if value is found |
not in | True if value is not found |
Example:
nums = [1, 2, 3]
print(2 in nums) # True
print(5 not in nums) # True
7. Identity Operators
Used to compare memory locations of two objects.
| Operator | Meaning |
|---|---|
is | True if both variables refer to the same object |
is not | True if they do not refer to the same object |
Example:
a = [1, 2, 3]
b = a
c = [1, 2, 3]
print(a is b) # True (b refers to same object)
print(a is c) # False (different objects with same value)
8. Operator Precedence (Order of Evaluation)
Python evaluates operators in a specific order:
- Parentheses
() - Exponent
** - Unary operators
+x,-x,~x - Multiplication, Division, Modulus, Floor division
*, /, %, // - Addition, Subtraction
+, - - Bitwise shifts
<<,>> - Bitwise AND, OR, XOR
- Comparison operators
- Logical NOT
- Logical AND
- Logical OR
- Assignment operators
9. Examples Combining Operators
result = 10 + 5 * 2
# multiplication first: 5 * 2 = 10
# then addition: 10 + 10 = 20
print(result)
Summary
| Category | Examples |
|---|---|
| Arithmetic | +, -, *, /, %, **, // |
| Comparison | ==, !=, >, <, >=, <= |
| Logical | and, or, not |
| Assignment | =, +=, -=, *=, etc. |
| Bitwise | &, |
| Membership | in, not in |
| Identity | is, is not |
