Python Series track

Week 03 · Basic Track

Week 3 — Operators & I/O

Master Python's arithmetic, comparison, and logical operators, format output with f-strings, and build interactive programs with input().

Basic Track2 min readFree lesson

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

  1. Calculate input() result as is

    a = input("Number:")   #String
    print(a + 10)       # TypeError
    

    → a = int(input("Number: "))

  2. Confuse / with // / is always a float, // is quotient (integer). 5/2 = 2.5, 5//2 = 2.

  3. Omit f before f-string

    print("{name}")   #Just print "{name}"
    print(f"{name}")  #Variable substituted
    
  4. 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 calculator
  • exercise2.md — Change calculation
  • exercise3.md — Currency converter

Next Week

Week 4. Conditional statements — Control flow with if/elif/else

Example files

The runnable files used in this lesson. Copy them into your own project and experiment.

examples/01_arithmetic.pyPython
print(5 + 3)      # 8
print(7 - 2)      # 5
print(4 * 6)      # 24
print(7 / 2)      #3.5  (real number division)
print(7 // 2)     #3    (quotient)
print(7 % 2)      #1    (remainder)
print(2 ** 10)    #1024 (2 to the power of 10)

#priority (same as in mathematics)
print(2 + 3 * 4)        # 14
print((2 + 3) * 4)      # 20
examples/02_input.pyPython
name = input("Please enter your name:")
print("Welcome,", name)
print(f"{name}, nice to meet you!")
examples/03_fstring.pyPython
name = "Hong Gil-dong"
score = 95
price = 12345

print(f"{name}'s score is {score} points")
print(f"Price: {price:,} Won")             #12,345 won
print(f"Pi: {3.141592:.2f}")         # 3.14
print(f"Name: {name:>10}")               #Right alignment width 10
print(f"Name: {name:<10}|")              #Left alignment
print(f"Name: {name:^10}|")              #Center alignment
examples/04_calc_input.pyPython
a = int(input("First number:"))
b = int(input("Second number:"))

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}")

Assignment

Goal

Enter height and weight to calculate and output BMI.

BMI Formula

BMI-weight-kg/height-m

Requirements

  • Enter height in cm (e.g., 175)
  • Weight in kg
  • Result up to two decimal places

Input/Output Example

$ python bmi.py
Height (cm): 175
Weight (kg): 70
Your BMI is 22.86.

Assignment 2. Change Calculator

Goal

Enter the payment amount and the amount received to output the change.

Requirements

  • Both values entered as integers (in won)
  • Change = Amount received - Payment amount
  • Display with thousand separators (f"{value:,}")

Input/Output Example

Payment Amount: 7800
Received Amount: 10000
Change: 2,200 won

Assignment 3. Exchange Rate Converter

Goal

Convert Korean Won (KRW) to US Dollars (USD).

Requirements

  • Exchange rate as a constant in the code (e.g., RATE = 1300)
  • Input can be an integer or a float
  • Result up to two decimal places

Input/Output Example

Korean Won: 130000
Dollar: $100.00

Challenge

  • Also output the reverse conversion (USD → KRW) at once
Show the reference solution

Try the assignment yourself first — comparing your attempt with a working solution is where most of the learning happens.

solution1.pyPython
height_cm = float(input("Height (cm):"))
weight_kg = float(input("Weight (kg):"))

height_m = height_cm / 100
bmi = weight_kg / (height_m ** 2)

print(f"Your BMI is {bmi:.2f}.")
solution2.pyPython
price = int(input("Payment Amount:"))
paid = int(input("Received Amount:"))

change = paid - price
print(f"Change: {change:,} won")
solution3.pyPython
RATE = 1300  # 1 USD = 1300 KRW

krw = float(input("Korean Won:"))
usd = krw / RATE

print(f"Dollar: ${usd:.2f}")
print(f"Reverse conversion verification: ${usd:.2f} = {usd * RATE:,.0f} KRW")

Learn this lesson with a mentor

Work through the Python Series track on your own, or join a guided cohort at our Pitampura campus and online.

Call NowEnroll Now