Foundations · #1 of 11
Variables, Types, and I/O
Names, casting, and user input
Why it matters
Every bug you fix in Python starts with understanding types, names, and input/output.
The idea
Variables are labels stuck on objects. You can re-label anytime, but the object stays the same — and the object always carries its type.
Try it
type() is your fastest debugger. When numbers behave like strings (or vice versa), type() tells you why.
age = 25
height = 5.9
name = "Ada"
is_engineer = True
print(age, type(age).__name__)
print(height, type(height).__name__)
print(name, type(name).__name__)
print(is_engineer, type(is_engineer).__name__)
input() ALWAYS returns a string. Cast before doing math — otherwise "2" + "3" == "23".
# input() blocked in Pyodide — pretend the user typed "42"
raw = "42"
print("raw:", raw, type(raw).__name__)
n = int(raw)
print("n*2 =", n * 2)
# What happens if you forget to cast?
try:
print(raw + 1)
except TypeError as e:
print("TypeError:", e)
Quick check
- Q: What type is
input()? A:str - Q: What happens with
int("10.5")? A: It errors — convert tofloatfirst.
Mini drills
- Ask for two numbers and print their sum.
- Convert centimeters to meters using input.
Common mistakes
- Mistake:
age = input(...)thenage + 1. Fix:age = int(input(...)). - Mistake: Shadowing built-ins like
list = []. Fix: Use better names.
Key takeaways
- Variables point to objects; the object owns the type.
input()always returns astr— cast withint()/float()before math.- Use
type()to diagnose mystery bugs. - Python is dynamically typed: the same name can re-bind to a different type.