Python Lesson 6 – Operators | Dataplexa

Operators in Python

Operators are symbols that let Python perform actions on values and variables. They help you calculate totals, compare values, combine conditions, and update variables quickly.

In this lesson, you will learn the most important operator types used in real Python programs: arithmetic, assignment, comparison, logical, membership, and identity.

Arithmetic Operators

Arithmetic operators are used for math operations like addition, subtraction, multiplication, and division.

  • + addition
  • - subtraction
  • * multiplication
  • / division (always gives a float)
  • // floor division (drops decimal part)
  • % modulo (remainder)
  • ** power (exponent)

# Arithmetic operators

a = 10
b = 3

print(a + b)   # addition
print(a - b)   # subtraction
print(a * b)   # multiplication
print(a / b)   # division (float result)
print(a // b)  # floor division
print(a % b)   # remainder
print(a ** b)  # power
  
13
7
30
3.3333333333333335
3
1
1000
  • / gives a decimal result even if values are whole numbers.
  • // removes the decimal part.
  • % is useful for checking even/odd numbers and cycles.

Assignment Operators

Assignment operators store values in variables. Python also supports shortcut operators to update a variable in one step.

  • = assign
  • += add and assign
  • -= subtract and assign
  • *= multiply and assign
  • /= divide and assign

# Assignment operators

score = 10
print(score)

score += 5     # score = score + 5
print(score)

score -= 2     # score = score - 2
print(score)

score *= 3     # score = score * 3
print(score)

score /= 2     # score = score / 2 (float result)
print(score)
  
10
15
13
39
19.5
  • Shortcuts like += make code smaller and easier to read.
  • After /=, the result becomes a float because division produces decimals.

Comparison Operators

Comparison operators compare two values and return a boolean: True or False. These are the operators that power conditions in real programs.

  • == equal to
  • != not equal to
  • > greater than
  • < less than
  • >= greater than or equal to
  • <= less than or equal to

# Comparison operators

x = 20
y = 15

print(x == y)
print(x != y)
print(x > y)
print(x < y)
print(x >= 20)
print(y <= 10)
  
False
True
True
False
True
False
  • Comparison results are always True or False.
  • You will use these heavily in if conditions in the next lesson.

Logical Operators

Logical operators combine multiple conditions. They also return True/False.

  • and → True only if both conditions are True
  • or → True if at least one condition is True
  • not → reverses the boolean result

# Logical operators

age = 22
has_id = True

# Both must be true for access
print(age >= 18 and has_id)

# At least one condition should be true
print(age < 18 or has_id)

# Reverse the result
print(not has_id)
  
True
True
False
  • and is useful for strict rules.
  • or is useful when any one option is enough.
  • not flips True to False and False to True.

Membership Operators

Membership operators check whether a value exists inside a collection (like a list, set, or string).

  • in → checks if value exists
  • not in → checks if value does not exist

# Membership operators

courses = ["Python", "SQL", "AI"]

print("Python" in courses)
print("Java" in courses)

text = "Dataplexa Python Course"
print("Python" in text)
print("R" not in text)
  
True
False
True
True
  • Membership checks are common in validation and filtering logic.
  • Strings also support membership checks.

Identity Operators

Identity operators check whether two variables point to the same object in memory. This is different from checking if values are equal.

  • is → same object
  • is not → different objects

# Identity vs equality

a = [1, 2, 3]
b = [1, 2, 3]
c = a

print(a == b)   # values are equal
print(a is b)   # objects are different
print(a is c)   # same object
  
True
False
True
  • == compares values.
  • is compares object identity (memory reference).
  • For beginners, use == for most comparisons, and use is carefully.

Real-World Example: Checkout Discount Logic

This example uses arithmetic, comparison, and logical operators together. It calculates a final total and applies a discount rule.


# Checkout example using multiple operators

price = 499.0
quantity = 3
discount_rate = 0.10

subtotal = price * quantity

# Discount rule: apply discount if subtotal is 1000 or more
has_discount = subtotal >= 1000

if has_discount:
    total = subtotal - (subtotal * discount_rate)
else:
    total = subtotal

print("Subtotal:", subtotal)
print("Discount Applied:", has_discount)
print("Final Total:", total)
  
Subtotal: 1497.0
Discount Applied: True
Final Total: 1347.3
  • * calculates the subtotal.
  • >= checks if the discount rule is met.
  • - subtracts the discount amount.
  • The boolean has_discount helps the program decide which total to print.

Practice

Which operator gives the remainder of a division?

Which operator removes the decimal part in division?

Which operator checks if two values are equal?

Which logical operator becomes True only when both conditions are True?

Which membership operator checks whether a value exists inside a list or string?

Quick Quiz

What does // represent?




What is the result of: 10 > 5 and 2 < 1?




If a = [1,2] and b = [1,2], what is the result of a is b?




Which operator should you use to check if "Python" exists in a list?




Operators Summary Table

Category Operators Purpose
Arithmetic + - * / // % ** Perform mathematical operations
Assignment = += -= *= /= Assign and update variables
Comparison == != > < >= <= Compare values and return True/False
Logical and or not Combine or invert conditions
Membership in, not in Check if a value exists in a collection
Identity is, is not Check if two variables refer to the same object

Recap: You learned the core operator categories in Python and how they are used in real programs: calculations, comparisons, condition combining, membership checks, and safe updates.

Next up: Conditional Statements in Python.