Python Operators

In Python, operators are special symbols used to perform operations on variables and values. You’ve already seen some, like + and *.

1. Types of Operators in Python

Python supports the following types of operators:

  • Arithmetic Operators
  • Assignment Operators
  • Comparison (Relational) Operators
  • Logical Operators
  • Identity Operators
  • Membership Operators
  • Bitwise Operators

2. Arithmetic Operators

Used for basic mathematical operations.

OperatorNameExampleOutput
+Addition5 + 38
-Subtraction5 - 32
*Multiplication5 * 315
/Division10 / 25.0
//Floor Division10 // 33
%Modulus10 % 31
**Exponent2 ** 38

Example:

a = 10
b = 3

print(a + b)  # 13
print(a - b)  # 7
print(a * b)  # 30
print(a / b)  # 3.333...
print(a // b) # 3
print(a % b)  # 1
print(a ** b) # 1000

3. Assignment Operators

Used to assign values to variables.

OperatorExampleEquivalent
=x = 5Assign 5 to x
+=x += 3x = x + 3
-=x -= 3x = x - 3
*=x *= 2x = x * 2
/=x /= 2x = x / 2
//=x //= 2Floor division
%=x %= 2Modulo assign
**=x **= 2Power assign

Example:

x = 10
x += 5
print(x)  # 15

x *= 2
print(x)  # 30

4. Comparison Operators

Used to compare two values. Returns True or False.

OperatorMeaningExample
==Equal tox == y
!=Not equal tox != y
>Greater thanx > y
<Less thanx < y
>=Greater or equalx >= y
<=Less or equalx <= y

Example:

x = 10
y = 5

print(x > y)   # True
print(x == y)  # False
print(x != y)  # True

5. Logical Operators

Used to combine multiple Boolean expressions.

OperatorDescriptionExample
andTrue if both are Truex > 5 and y < 10
orTrue if at least one Truex > 5 or y > 10
notReverses the resultnot(x > 5)

Example:

x = 10
y = 5
print(x > 5 and y < 10)  # True
print(x > 5 or y > 10)   # True
print(not(x > 5))        # False

6. Identity Operators

Used to compare memory locations of objects.

OperatorMeaningExample
isTrue if same objectx is y
is notTrue if different objectsx is not y

Example:

a = [1, 2]
b = a
c = [1, 2]
print(a is b)      # True
print(a is c)      # False
print(a is not c)  # True

7. Membership Operators

Used to check if a value is part of a sequence (string, list, tuple, etc.)

OperatorMeaningExample
inTrue if value exists"a" in "abc"
not inTrue if not exists4 not in [1,2,3]

Example:

print("H" in "Hello")     # True
print(3 not in [1, 2, 4]) # True

8. Bitwise Operators

Used to compare binary numbers (advanced).

OperatorNameExample
&ANDa & b
``OR
^XORa ^ b
~NOT~a
<<Left Shifta << 2
>>Right Shifta >> 2
a = 5      # 0101
b = 3      # 0011

print(a & b)  # 1
print(a | b)  # 7
print(a ^ b)  # 6
print(~a)     # -6

✅ Summary Table

CategoryOperators
Arithmetic+, -, *, /, %, //, **
Assignment=, +=, -=, *=, /=, etc.
Comparison==, !=, >, <, >=, <=
Logicaland, or, not
Identityis, is not
Membershipin, not in
Bitwise&, `