int(), float(), and str() are also the type names. They double as constructors.
"1" + "2" is "12", not 3int(), float(), str()
Today, they start talking to you. Every real program eventually needs input from a human. Python gives you exactly one tool for that:
input().
name = input("What is your name? ")
print(f"Hello, {name}!")
input() — the front door to your programTakes an optional prompt string, shows it to the user, waits for them to type, and returns whatever they typed when they press Enter.
age = input("Your age? ")
print("You typed:", age)
# Session:
# Your age? 19
# You typed: 19
input() always returns a string. Always. Even if the user types digits.
"1" + "2" is "12", not 3# What you wrote:
a = input("First number: ")
b = input("Second number: ")
print(a + b)
# Input: 1, 2
# Output: "12"
# What you meant:
a = int(input("First number: "))
b = int(input("Second number: "))
print(a + b)
# Input: 1, 2
# Output: 3
Text → whole number.
Text → decimal.
Number → back to text.
int(), float(), and str() are also the type names. They double as constructors.
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
float() first, then int() only if you actually need a whole number.
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
Every concept from today, in four lines.
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.")
input() · int() · arithmetic · f"…" — all four ideas in one program.
int() around input() when you want a number.int("12.5") — use float() first.+ with strings and numbers: "You are " + 19 crashes.%-formatting or .format() — just use f-strings."twenty" when you wanted 20.Ask the user for their name and age. Print a single line including how old they'll be next year.
Ask the bill amount and tip %. Print the tip and total, formatted to 2 decimal places.
students/<your-username>/D04_greeter.py. Full brief in lab/module_1_foundations/D04_greeter/problem.md. PR before 12:00.
if / elif / else deep dive on Day 6.