Back to Python Notes
Topic #198

Operators in Python

By the end of this lesson, you will be able to use Python's core operators to perform calculations, make decisions, and manipulate data efficiently.

What it is

Operators are special symbols or keywords that tell the Python interpreter to perform specific mathematical or logical manipulations. They act on values (operands) to produce a new value. The main categories include arithmetic for math, comparison for checking relationships, logical for combining conditions, assignment for storing values, membership for checking existence in collections, and bitwise for low-level binary operations.

Why it matters

  • Data Processing: Arithmetic operators allow you to calculate totals, averages, and transformations.
  • Control Flow: Comparison and logical operators drive if statements and loops, enabling decision-making.
  • Efficiency: Membership operators (in) provide a clean way to check lists or strings without manual iteration.
  • State Management: Assignment operators update variables concisely, reducing code verbosity.

Syntax or steps

Python follows standard precedence rules: parentheses first, then exponentiation, multiplication/division, addition/subtraction, comparisons, and finally logical operators. Assignment happens last. For example, x += 1 is shorthand for x = x + 1.

Example

# Arithmetic
a = 10
b = 3
print(a + b)   # Addition: 13
print(a % b)   # Modulus (remainder): 1
print(a // b)  # Floor Division: 3

# Comparison & Logical
x = 5
y = 10
is_greater = x > y       # False
is_equal = x == 5        # True
combined = (x > 0) and (y < 20) # True

# Membership
fruits = ["apple", "banana"]
has_apple = "apple" in fruits    # True

# Bitwise
binary_val = 5 & 3  # Binary 101 AND 011 results in 001 (1)

This snippet demonstrates how different operator types interact with basic data. Note that // performs integer division, discarding the remainder, while % returns only the remainder. The and keyword requires both sides to be true, whereas & operates on individual bits.

Common mistakes

  • Confusing = and ==: Using a single equals sign inside an if statement causes a syntax error because it attempts assignment rather than comparison.
  • Integer Division Expectations: In Python 3, / always returns a float. Use // if you strictly need an integer result.
  • Misusing is vs ==: == checks value equality, while is checks object identity (memory address). Use == for comparing numbers or strings.
  • Precedence Errors: Neglecting parentheses can lead to unexpected results when mixing arithmetic and bitwise operators. Always group complex expressions explicitly.

When to use it

Operator TypeUse CaseAlternative/Note
Arithmetic (+ - * /)Mathematical calculationsUse math module for advanced functions like square roots.
Logical (and or not)Combining boolean conditionsAvoid using bitwise & | for booleans unless performance-critical.
Membership (in)Checking list/string contentsFaster and more readable than writing a loop.

Practice

Guided Exercise: Write a script that takes two integers, calculates their sum and product, and prints whether the sum is greater than the product.

Challenge: Create a list of numbers [1, 2, 3, 4, 5]. Use membership operators to check if 3 is present, and use bitwise AND to find which numbers share the least significant bit with 1.

Hint: Remember that num & 1 checks if a number is odd.

Quick check

Question: What is the output of print(7 // 2)?

Answer: 3. The floor division operator rounds down to the nearest whole number.

Summary

Python operators provide the fundamental building blocks for computation and logic. Mastering the distinction between arithmetic, logical, and bitwise operations ensures your code is both correct and efficient. Always prioritize readability by using explicit parentheses when combining multiple operator types.

Want to go beyond the notes?

Join Coding Now Tech Institute's Python course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Operators in Python – FAQs

Quick answers about learning Operators in Python in Python.

This free note from Coding Now Tech Institute explains Operators in Python in Python — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Python topic on Coding Now Tech Institute, including Operators in Python, is 100% free with no signup required.
With focused practice, most students grasp Operators in Python in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now