← Previous

Lesson 3 · Python

Operators in Python

~35 min

1. What are Operators?

Operators are symbols that allow Python to perform operations on values and variables. Think of operators as tools that tell Python what action to perform.

For example:

x = 10
y = 5

print(x + y)

Here, the + symbol is an operator. It tells Python to add the values together.

Operators are everywhere in programming — calculations, comparisons, checking conditions, and making decisions all depend on operators.

2. Arithmetic Operators

Arithmetic operators are used for mathematical calculations.

  • + → Addition
  • - → Subtraction
  • * → Multiplication
  • / → Division
  • % → Modulus (remainder)
  • // → Floor division
  • ** → Exponent (power)
a = 10
b = 3

print(a + b)   # 13
print(a - b)   # 7
print(a * b)   # 30
print(a / b)   # 3.3333
print(a % b)   # 1
print(a // b)  # 3
print(a ** b)  # 1000

Why it matters: Arithmetic operators are used in apps, games, calculators, banking systems, and almost every software system.

Mini Practice: Create two variables called num1 and num2. Perform all arithmetic operations on them.

3. Assignment Operators

Assignment operators are used to assign values to variables.

score = 10

score += 5
print(score)   # 15

score -= 3
print(score)   # 12

score *= 2
print(score)   # 24

Common assignment operators:

  • = → Assign value
  • += → Add and assign
  • -= → Subtract and assign
  • *= → Multiply and assign
  • /= → Divide and assign

Why it matters: These operators make your code shorter and cleaner.

4. Comparison Operators

Comparison operators compare two values and return either True or False.

age = 18

print(age == 18)  # True
print(age != 20)  # True
print(age > 15)   # True
print(age < 30)   # True
print(age >= 18)  # True
print(age <= 17)  # False

Common comparison operators:

  • == → Equal to
  • != → Not equal to
  • > → Greater than
  • < → Less than
  • >= → Greater than or equal to
  • <= → Less than or equal to

Why it matters: Comparison operators are used to make decisions in programs. For example, checking if someone is old enough to sign up.

5. Logical Operators

Logical operators are used to combine multiple conditions.

age = 20
has_id = True

print(age >= 18 and has_id)
print(age >= 18 or has_id)
print(not has_id)
  • and → Both conditions must be True
  • or → At least one condition must be True
  • not → Reverses the result

Why it matters: Logical operators help programs make smarter decisions.

6. Membership Operators

Membership operators check if something exists inside a collection.

name = "Infotris"

print("Learn" in name)
print("Python" not in name)
  • in → Exists
  • not in → Does not exist

7. Identity Operators

Identity operators compare memory locations.

x = [1, 2]
y = x
z = [1, 2]

print(x is y)      # True
print(x is z)      # False
print(x is not z)  # True

Key takeaway: Use is when checking identity, and == when checking values.

8. Common Beginner Mistakes

⚠️ Watch out for these mistakes:

  • Confusing = and ==: = assigns a value, while == compares values.
  • Using / instead of //: Single slash gives a float, double slash removes decimals.
  • Confusing is with ==: is checks identity, not value equality.

3. Comparison Operators

Comparison operators compare two values and return either True or False. These are called Boolean values.

Operator Meaning Example Result
== Equal to 5 == 5 True
!= Not equal to 10 != 5 True
> Greater than 8 > 3 True
< Less than 2 < 10 True
>= Greater than or equal 10 >= 10 True
<= Less than or equal 5 <= 10 True

Example:

age = 18

print(age >= 18)   # True
print(age < 18)    # False
print(age == 18)   # True
print(age != 20)   # True

🧠 Mini Practice

Create a variable called marks and assign it any number. Print whether it is greater than 50.

4. Logical Operators

Logical operators are used when you want to combine multiple conditions together.

Python has three logical operators:

  • and → Returns True only if BOTH conditions are true.
  • or → Returns True if ANY one condition is true.
  • not → Reverses the result.

4.1 The and Operator

age = 20
has_id = True

print(age >= 18 and has_id)
# True

Explanation: Both conditions are true, so Python returns True.

4.2 The or Operator

is_weekend = False
is_holiday = True

print(is_weekend or is_holiday)
# True

Explanation: One condition is true, so the result becomes True.

4.3 The not Operator

is_logged_in = False

print(not is_logged_in)
# True

Explanation: not flips the value.

🧠 Mini Practice

Create variables called is_student and has_discount. Use and to check if both are true.

5. Assignment Operators

Assignment operators help update variable values quickly.

Operator Meaning Example
+= Add and assign x += 5
-= Subtract and assign x -= 5
*= Multiply and assign x *= 2
/= Divide and assign x /= 2

Example:

coins = 10

coins += 5
print(coins)   # 15

coins -= 2
print(coins)   # 13

Why use this? It makes code cleaner and shorter.

6. Common Beginner Mistakes

⚠️ Watch out for these mistakes:

  • Using = instead of ==
    Wrong: if age = 18
    Correct: if age == 18
  • Dividing expecting an integer
    10 / 2 gives 5.0, not 5. Use // for whole numbers.
  • Confusing % (modulus)
    10 % 3 returns remainder → 1
Interactive Practice

Mini Practice: Smart Calculator

Create a Python program that asks the user for two numbers and prints: addition, subtraction, multiplication, division, remainder, and whether the first number is greater than the second number.

0% complete
Check when done
Quiz

Test Your Understanding

1. What will be the output of 10 % 3?

2. Which operator checks if two values are equal?

3. What will this return? True and False

🚀 Progression

Great work. You now understand how Python makes calculations and decisions. Next, you will learn one of the most powerful concepts in programming: Conditional Statements (if, else, elif).