
Python Course
Operators in Python
Operators are the symbols and keywords that tell Python what action to perform on values and variables. Without operators, you could store data but you could not do anything useful with it — no calculations, no comparisons, no decisions.
Every time you add two numbers, check if a user is logged in, or verify whether an item is in a list, you are using an operator. This lesson covers all the major operator types you will use in real Python programs.
Arithmetic Operators
Arithmetic operators perform mathematical calculations. They work on numbers and return a numeric result.
+— adds two values together-— subtracts the right value from the left*— multiplies two values/— divides and always returns a decimal result//— floor division, drops the decimal part%— modulo, gives the remainder after division**— raises a number to the power of another
a = 10
b = 3
print(a + b) # 13
print(a - b) # 7
print(a * b) # 30
print(a / b) # 3.333... (always float)
print(a // b) # 3 (removes decimal)
print(a % b) # 1 (remainder)
print(a ** b) # 1000 (10 to the power of 3)
/always gives a decimal result, even when dividing two whole numbers.//is useful when you only need the whole part — like calculating how many complete boxes fit.%is very useful for checking whether a number is even or odd, or for creating repeating cycles.
Assignment Operators
Assignment operators store values in variables. Python also has shortcut versions that update a variable in a single step, making your code shorter and easier to read.
=— stores a value in a variable+=— adds to the current value and saves the result-=— subtracts and saves the result*=— multiplies and saves the result/=— divides and saves the result
score = 10
print(score)
score += 5 # same as: score = score + 5
print(score)
score -= 2 # same as: score = score - 2
print(score)
score *= 3 # same as: score = score * 3
print(score)
score /= 2 # same as: score = score / 2
print(score)
- These shortcuts are used constantly in loops, counters, and score trackers.
- After using
/=, the variable becomes a float because division always produces a decimal.
Comparison Operators
Comparison operators compare two values and return a boolean — either True or False. These results are what power decisions in your programs. You will use these in almost every if statement you write.
==— checks if two values are equal!=— checks if two values are not equal>— left value is greater than right<— left value is less than right>=— greater than or equal to<=— less than or equal to
x = 20
y = 15
print(x == y) # False — 20 is not equal to 15
print(x != y) # True — they are different
print(x > y) # True — 20 is greater
print(x < y) # False — 20 is not less than 15
print(x >= 20) # True — 20 equals 20
print(y <= 10) # False — 15 is not less than or equal to 10
- Never confuse
=(assignment) with==(comparison). This is one of the most common beginner mistakes. - Comparison results are always True or False — you can store them in variables or use them directly in conditions.
Logical Operators
Logical operators let you combine multiple conditions into a single expression. They are used whenever a decision depends on more than one thing being true at the same time.
and— True only when both conditions are Trueor— True when at least one condition is Truenot— reverses the boolean result
age = 22
has_id = True
# and: both conditions must be true
print(age >= 18 and has_id) # True
# or: at least one must be true
print(age < 18 or has_id) # True
# not: flips the result
print(not has_id) # False
- Use
andfor strict rules — the person must be over 18 AND have valid ID. - Use
orfor flexible rules — the person can pay by card OR cash. - Use
notto flip a condition — run the block if the user is NOT blocked.
Membership Operators
Membership operators check whether a value exists inside a collection such as a list, a string, or a set. They return True or False.
in— True if the value exists in the collectionnot in— True if the value does not exist
courses = ["Python", "SQL", "AI"]
print("Python" in courses) # True
print("Java" in courses) # False
text = "Dataplexa Python Course"
print("Python" in text) # True
print("R" not in text) # True
- Membership checks work on lists, strings, sets, tuples, and dictionaries.
- They are commonly used for input validation — is the user's input one of the allowed options?
Identity Operators
Identity operators check whether two variables point to the exact same object in memory. This is different from checking if two values are equal — two variables can hold identical values but still be separate objects.
is— True if both variables refer to the same objectis not— True if they refer to different objects
a = [1, 2, 3]
b = [1, 2, 3]
c = a
print(a == b) # True — same values
print(a is b) # False — different objects in memory
print(a is c) # True — c points to the same object as a
==compares values.iscompares identity (memory location).- For most everyday comparisons, use
==. Useisspecifically when you need to check object identity.
Real World Example — Checkout Discount Logic
This example uses arithmetic, comparison, and logical operators together to build a simple but realistic shopping checkout system.
price = 499.0
quantity = 3
discount_rate = 0.10
subtotal = price * quantity
# Apply 10% discount if subtotal reaches 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)
*calculates the subtotal from price and quantity.>=checks whether the discount threshold is reached.- The boolean result in
has_discountdrives which total gets printed. - This pattern — calculate, compare, decide — is at the heart of almost every real program.
Operators at a Glance
| Category | Operators | Purpose |
|---|---|---|
| Arithmetic | + - * / // % ** | Perform mathematical calculations |
| Assignment | = += -= *= /= | Store and update variable values |
| Comparison | == != > < >= <= | Compare values, return True or False |
| Logical | and or not | Combine or reverse conditions |
| Membership | in not in | Check if a value exists in a collection |
| Identity | is is not | Check if variables share the same object |
Practice
Which operator gives the remainder after dividing two numbers?
Which operator divides two numbers and drops the decimal part?
Which operator checks if two values are equal to each other?
Which logical operator returns True only when all conditions are True?
Which membership operator checks if a value exists inside a list or string?
Quick Quiz
What type of division does the // operator perform?
What is the result of 10 > 5 and 2 < 1?
If a = [1,2] and b = [1,2], what does a is b return?
Which operator checks if "Python" exists in a list of courses?