Stage: Basic | Prerequisite: Week 2 (Variables, Data Types, Type Conversion)
Learning Goals
- Use arithmetic, comparison, and logical operators
- Receive user input with
input() - Neatly print using
print()and f-strings - Convert the received string to a number and calculate
1. Arithmetic Operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ |
Addition | 5 + 3 |
8 |
- |
Subtraction | 5 - 3 |
2 |
* |
Multiplication | 5 * 3 |
15 |
/ |
Division (float) | 7 / 2 |
3.5 |
// |
Division (quotient) | 7 // 2 |
3 |
% |
Remainder | 7 % 2 |
1 |
** |
Exponentiation | 2 ** 10 |
1024 |
2. Comparison and Logical Operators
print(5 > 3) # True
print(5 == 5) #True (Same)
print(5 != 3) #True (Different)
print(True and False) # False
print(True or False) # True
print(not True) # False
3. Assignment Operators
x = 10
x += 5 # x = x + 5 → 15
x -= 3 # 12
x *= 2 # 24
x //= 5 # 4
4. Receiving Input with input()
name = input("Please enter your name:")
print("Welcome,", name)
⚠️ input() always returns a string. To receive a number, use type conversion:
age = int(input("Age:"))
5. Formatting with f-strings
name = "Hong Gil-dong"
score = 95
print(f"{name}'s score is {score} points")
#Digits / Decimal Point / Alignment
price = 12345
print(f"Price: {price:,} Won") #Price: 12,345 Won
print(f"Pi: {3.141592:.2f}") #Pi: 3.14
print(f"{name:>10}") #Right-aligned width 10
Key Examples (examples/)
| File | Content |
|---|---|
01_arithmetic.py |
Collection of Arithmetic Operators |
02_input.py |
input() Basics |
03_fstring.py |
f-string formatting |
04_calc_input.py |
Convert input to number for calculation |
⚠️ Common Mistakes
-
Calculate
input()result as isa = input("Number:") #String print(a + 10) # TypeError→
a = int(input("Number: ")) -
Confuse
/with///is always a float,//is quotient (integer).5/2 = 2.5,5//2 = 2. -
Omit
fbefore f-stringprint("{name}") #Just print "{name}" print(f"{name}") #Variable substituted -
Use
=instead of===is assignment,==is comparison. Often confused in conditional statements.
❓ FAQ
Q1. print() automatically adds a newline at the end. How to prevent it?
A. Remove newline with print("hi", end=""). Like end="-", other characters are also possible.
Q2. How to print multiple variables at once?
A. Separate with commas as in print(a, b, c) to connect with spaces. Can change delimiter using sep="-".
Q3. How to receive multiple inputs in one line?
A. a, b = input("Two numbers: ").split() — separate with spaces. Still strings, so type conversion is needed.
📝 Exercises (exercises/)
exercise1.md— BMI calculatorexercise2.md— Change calculationexercise3.md— Currency converter
Next Week
Week 4. Conditional statements — Control flow with if/elif/else