Python Basics: Operators
Learn how to use arithmetic, comparison, logical, assignment, and membership operators like a coding wizard!
1. Arithmetic Operators: Math in Python
These operators perform math operations (like +, -, *, /).
Operator | Name | Example | Result |
---|---|---|---|
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 10 - 4 | 6 |
* | Multiplication | 3 * 4 | 12 |
/ | Division | 15 / 2 | 7.5 |
% | Modulus | 15 % 2 | 1 |
** | Exponent | 2 ** 3 | 8 |
// | Floor Division | 15 // 2 | 7 |
candies = 10
friends = 3
candies_per_friend = candies // friends # Each gets 3 candies
leftover = candies % friends # 1 candy left
print(f"Each friend gets {candies_per_friend} candies, with {leftover} left!")
Output:
Each friend gets 3 candies, with 1 left!
2. Comparison Operators: Decision-Making Tools
These operators compare values and return True or False.
Operator | Meaning | Example | Result |
---|---|---|---|
== | Equal to | 5 == 5 | True |
!= | Not equal to | 5 != 3 | True |
> | Greater than | 10 > 7 | True |
< | Less than | 10 < 7 | False |
>= | Greater than or equal | 8 >= 8 | True |
<= | Less than or equal | 5 <= 3 | False |
score = 85
passed = score >= 60
print("Did you pass?", passed)
Output:
Did you pass? True
3. Logical Operators: Combine Conditions
Use and
, or
, not
to combine True/False values.
Operator | Meaning | Example | Result |
---|---|---|---|
and | Both conditions must be True | (5 > 3) and (2 == 2) | True |
or | At least one condition is True | (5 < 3) or (2 == 2) | True |
not | Reverses the result | not (5 > 3) | False |
has_permission = True
has_form = False
# Can join the trip only if both are True
can_join = has_permission and has_form
print("Can join trip?", can_join)
Output:
Can join trip? False
4. Assignment Operators: Shortcut Updaters
These operators assign values to variables (with shortcuts!).
Operator | Example | Equivalent To |
---|---|---|
= | x = 5 | x = 5 |
+= | x += 3 | x = x + 3 |
-= | x -= 2 | x = x - 2 |
*= | x *= 4 | x = x * 4 |
/= | x /= 2 | x = x / 2 |
pocket_money = 20
pocket_money += 5 # Birthday gift!
print("Total money:", pocket_money)
Output:
Total money: 25
5. Membership Operators: Check if Present
Check if a value exists in a sequence (like a list or string).
Operator | Meaning | Example | Result |
---|---|---|---|
in | Exists in | "a" in "apple" | True |
not in | Does not exist in | "z" not in "apple" | True |
lunchbox = ["apple", "sandwich", "juice"]
print("cookie" in lunchbox) # Output: False
print("apple" in lunchbox) # Output: True
Fun Activity: Password Checker
username = input("Enter username: ")
password = input("Enter password: ")
# Check if password has at least 8 characters and a number
is_long = len(password) >= 8
has_number = any(char.isdigit() for char in password)
valid = is_long and has_number
print("Password valid?", valid)
Try:
Password: python3 → Too short!
Password: ilovecoding2024 → Valid!
Common Mistakes
- Using
=
instead of==
: - Forgetting
:
after conditions. - Mixing data types:
5 == "5"
→ False.
if x = 5: # ❌ Wrong! Use ==
Key Takeaways
- ✅ Arithmetic: Math operations (+, -, *, /).
- ✅ Comparison: Compare values (==, >, <).
- ✅ Logical: Combine conditions (and, or, not).
- ✅ Assignment: Update
- 3 (Floor division: 10//3 = 3)
- False (Because a > 5 is True → not True = False)
- True ("cat" is part of "catch")
Practice Quiz
What’s the output?
a = 10 b = 5 print(a // 3) # Q1: ? print(not (a > 5)) # Q2: ? print("cat" in "catch me if you can") # Q3: ?
Answers:
What’s Next?
Learn control flow (if-else, loops) to build games and quizzes!
Tags:
python