Special Offer Diwali Special Bundle Offer: 3 courses for ₹8,999, save ₹9,001 (50% OFF)! Limited seats available. View Bundles →
0% Completed
Tutorials / Python Tutorial / Chapter 1: Getting Started & Language Basics
PYTHON ⏱ 10 min read

Operators in Python

Operators are symbols that perform arithmetic, logical, and structural operations on variables and values.

What is it?

Python includes Arithmetic, Comparison, Logical, Assignment, Identity, and Membership operators.

Why is it used?

Used to calculate figures, verify conditions, search collections, and check memory identities.

Detailed Explanation

Key Python Operators: • Arithmetic: `+`, `-`, `*`, `/` (float division), `//` (floor/integer division), `%` (modulo), `**` (exponentiation: `2**3 = 8`). • Comparison: `==`, `!=`, `>`, `<`, `>=`, `<=`. • Logical: `and`, `or`, `not` (written as plain English words, not `&&` or `||`!). • Membership: `in` and `not in` (checks if an item exists inside a string, list, or dictionary). • Identity: `is` and `is not` (checks if two variables refer to the exact same object in memory).

Syntax Structure

Syntax Blueprint
# Floor division and power
quotient = 17 // 3  # 5
power = 2 ** 4      # 16

# Membership
is_enrolled = "python" in course_list

Code Example

PYTHON Code
# Institute Admissions Math & Verification
base_course_price = 2500
bundle_price = 5397
num_courses = 3

# Arithmetic: Floor division & modulo
individual_cost = base_course_price * num_courses
savings = individual_cost - bundle_price
discount_percentage = (savings / individual_cost) * 100

print(f"Individual Total: ₹{individual_cost}")
print(f"Bundle Savings:   ₹{savings} ({discount_percentage:.1f}% OFF)")

# Membership Operator (in / not in)
available_languages = ["python", "javascript", "c", "cpp", "html", "css"]
query = "python"

if query in available_languages:
    print(f"Great news! {query.upper()} is available in our tutorial catalog.")

# Logical Operators (and, or, not)
age = 20
has_laptop = True
if age >= 16 and has_laptop:
    print("Eligible for Python Masterclass lab sessions.")

Explanation of the Code

• Demonstrates division, power, and formatting with f-strings. • Uses Python's clean English membership operator `in` to search the list. • Uses `and` for compound logical conditions.

Expected Output

Individual Total: ₹7500 Bundle Savings: ₹2103 (28.0% OFF) Great news! PYTHON is available in our tutorial catalog. Eligible for Python Masterclass lab sessions.

Real-World Application

The `in` operator is used everywhere in Python web applications to check permissions, validate form fields, and search query params.

Common Mistakes to Avoid

• Using single slash `/` when expecting an integer (in Python 3, `/` always produces a `float`). Use `//` for integer division. • Using `&&` or `||` (Python syntax uses `and` and `or`).

Quick Practice Challenge

Calculate 2 to the power of 10 using `**` and verify if the word 'Flask' is in 'Python Flask Backend'.

Key Takeaways

• Use `//` for floor division and `**` for exponentiation. • Python uses words `and`, `or`, `not` instead of symbols. • The `in` operator provides readable membership checks in lists and strings.