OPERATORS
Python operators are special symbols used to perform operations on variables and values.
Below is a clear explanation of all major operator types with examples.
Python Operators with Examples
Python operators are grouped into these categories:
1. Arithmetic Operators
2. Assignment Operators
3. Comparison Operators
4. Logical Operators
5. Bitwise Operators
6. Membership Operators
7. Identity Operators
1. Arithmetic Operators
Arithmetic operators are symbols used in mathematics and programming to perform
fundamental operations, addition (+), subtraction (-), multiplication (*), division (/), modulus
(%), and exponentiation (**), on numerical values (operands) and returning a numeric result.
Operator Meaning Example
+ Addition a+b
- Subtraction a–b
* Multiplication a*b
/ Division (float) a/b
// Floor Division (integer division) a // b
% Modulus (remainder) a%b
** Exponentiation a ** b
Example:
# Arithmetic Opertors in Python
a = int(input("Enter First Number: "))
b = int(input("Enter Second Number: "))
print("a = ", a)
print("b = ", b)
print("\nArithmetic Operators:")
print("Addition (a + b) =", a + b)
print("Subtraction (a - b) =", a - b)
print("Multiplication (a * b) =", a * b)
print("Division (a / b) =", a / b)
print("Floor Division (a // b) =", a // b)
print("Modulus (a % b) =", a % b)
print("Exponent (a ** b) =", a ** b)
Output:
D:\Program>python [Link]
Enter First Number: 15
Enter Second Number: 4
a = 15
b= 4
Arithmetic Operators:
Addition (a + b) = 19
Subtraction (a - b) = 11
Multiplication (a * b) = 60
Division (a / b) = 3.75
Floor Division (a // b) = 3
Modulus (a % b) = 3
Exponent (a ** b) = 50625
2. Assignment Operators
Assignment Operators are used to assign values to variables.
Step-by-step process:
Evaluate the right-hand side expression
Apply the assignment operator
Store the result in the variable on the left-hand side
Operator Meaning Example
= Assign value a=5
+= Add and assign a += 3 → a = a + 3
-= Subtract and assign a -= 2 → a = a - 2
*= Multiply and assign a *= 4 → a = a * 4
/= Divide and assign a /= 2 → a = a / 2
//= Floor divide and assign a //= 3
%= Modulus and assign a %= 3
**= Exponent and assign a **= 2
Example:
# Assignment Operators in Python
a = int(input("Enter a Number: "))
print("\nInitial value of a = ", a)
a += 4
print("a += 4:", a)
a -= 2
print("a -= 2:", a)
a *= 3
print("a *= 3:", a)
a /= 4
print("a /= 4:", a)
a //= 2
print("a //= 2:", a)
a %= 3
print("a %= 3:", a)
a **= 3
print("a **= 3:", a)
Output:
D:\Program>python [Link]
Enter a Number: 13
Initial value of a = 13
a += 4: 17
a -= 2: 15
a *= 3: 45
a /= 4: 11.25
a //= 2: 5.0
a %= 3: 2.0
a **= 3: 8.0
3. Comparison Operators
Comparison Operators are used to two compare values and return a Boolean result (True or
False).
Operator Meaning Example
== Equal to a == b
!= Not equal to a != b
> Greater than a>b
< Less than a<b
>= Greater than or equal to a >= b
<= Less than or equal to a <= b
Example:
# Comparison Operators in Python
a = int(input("Enter the first value: "))
b = int(input("Enter the second value: "))
print("a =", a)
print("b =", b)
print("\nComparison Results: ")
print("a == b : ", a == b)
print("a != b : ", a != b)
print("a > b : ", a > b)
print("a < b : ", a < b)
print("a >= b : ", a >= b)
print("a <= b : ", a <= b)
Output:
D:\Program>python [Link]
Enter the first value: 10
Enter the second value: 20
a = 10
b = 20
Comparison Results:
a == b : False
a != b : True
a > b : False
a < b : True
a >= b : False
a <= b : True
4. Logical Operators
Logical operators are used to combine conditional statements and evaluate Boolean values
(True or False).
Python has three logical operators:
Operator Meaning Example
And True if both conditions are true x > 5 and x < 10
Or True if any one condition is true x > 10 or x == 5
Not Reverses the condition not(x > 5)
Example:
# Logical Operators in Python
a = int(input("Enter the first value: "))
b = int(input("Enter the second value: "))
print("a =", a)
print("b =", b)
print("\nLogical Operator Results: ")
# AND Operator
print("a > 5 and b < 5 :", a > 5 and b < 5)
# OR Operator
print("a < 5 or b < 5 :", a < 5 or b < 5)
# NOT Operator
print("not(a == 7) :", not(a == 7))
print("not(b > 5) :", not(b > 5))
Output:
D:\Program>python [Link]
Enter the first value: 7
Enter the second value: 2
a=7
b=2
Logical Operator Results:
a > 5 and b < 5 : True
a < 5 or b < 5 : True
not(a == 7) : False
not(b > 5) : True
5. Bitwise Operators
Python bitwise operators are used to perform bitwise calculations on integers. The integers are
first converted into binary and then operations are performed on each bit or corresponding pair
of bits, hence the name bitwise operators.
Operator Name Meaning
& Bitwise AND 1 if both bits are 1
| Bitwise OR 1 if any one or both bits are 1
^ Bitwise XOR 1 if bits are different
~ Bitwise NOT Flips bits (two’s complement)
Shifts bits left (×2 (every time you shift left by 1, the number becomes
<< Left Shift
double (×2) i.e., 10 become 20) for each shift)
Shifts bits right (÷2 (every time you shift right by 1, the number
>> Right Shift
becomes half (÷2) i.e., 10 become 5) for each shift)
Example:
# Bitwise Operators in Python
a = 10 # binary: 1010
b = 4 # binary: 0100
print("a =", a, "binary =", bin(a))
print("b =", b, "binary =", bin(b))
# Bitwise AND
print("\na & b =", a & b)
# Bitwise OR
print("a | b =", a | b)
# Bitwise XOR
print("a ^ b =", a ^ b)
# Bitwise NOT
print("~a =", ~a)
# Left Shift
# Original: 1010, Shift left: 10100, No bits enter from the left; they just move.
# 1×16 + 0×8 + 1×4 + 0×2 + 0×1 = 16 + 4 = 20
print("a << 1 =", a << 1)
# Right Shift
# Original: 1010, Shift right: 0101, The last bit 0 is removed; A new 0 is placed on the left
# 0×8 + 1×4 + 0×2 + 1×1 = 4 + 1 = 5
print("a >> 1 =", a >> 1)
Output:
D:\Program>python [Link]
a = 10 binary = 0b1010
b = 6 binary = 0b110
a&b=2
a | b = 14
a ^ b = 12
~a = -11
a << 1 = 20
a >> 1 = 5
6. Membership Operators
Membership operators are used to test whether a value is present in a sequence (like list, string,
tuple, set, dictionary).
Operator Meaning Example
In Returns True if a value is present "a" in "apple"
not in Returns True if a value is not present 5 not in [1,2,3]
Example:
# Membership Operators in Python
fruits = ["apple", "banana", "mango"]
print("Fruits list: ", fruits)
# Using "in"
print("\n'apple' in fruits ->", "apple" in fruits)
print("'graph' in fruits ->", "graph" in fruits)
# Using "not in"
print("'orange' not in fruits ->", "orange" not in fruits)
print("'banana' not in fruits ->", "banana" not in fruits)
Output:
D:\Program>python [Link]
Fruits list: ['apple', 'banana', 'mango']
'apple' in fruits -> True
'graph' in fruits -> False
'orange' not in fruits -> True
'banana' not in fruits -> False
7. Identity Operators
In Python, the identity operator is used to check whether two variables refer to the same object
in memory, not whether their values are equal.
Operator Meaning Example
Is True if both variables refer to the same object a is b
is not True if variables refer to different objects a is not b
Example:
# Identity Operator in Python
a = [1, 2]
b = a # 'b' refers to the same object as 'a'
c = [1, 2]
print(a is b) # True (same object)
print(a is c) # False (different objects)
Output:
D:\Program>python [Link]
True
False