Python Operators
Introduction
Operators are special symbols or keywords used to perform operations on variables and values in Python.
For example:
a = 10
b = 5
print(a + b)
Output:
15
Here, + is an arithmetic operator that adds two numbers.
Python provides several categories of operators:
- Arithmetic Operators
- Relational (Comparison) Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Membership Operators
Let’s understand each type with examples.
1. Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations.
| Operator | Description | Example |
|---|---|---|
+ | Addition | 10 + 5 = 15 |
- | Subtraction | 10 - 5 = 5 |
* | Multiplication | 10 * 5 = 50 |
/ | Division | 10 / 5 = 2.0 |
// | Floor Division | 10 // 3 = 3 |
% | Modulus (remainder) | 10 % 3 = 1 |
** | Exponentiation | 2 ** 3 = 8 |
Example
a = 10
b = 3
print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(a // b)
print(a % b)
print(a ** b)
Output:
13
7
30
3.3333333333333335
3
1
1000
2. Relational (Comparison) Operators
Relational operators compare two values and return a Boolean result: True or False.
| Operator | Description | Example |
|---|---|---|
== | Equal to | 10 == 10 |
!= | Not equal to | 10 != 5 |
> | Greater than | 10 > 5 |
< | Less than | 5 < 10 |
>= | Greater than or equal to | 10 >= 10 |
<= | Less than or equal to | 5 <= 10 |
Example
a = 10
b = 5
print(a == b)
print(a != b)
print(a > b)
print(a < b)
print(a >= b)
print(a <= b)
Output:
False
True
True
False
True
False
3. Logical Operators
Logical operators are used to combine or modify Boolean conditions.
Python provides three logical operators:
| Operator | Description |
|---|---|
and | Returns True when both conditions are True |
or | Returns True when at least one condition is True |
not | Reverses the Boolean result |
Example: and Operator
a = 10
b = 5
print(a > 5 and b < 10)
Output:
True
Both conditions are True, so the result is True.
Example: or Operator
a = 10
b = 5
print(a < 5 or b < 10)
Output:
True
At least one condition is True.
Example: not Operator
a = True
print(not a)
Output:
False
The not operator reverses the Boolean value.
4. Bitwise Operators
Bitwise operators perform operations on the binary representation of integers.
| Operator | Name | Description |
|---|---|---|
& | AND | Sets a bit to 1 when both bits are 1 |
^ | XOR | Sets a bit to 1 when the bits are different |
| | | OR | Sets a bit to 1 when at least one bit is 1 |
~ | NOT | Inverts the bits |
<< | Left Shift | Shifts bits to the left |
>> | Right Shift | Shifts bits to the right |
Example
a = 5
b = 3
print(a & b)
print(a | b)
print(a ^ b)
print(~a)
print(a << 1)
print(a >> 1)
Output:
1
7
6
-6
10
2
Explanation
The binary representation of the numbers is:
5 = 0101
3 = 0011
For the AND operation:
0101
& 0011
------
0001
The binary result 0001 is equal to decimal 1.
5. Assignment Operators
Assignment operators are used to assign values to variables and update their values.
| Operator | Example | Equivalent Expression |
|---|---|---|
= | a = 10 | Assigns 10 to a |
+= | a += 5 | a = a + 5 |
-= | a -= 5 | a = a - 5 |
*= | a *= 5 | a = a * 5 |
/= | a /= 5 | a = a / 5 |
//= | a //= 5 | a = a // 5 |
%= | a %= 5 | a = a % 5 |
**= | a **= 2 | a = a ** 2 |
Example
a = 10
a += 5
print(a)
a -= 3
print(a)
a *= 2
print(a)
Output:
15
12
24
6. Membership Operators
Membership operators check whether a value exists in a sequence, such as a string, list, or tuple.
Python provides two membership operators:
in: Returns True if the value exists in the sequence.not in: Returns True if the value does not exist in the sequence.
Example: in Operator
numbers = [10, 20, 30, 40]
print(20 in numbers)
print(50 in numbers)
Output:
True
False
Example: not in Operator
fruits = ["apple", "banana", "mango"]
print("orange" not in fruits)
print("apple" not in fruits)
Output:
True
False
7. Difference Between Python Operators
| Operator Category | Main Purpose |
|---|---|
| Arithmetic | Performs mathematical calculations |
| Relational | Compares two values |
| Logical | Combines or reverses conditions |
| Bitwise | Performs operations on binary bits |
| Assignment | Assigns or updates variable values |
| Membership | Checks whether a value exists in a sequence |
8. Practice Questions (MCQs)
Q1. Which operator is used for exponentiation in Python?
A. //
B. **
C. %
D. *
Correct Answer: B. **
Q2. What is the output of 10 // 3?
A. 3.33
B. 1
C. 3
D. 10
Correct Answer: C. 3
Q3. Which operator checks whether two values are equal?
A. =
B. !=
C. ==
D. >=
Correct Answer: C. ==
Q4. Which logical operator returns True only when both conditions are True?
A. or
B. not
C. and
D. in
Correct Answer: C. and
Q5. What is the output of the following code?
print(5 & 3)
A. 7
B. 6
C. 1
D. 0
Correct Answer: C. 1
Q6. Which operator checks whether an element exists in a list?
A. is
B. in
C. ==
D. not
Correct Answer: B. in
Q7. What is the output of the following code?
a = 10
a += 5
print(a)
A. 10
B. 5
C. 15
D. 50
Correct Answer: C. 15