Operators in Python

Types of Operators in Python

If variables are containers that hold data, then operators are the tools we use to work with that data. They’re fundamental to programming.

An operator takes one or more values (called operands) and performs an operation on them, returning a result. Without operators, Python would just be a storage system—unable to do anything useful with the data.

Python has 24 different operators organized into four main categories:

TypeCountPurpose
Arithmetic7Mathematical calculations
Comparison6Compare values, return True/False
Logical3Combine conditions
Assignment8Assign and update values

Arithmetic Operators perform mathematical calculations on numbers. They work exactly as you’d expect from math class.

1. Addition (+) – Adds two numbers. Example: 5 + 3 = 8

2. Subtraction (-) – Subtracts second from first. Example: 10 – 3 = 7

3. Multiplication (*) – Multiplies numbers. Example: 10 * 3 = 30

4. Division (/) – Always returns float. Example: 10 / 2 = 5.0 (not 5!)

5. Floor Division (//) – Divides and rounds down. Example: 10 // 3 = 3

6. Modulo (%) – Returns remainder. Example: 10 % 3 = 1

7. Exponentiation (**) – Raises to power. Example: 2 ** 10 = 1024

KEY INSIGHT: Division (/) always returns a float in Python 3. Use // for integer results.

Comparison Operators answer yes-or-no questions. Every comparison returns True or False.

The Six Comparison Operators:

1. Equals (==) – 5 == 5 returns True

2. Not Equals (!=) – 5 != 3 returns True

3. Greater Than (>) – 10 > 5 returns True

4. Less Than (<) – 5 < 10 returns True

5. Greater or Equal (>=) – 5 >= 5 returns True

6. Less or Equal (<=) – 5 <= 10 returns True

Python lets you chain comparisons: 0 < x < 10 means ‘is x between 0 and 10?’

This is cleaner than (x > 0) and (x < 10).

Logical Operators combine conditions to create complex decision logic.

1. AND – Returns True if BOTH conditions are True

Analogy: Security checkpoint needing BOTH ID and ticket

2. OR – Returns True if AT LEAST ONE is True

Analogy: Cafe accepting EITHER cash OR credit card

3. NOT – Inverts boolean value

Analogy: True becomes False and vice versa

age = 25
has_license = True
is_insured = True
can_drive = age >= 18 and has_license and is_insured
print(can_drive)  # True

Assignment Operators assign and update variable values.

1. Simple Assignment (=) – Example: x = 10

2. Add and Assign (+=) – Example: x += 5 (same as x = x + 5)

3. Subtract and Assign (-=) – Example: x -= 3

4. Multiply and Assign (*=) – Example: x *= 2

5. Divide and Assign (/=) – Example: x /= 4

6. Floor Divide and Assign (//=) – Example: x //= 2

7. Modulo and Assign (%=) – Example: x %= 5

8. Power and Assign (**=) – Example: x **= 2

Readability: x += 5 is clearer than x = x + 5

Brevity: Less code to write

Efficiency: Slight performance improvement

CategoryOperatorsPurpose
Arithmetic (7)+, -, *, /, //, %, **Math operations
Comparison (6)==, !=, >, <, >=, <=True/False checks
Logical (3)and, or, notCombine conditions
Assignment (8)=, +=, -=, *=, /=, //=, %=, **=Assign/update values

1. Division Always Returns Float – In Python 3, 10/2 returns 5.0, not 5. Use // for integer results.

2. Comparison Chaining is Pythonic – Write 0 < x < 10 instead of (x > 0) and (x < 10).

3. Logical Operators Use Short-Circuiting – Python stops evaluating as soon as it knows the result.

4. Compound Assignment for Efficiency – Use x += 5 instead of x = x + 5 for cleaner code.

What’s Next?

When you write result = 2 + 3 * 4, does Python evaluate it as (2+3)*4 = 20 or as 2+(3*4) = 14?

The answer lies in Operator Precedence. This is covered in INTELEVO Episode 04: Expressions & Operator Precedence.

Practice Exercises:

  1. Calculate circle area using exponentiation (π * r²)
  2. Check if a number is within 10-20 range using comparison chaining
  3. Validate user voting eligibility (age >= 18 AND citizen)
  4. Calculate compound interest using compound assignment operators

Subscribe to intutiveturial and do watch Intelevo for more comprehensive tutorials.

Leave a Comment

Your email address will not be published. Required fields are marked *