🌱
Basic
Arithmetic · Comparison · Logical · f-strings · input()
Week 3 — Operators & I/O
Master Python's arithmetic, comparison, and logical operators, format output with f-strings, and build interactive programs with input().
operatorsf-stringinputformat
Duration
⏱ 2 hours
Level
📊 Beginner
Prerequisite
🎯 Week 2
OUTCOME
Build a simple calculator that reads two numbers and prints results
What you'll learn
- 1Use all arithmetic operators including // and %
- 2Compare values with ==, !=, <, >, <=, >=
- 3Combine conditions with and, or, not
- 4Format output with f-strings
- 5Chain augmented assignment operators (+=, -=, *=)
1. Arithmetic Operators
| Operator | Name | Example | Result |
|---|---|---|---|
| + | Add | 5 + 3 | 8 |
| - | Subtract | 5 - 3 | 2 |
| * | Multiply | 5 * 3 | 15 |
| / | Divide | 7 / 2 | 3.5 |
| // | Floor divide | 7 // 2 | 3 |
| % | Modulo | 7 % 2 | 1 |
| ** | Exponent | 2 ** 10 | 1024 |
💡
// always returns int (floors toward negative infinity). % returns the remainder — handy for even/odd checks: n % 2 == 0.
2. Comparison & Logical Operators
python
print(5 > 3) # True
print(5 == 5) # True
print(5 != 3) # True
print(True and False) # False
print(True or False) # True
print(not True) # False3. f-strings
f-strings (f"...") are the modern way to embed expressions inside string literals.
python
name = "Alice"
age = 20
pi = 3.14159
print(f"Hello, {name}!")
print(f"Age: {age}")
print(f"Pi ≈ {pi:.2f}") # 2 decimal places4. Augmented Assignment
python
x = 10
x += 5 # x = x + 5 = 15
x -= 3 # x = 12
x *= 2 # x = 24
print(x) # 245. Common Mistakes
- = vs ==: = assigns, == compares. if x = 5 is a SyntaxError.
- 7 / 2 → 3.5 (float), not 3. Use 7 // 2 for integer division.
- "5" + 5 → TypeError. Convert first: int("5") + 5.
💻 Examples
Run these examples and check the output yourself.
01_arithmetic.py— All arithmetic operators
CODE
a, b = 17, 5
print(f"{a} + {b} = {a+b}")
print(f"{a} - {b} = {a-b}")
print(f"{a} * {b} = {a*b}")
print(f"{a} / {b} = {a/b:.2f}")
print(f"{a} // {b} = {a//b}")
print(f"{a} % {b} = {a%b}")
print(f"{a} ** {b} = {a**b}")
▶ Output
17 + 5 = 22
17 - 5 = 12
17 * 5 = 85
17 / 5 = 3.40
17 // 5 = 3
17 % 5 = 2
17 ** 5 = 141985702_fstring.py— f-string formatting
CODE
name = "Alice"
score = 92.567
print(f"Student: {name}")
print(f"Score: {score:.1f}")
print(f"Passed: {score >= 60}")
▶ Output
Student: Alice
Score: 92.6
Passed: True03_calculator.py— Simple two-number calculator
CODE
a = float(input("First number: "))
b = float(input("Second number: "))
print(f"{a} + {b} = {a+b}")
print(f"{a} - {b} = {a-b}")
print(f"{a} * {b} = {a*b}")
if b != 0:
print(f"{a} / {b} = {a/b:.4f}")
else:
print("Division by zero!")
📝 Exercises
Try them yourself first, then open the solution to compare.
Exercise 1
Odd/Even Checker
Goal: Read an integer and print whether it is odd or even.
Requirements
- Use input() to read an integer
- Use % operator
- Print 'even' or 'odd'
Sample I/O
Enter a number: 7
7 is odd▶Toggle solution
SOLUTION
n = int(input("Enter a number: "))
if n % 2 == 0:
print(f"{n} is even")
else:
print(f"{n} is odd")
Exercise 2
BMI Calculator
Goal: Calculate and print BMI from weight and height.
Requirements
- Read weight (kg) and height (m) from input
- BMI = weight / height**2
- Print result with 2 decimal places
Sample I/O
Weight (kg): 65
Height (m): 1.70
BMI: 22.49▶Toggle solution
SOLUTION
w = float(input("Weight (kg): "))
h = float(input("Height (m): "))
bmi = w / h**2
print(f"BMI: {bmi:.2f}")
Example code / lecture materials
All lecture materials and example code are openly available on GitHub.
View on GitHub ↗