Python Operators Tutorial for Beginners | Arithmetic, Comparison & Logical Operators
Operators are one of the most essential building blocks of programming. They allow us to perform calculations, compare values, make decisions, manipulate data, and control the flow of a program. Whether you’re building a calculator, login system, shopping cart, or AI-powered application, operators play a vital role in processing information.
In this lesson, you’ll learn about Python’s different types of operators, including arithmetic, assignment, comparison, logical, membership, identity, and bitwise operators. Through real-world examples and practical coding exercises, you’ll understand how operators are used in software development and how they help create efficient and intelligent programs.
By the end of this lesson, you’ll be able to use Python operators confidently in your own programs.
Learning Objectives
After completing this lesson, you will be able to:
- Understand the purpose of operators in Python.
- Perform mathematical calculations using arithmetic operators.
- Compare values using comparison operators.
- Assign values efficiently using assignment operators.
- Combine conditions using logical operators.
- Check membership in sequences.
- Compare object identities.
- Understand basic bitwise operations.
- Apply operators in real-world Python programs.
Topics Covered
- What are Operators?
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Membership Operators
- Identity Operators
- Bitwise Operators
- Operator Precedence
- Best Practices
Detailed Lesson Content
Introduction to Operators
Imagine you’re developing an online shopping website. Every time a customer adds products to their cart, the application needs to:
- Add item prices
- Calculate discounts
- Compare stock quantities
- Check login status
- Verify user permissions
All these operations are performed using operators.
An operator is a symbol or keyword that performs an operation on one or more values (operands).
Example:
price = 500
tax = 90
total = price + tax
print(total)
Output:
590
Here:
+is an operator.priceandtaxare operands.
Types of Operators in Python
Python provides several categories of operators:
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Membership Operators
- Identity Operators
- Bitwise Operators
Let’s explore each one.
1. Arithmetic Operators
Arithmetic operators perform mathematical calculations.
| Operator | Meaning | Example |
|---|---|---|
| + | Addition | a + b |
| – | Subtraction | a – b |
| * | Multiplication | a * b |
| / | Division | a / b |
| // | Floor Division | a // b |
| % | Modulus | a % b |
| ** | Exponent | a ** b |
Addition
a = 20
b = 10
print(a + b)
Output
30
Subtraction
print(a - b)
Output
10
Multiplication
print(a * b)
Output
200
Division
print(a / b)
Output
2.0
Floor Division
Returns only the integer part.
print(17 // 5)
Output
3
Modulus
Returns the remainder.
print(17 % 5)
Output
2
Useful for:
- Even/Odd checking
- Pagination
- Cyclic operations
Exponent
print(2 ** 5)
Output
32
2. Assignment Operators
Assignment operators assign values to variables.
Basic assignment:
salary = 50000
Other assignment operators:
| Operator | Example |
|---|---|
| = | x = 5 |
| += | x += 5 |
| -= | x -= 5 |
| *= | x *= 5 |
| /= | x /= 5 |
| %= | x %= 5 |
Example:
marks = 80
marks += 10
print(marks)
Output
90
3. Comparison Operators
Comparison operators compare two values.
The result is always:
- True
- False
| Operator | Meaning |
|---|---|
| == | Equal |
| != | Not Equal |
| > | Greater Than |
| < | Less Than |
| >= | Greater Than or Equal |
| <= | Less Than or Equal |
Example:
age = 20
print(age >= 18)
Output
True
Comparison operators are commonly used in:
- Login systems
- Eligibility checks
- AI decision-making
- Validation
4. Logical Operators
Logical operators combine multiple conditions.
Python provides:
- and
- or
- not
Example:
age = 22
citizen = True
print(age >= 18 and citizen)
Output
True
OR Operator
print(True or False)
Output
True
NOT Operator
print(not True)
Output
False
Logical operators are heavily used in authentication systems and access control.
5. Membership Operators
Membership operators check whether a value exists in a sequence.
Operators:
- in
- not in
Example:
courses = ["Python","Java","React"]
print("Python" in courses)
Output
True
Another example:
print("PHP" not in courses)
Output
True
6. Identity Operators
Identity operators compare whether two variables refer to the same object in memory.
Operators:
- is
- is not
Example:
a = 100
b = 100
print(a is b)
Output
True
Example:
print(a is not b)
Output
False
Identity operators are useful when checking for objects such as None.
user = None
print(user is None)
7. Bitwise Operators
Bitwise operators perform operations on binary numbers.
Common operators:
- &
- |
- ^
- ~
- <<
Example:
a = 5
b = 3
print(a & b)
Although bitwise operators are less common in everyday web development, they are widely used in:
- Networking
- Encryption
- Embedded Systems
- Performance Optimization
Operator Precedence
Python follows a specific order when evaluating expressions.
Example:
result = 5 + 3 * 2
print(result)
Output
11
Multiplication happens before addition.
Use parentheses to change the order.
result = (5 + 3) * 2
Output
16
Real-World Example
Let’s calculate the final bill in an online shopping application.
product_price = 1200
quantity = 2
discount = 200
subtotal = product_price * quantity
final_amount = subtotal - discount
print("Subtotal:", subtotal)
print("Final Amount:", final_amount)
print("Eligible for Free Shipping:", final_amount >= 2000)
Output
Subtotal: 2400
Final Amount: 2200
Eligible for Free Shipping: True
This example combines arithmetic, assignment, and comparison operators in a practical scenario.
Best Practices
- Use parentheses to improve readability.
- Avoid overly complex expressions.
- Use logical operators carefully.
- Prefer meaningful variable names.
- Understand operator precedence to avoid bugs.
- Test expressions with different input values.
Key Takeaways
- Operators perform operations on data.
- Arithmetic operators handle mathematical calculations.
- Assignment operators update variable values.
- Comparison operators return
TrueorFalse. - Logical operators combine conditions.
- Membership operators check if values exist in collections.
- Identity operators compare object references.
- Bitwise operators work with binary data.
- Operator precedence determines the order of evaluation.



