>> Module 01 / Day 04 / Lecture

I/O, Types & Conversions

Day 4 of a 40-day journey
Until today, your programs talked to themselves. Today, they start talking to you.
Lecture
75 min · 09:00–10:15
Lab
60 min · Greeter + Tip Calculator
Module
M1 · Python Foundations
DAY 04 / 40
>> Today's roadmap

Five stops, then a lab

  1. input() — talking to the user
  2. Why "1" + "2" is "12", not 3
  3. Type conversion — int(), float(), str()
  4. f-strings revisited — the only formatting you should use
  5. Lab — Greeter + Tip Calculator
>> The hook

Until today, your programs talked to themselves.

Today, they start talking to you. Every real program eventually needs input from a human. Python gives you exactly one tool for that: input().

first_taste.py
name = input("What is your name? ")
print(f"Hello, {name}!")
>> 01 / 05 · Talking to the user

input() — the front door to your program

Takes an optional prompt string, shows it to the user, waits for them to type, and returns whatever they typed when they press Enter.

input_basics.py
age = input("Your age? ")
print("You typed:", age)

# Session:
# Your age? 19
# You typed: 19
Always a string.
input() always returns a string. Always. Even if the user types digits.
>> 02 / 05 · The classic beginner bug

Why "1" + "2" is "12", not 3

what_you_wrote.py
# What you wrote:
a = input("First number: ")
b = input("Second number: ")
print(a + b)

# Input:  1, 2
# Output: "12"
what_you_meant.py
# What you meant:
a = int(input("First number: "))
b = int(input("Second number: "))
print(a + b)

# Input:  1, 2
# Output: 3
>> 03 / 05 · Converters

Three converters you'll use forever

int()
"42"  →  42

Text → whole number.

float()
"3.14"  →  3.14

Text → decimal.

str()
42  →  "42"

Number → back to text.

>> Things that will explode in your face

Five conversions to watch for

conversion_bugs.py
int("abc")             # ValueError — not a number
int("12.5")            # ValueError — int() refuses decimals
int("  42  ")          # OK — whitespace is fine
float("12.5")          # 12.5
int(float("12.5"))     # 12 — the "convert then convert" trick
Rule of thumb.
When in doubt, float() first, then int() only if you actually need a whole number.
>> 04 / 05 · String formatting

f-strings — the only formatting you should use

fstrings.py
name = "Aisha"
age  = 19
print(f"{name} is {age} years old.")
# → Aisha is 19 years old.

# You can do math inside the braces:
print(f"In 5 years, {name} will be {age + 5}.")

# You can format numbers:
pi = 3.14159
print(f"Pi to 2 places: {pi:.2f}")
# → Pi to 2 places: 3.14
>> 05 / 05 · Putting it together

A complete tiny program

Every concept from today, in four lines.

age_to_30.py
name = input("Your name? ")
age  = int(input("Your age? "))
years_to_30 = 30 - age
print(f"Hi {name}! You'll be 30 in {years_to_30} years.")
>> Common failures

Five mistakes you'll make today

  1. Forgetting int() around input() when you want a number.
  2. Calling int("12.5") — use float() first.
  3. Mixing + with strings and numbers: "You are " + 19 crashes.
  4. Using old %-formatting or .format() — just use f-strings.
  5. Trusting the user — they might type "twenty" when you wanted 20.
>> 11:00 – 12:00 · Lab

Today's lab — Greeter + Tip Calculator

PROBLEM 1
Greeter

Ask the user for their name and age. Print a single line including how old they'll be next year.

PROBLEM 2
Tip Calculator

Ask the bill amount and tip %. Print the tip and total, formatted to 2 decimal places.

>> End of Day 04

What you can now do

  • ✓ Read input from a user and convert it to the right type.
  • ✓ Format output cleanly with f-strings.
  • ✓ Catch and avoid the "string vs number" bug class.
01 / 12
←/→ navigate   F fullscreen   Ctrl/⌘+P save as PDF