← Back to Home

Python Operators

By Instructor

Operators are used to perform operations on variables and values. Python provides 7 main categories of operators for mathematical, logical, and bitwise manipulations.

This comprehensive guide covers Arithmetic, Assignment, Comparison, Logical, Identity, Membership, and Bitwise operators with practical examples and comparison guides.

By Instructor8 chaptersPython 3.xBeginner to Advanced
operators_demo.py
x = 4
y = 2

print(f"Power: {x**y}, Floor Div: {x//y}")
python operators_demo.py → Power: 16, Floor Div: 2
Chapter 1

Overview & Precedence

Operators are special symbols used to perform operations on values and variables. Python categorizes operators into 7 distinct groups:

OrderCategoryPrimary Use / Function
1ArithmeticPerform mathematical calculations
2AssignmentAssign or update variable values
3ComparisonRelational checks returning True/False
4LogicalCombine multiple conditional expressions
5MembershipTest if a value exists inside a collection
6IdentityCheck if two variables reference the same memory object
7BitwisePerform bit-level operations on integers
Chapter 2

Arithmetic Operators

Arithmetic operators mathematical operations execute karte hain:

OperatorNameExampleDescription
+Addition4 + 2 → 6Adds values together
-Subtraction4 - 2 → 2Subtracts right value from left
*Multiplication4 * 2 → 8Multiplies values
/Division4 / 2 → 2.0Divides values (returns float)
//Floor Division4 // 2 → 2Divides and rounds down to whole int
%Modulus4 % 2 → 0Returns remainder of division
**Exponentiation4 ** 2 → 16Power calculation (base ** exponent)
arithmetic_examples.py
x = 4
y = 2

print(x + y)
print(x - y)
print(x * y)
print(x / y)
print(x // y)
print(x ** y)
expression_eval.py
x = 50
y = 30
b = 20
m = 60

print(x - y + b - m)
Chapter 3

Comparison Operators

Comparison (Relational) operators compare two values and return boolean True or False.

OperatorNameExampleBoolean Output
==Equal5 == 5True
!=Not Equal5 != 3True
>Greater Than5 > 2True
<Less Than5 < 2False
>=Greater Than or Equal5 >= 5True
<=Less Than or Equal5 <= 6True
comparison_demo.py
a = 20
b = 20

print(a == b)
print(a != b)
print(a > b)
print(a < b)
print(a >= b)
print(a <= b)
Chapter 4

Assignment Operators

Assignment operators variable ko value assign karne ya existing value ko update karne ke liye use hote hain.

OperatorEquivalent ExpressionExampleResult (if x = 10 initial)
=x = 10x = 1010
+=x = x + 5x += 515
-=x = x - 3x -= 37
*=x = x * 2x *= 220
/=x = x / 2x /= 25.0
%=x = x % 3x %= 31
//=x = x // 3x //= 33
**=x = x ** 3x **= 3 (initial 2)8
assignment_demo.py
x = 10
x += 5
print(x)

x %= 3
print(x)
Chapter 5

Logical Operators

Logical operators combine conditional statements:

  • and: Returns True if both conditions are True.
  • or: Returns True if at least one condition is True.
  • not: Reverse the boolean result (True becomes False, False becomes True).
logical_demo.py
a = 200
b = 33
c = 50

if a > b and c > a:
    print("Both conditions are True")
else:
    print("One or both conditions are False")

if a > b or a > c:
    print("At least one of the conditions is True")
else:
    print("One or both conditions are False")

if not a > b:
    print("a is NOT greater than b")
else:
    print("a is greater than b")
Chapter 6

Identity Operators & == vs is

Identity operators check whether two variables refer to the exact same object in computer memory space.

  • is — Returns True if both variables point to the same object.
  • is not — Returns True if variables point to different objects.
identity_demo.py
a = [1, 2, 3]
b = a
print(a is b)

c = [1, 2, 3]
print(a is not c)
Crucial Distinction: == vs is

== compares values (content equality).
is compares object identity (memory reference location).

compare_equals_and_is.py
a = [1, 2]
b = [1, 2]

print(a == b)
print(a is b)
Chapter 7

Membership Operators

Membership operators check whether a value exists inside a sequence or collection (such as strings, lists, tuples, or sets).

  • in — Returns True if value is present in collection.
  • not in — Returns True if value is NOT present in collection.
membership_list.py
names = ["Gagan", "Rajat", "Lokesh"]

print("Gagan" in names)
print("Ali" not in names)
membership_string.py
name = "Gagan"

print("G" in name)
print("z" not in name)
Chapter 8

Bitwise Operators

Bitwise operators work directly on the binary bit representations of integers.

OperatorNameExample (a=5 [101], b=3 [011])Binary ResultDecimal Output
&AND5 & 3101 & 011 → 0011
|OR5 | 3101 | 011 → 1117
^XOR5 ^ 3101 ^ 011 → 1106
~NOT~5-(5 + 1)-6
<<Left Shift5 << 1101010
>>Right Shift5 >> 10102
bitwise_demo.py
a = 5
b = 3

print(a & b)
print(a | b)
print(a ^ b)
print(~5)
print(5 << 1)
print(5 >> 1)