Python Tutorial

Introduction

Edit Template
  • Home
  • /
  • Python Operators

Python Tutorial

Introduction

Edit Template
  • Home
  • /
  • Python Operators

Python Operators

In Python, operators are special symbols or keywords used to perform operations on variables and values. They are the building blocks of expressions and logic in programming.

Let’s go through each category of Python operators with unique examples to better understand how they work.

1. Arithmetic Operators

These operators are used for simple math operations.

				
					a = 10
b = 3

print(a + b)   # 13 (Addition)
print(a - b)   # 7 (Subtraction)
print(a * b)   # 30 (Multiplication)
print(a / b)   # 3.333... (Division)
print(a ** b)  # 1000 (Exponentiation)
print(a % b)   # 1 (Modulus - remainder)
print(a // b)  # 3 (Floor Division)

				
			

2. Assignment Operators

These allow you to assign and update variables at the same time.

				
					x = 5
x += 2    # Same as x = x + 2
x *= 3    # Same as x = x * 3
x %= 4    # Same as x = x % 4
print(x)  # Output will be the result of all the operations

				
			

Other assignment operators include: -=, /=, //=, **=, &=, |=, ^=, >>=, <<=

3. Bitwise Operators

Bitwise operators deal with binary numbers directly.

				
					a = 6      # 110 in binary
b = 3      # 011 in binary

print(a & b)  # 2 (AND)
print(a | b)  # 7 (OR)
print(~a)     # -7 (NOT)
print(a ^ b)  # 5 (XOR)
print(a << 1) # 12 (Left shift)
print(b >> 1) # 1 (Right shift)

				
			

4. Comparison Operators

Used to compare values and return True or False.

				
					a = 8
b = 12

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

				
			

5. Logical Operators

These operators combine multiple boolean expressions.

				
					x = 10
y = 20

print(x > 5 and y < 30)  # True
print(x > 15 or y > 15)  # True
print(not(x > 5))        # False

				
			

6. Identity Operators

They check if two variables point to the same object (memory reference).

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

print(a is b)       # True (same object)
print(a is not c)   # True (different objects even if values match)

				
			

7. Membership Operators

They check whether a value exists in a sequence like a list or string.

				
					fruits = ['apple', 'banana', 'mango']

print('banana' in fruits)     # True
print('cherry' not in fruits) # True

				
			

Operator Precedence (Order of Execution)

When writing expressions with multiple operators, Python follows a specific order:

From highest to lowest:

  • () – Parentheses

  • ** – Exponentiation

  • +x, -x, ~x – Unary operators

  • *, /, %, // – Multiplicative

  • +, - – Additive

  • <<, >> – Bitwise shifts

  • & – Bitwise AND

  • ^, | – Bitwise XOR and OR

  • Comparisons: <, <=, >, >=

  • Equality: ==, !=

  • Assignment: =, +=, *=, etc.

  • Identity: is, is not

  • Membership: in, not in

  • Logical: not, and, or

Final Thoughts

Python gives you a wide variety of operators to handle numbers, logic, comparisons, and more. Mastering operators will help you write efficient and clean code, especially when combining logic and calculations.

Keep experimenting with different operators and try combining them to build more complex expressions.

Scroll to Top