Python Operators

Special symbols or keywords are used in Python to perform operations on variables and values. Different types of operators are supported in Python, and each type has a different function.

Let’s go through them one by one:

1. Arithmetic Operators

These operators are used to perform mathematical operations.

OperatorDescriptionExample
+Addition5 + 3 = 8
-Subtraction5 - 3 = 2
*Multiplication5 * 3 = 15
/Division (float)5 / 2 = 2.5
//Floor Division5 // 2 = 2
%Modulus (remainder)5 % 2 = 1
**Exponentiation2 ** 3 = 8

Example:

a = 10
b = 3
print(a + b) # Addition
print(a - b) # Subtraction
print(a * b) # Multiplication
print(a / b) # Division
print(a // b) # Floor Division
print(a % b) # Modulus
print(a ** b) # Exponentiation

2. Comparison (Relational) Operators

These operators compare values and return a boolean (True or False).

OperatorDescriptionExample
==Equal to5 == 3 → False
!=Not equal to5 != 3 → True
>Greater than5 > 3 → True
<Less than5 < 3 → False
>=Greater than or equal to5 >= 3 → True
<=Less than or equal to5 <= 3 → False

Example:

a = 10
b = 5
print(a > b) # True
print(a < b) # False
print(a == b) # False
print(a != b) # True
print(a >= b) # True
print(a <= b) # False

3. Logical Operators

These are used to combine conditional statements.

OperatorDescriptionExample
andLogical AND(5 > 3) and (3 > 1) → True
orLogical OR(5 > 3) or (3 < 1) → True
notLogical NOTnot(5 > 3) → False

Example:

x = 5
y = 3
z = 8

print((x > y) and (z > x)) # True
print((x > y) or (z < x)) # True
print(not(x > y)) # False

4. Assignment Operators

Used to assign values to variables, and combine assignment with arithmetic.

OperatorExampleEquivalent To
=x = 5Assign value 5
+=x += 3x = x + 3
-=x -= 3x = x - 3
*=x *= 3x = x * 3
/=x /= 3x = x / 3
//=x //= 3x = x // 3
%=x %= 3x = x % 3
**=x **= 3x = x ** 3

Example:

x = 5
x += 3 # Equivalent to x = x + 3
print(x) # 8

x *= 2 # Equivalent to x = x * 2
print(x) # 16

5. Bitwise Operators

Operate on binary representations of integers.

OperatorDescriptionExample
&Bitwise AND5 & 3 → 1
``Bitwise OR
^Bitwise XOR5 ^ 3 → 6
~Bitwise NOT~5 → -6
<<Left Shift5 << 1 → 10
>>Right Shift5 >> 1 → 2

Example:

a = 5 # Binary: 0101
b = 3 # Binary: 0011

print(a & b) # AND → 0001 → 1
print(a | b) # OR → 0111 → 7
print(a ^ b) # XOR → 0110 → 6
print(~a) # NOT → -(5+1) → -6
print(a << 1) # Left Shift → 1010 → 10
print(a >> 1) # Right Shift → 0010 → 2

6. Membership Operators

These test whether a value is a member of a sequence.

OperatorDescriptionExample
inReturns True if present'a' in 'apple' → True
not inReturns True if not present'b' in 'apple' → False

Example:

text = "hello"
print('h' in text) # True
print('z' not in text) # True

7. Identity Operators

These check if two objects share the same memory location.

OperatorDescriptionExample
isTrue if identical objectsx is y
is notTrue if not identicalx is not y

Example:

a = [1, 2, 3]
b = a
c = [1, 2, 3]

print(a is b) # True (b points to the same object as a)
print(a is c) # False (different objects with same content)
print(a == c) # True (contents are the same)

8. Special Operators

  • None is used for null values.
  • Ternary operator for conditional assignment:
x = 10
result = "Even" if x % 2 == 0 else "Odd"
print(result) # Output: Even