โ
โญ Module 3 of 12 ยท Python for Beginners
Operators
in Python
Operators are the action words of programming โ they tell Python what to do with your data. Master them and your code comes alive!
๐ 6 Lessons
๐งฎ Live Calculator
โ๏ธ 3 Exercises
๐ง 5 Questions
๐ฏ What You'll Learn
Arithmetic
Add, subtract, multiply, divide
Comparison
Compare values
Logical
Combine conditions
Precedence
Order of operations
โ
Prerequisites
โ
Variables & Data Types (Module 2)๐ Lesson 1
Arithmetic Operators
Perform mathematical calculations on numbers.
| Operator | Name | Example | Result |
|---|---|---|---|
| + | Addition | 10 + 3 | 13 |
| - | Subtraction | 10 - 3 | 7 |
| * | Multiplication | 10 * 3 | 30 |
| / | Division | 10 / 3 | 3.333โฆ |
| // | Floor Division | 10 // 3 | 3 |
| % | Modulus | 10 % 3 | 1 |
| ** | Exponentiation | 2 ** 8 | 256 |
arithmetic.py
a, b = 17, 5print(a + b) # 22
print(a - b) # 12
print(a * b) # 85
print(a / b) # 3.4
print(a // b) # 3
print(a % b) # 2
print(a ** b) # 1419857
22
12
85
3.4
3
2
1419857
12
85
3.4
3
2
1419857
๐ต Modulus Visualizer โ See the remainder
Divide
by
The modulus operator
% gives you the remainder. Great for checking if a number is even: n % 2 == 0!๐งฎ Interactive Demo
Live Python Calculator
Try every operator โ see the Python expression and result in real time.
0
๐ Python Equivalent
print(0) # 0๐ History
๐ Lesson 2
Comparison Operators
Compare values โ always return True or False.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| == | Equal to | 5 == 5 | True |
| != | Not equal | 5 != 3 | True |
| > | Greater than | 10 > 3 | True |
| < | Less than | 3 < 10 | True |
| >= | Greater/equal | 5 >= 5 | True |
| <= | Less/equal | 4 <= 5 | True |
โ๏ธ Interactive Comparison
Don't confuse
= (assignment) with == (comparison)!๐ Lesson 3
Logical Operators
Combine conditions โ and, or, not.
๐ค
and
True only if both are True
True and True โ True๐
or
True if at least one is True
True or False โ True๐
not
Flips the value
not True โ Falselogical.py
age = 20score = 85
print(age >= 18 and score >= 80)
print(age >= 18 or score >= 90)
True
True
True
๐ Lesson 4
Operator Precedence & Shorthand
Order of operations and augmented assignments.
1
( )
Parentheses
2
**
Exponent
3
* / // %
Multiply/Divide
4
+ -
Add/Subtract
5
== != < > <= >=
Comparisons
6
not
Logical NOT
7
and
Logical AND
8
or
Logical OR
x += 5
โ
x = x + 5
x -= 3
โ
x = x - 3
x *= 2
โ
x = x * 2
x /= 4
โ
x = x / 4
x //= 3
โ
x = x // 3
x %= 7
โ
x = x % 7
๐ป Practice
Coding Exercises
โญ Exercise 1
Odd or Even
Use the modulus operator to check if 47 is odd/even.
n = 47
print(f"{n} is {'even' if n % 2 == 0 else 'odd'}") # odd
print(f"{n} is {'even' if n % 2 == 0 else 'odd'}") # odd
โญ Exercise 2
Voter Eligibility
Check if age >= 18 AND is_citizen = True.
age, is_citizen = 20, True
can_vote = age >= 18 and is_citizen # True
can_vote = age >= 18 and is_citizen # True
โญ Exercise 3
Predict the Output
Predict: 3 + 4 * 2, (3+4)*2, 2**3**2, 15%4, True and False or True
# 11, 14, 512, 3, True
๐ง Quiz Time
Test Your Knowledge
Question 1 of 5
๐
5/5
5
Correct
0
Wrong
Module 03 โ Complete
Operators Mastered! ๐
You can now perform calculations, comparisons, and logical operations.
โ Arithmetic
โ Comparison
โ Logical
โ Precedence
โ Shorthand
Next up: Module 4 โ Conditionals