Week 1: Variables, Types & I/O
🎯 Learning objectives
By the end of this week you can:
- Store a value under a name (a variable) and explain why that name is just a label, not a box.
- Identify Python's core built-in types:
int,float,str,bool. - Use arithmetic, comparison, and augmented-assignment operators correctly.
- Read input from a student at the keyboard and print formatted output back.
- Write a first small, complete, interactive program combining all of the above.
Lesson
Variables as names for values
In math, when you write "let ", you're binding a name to a value for the rest of the argument. Python does exactly this:
x = 5
The right-hand side is evaluated first (5), then the name x is pointed at it. Unlike math, x can be reassigned — x = 5 followed later by x = x + 1 doesn't contradict itself, it just re-points the label:
x = 5
x = x + 1 # x now names 6
Read x = x + 1 as "the new value of is the old value of plus one," the same way you'd read a recurrence relation .
You'll see this pattern — read the current value, compute something from it, store it back under the same name — so often that Python gives it a shorthand, the augmented assignment operators:
x = 5
x += 1 # same as x = x + 1 -> 6
x -= 2 # same as x = x - 2 -> 4
x *= 3 # same as x = x * 3 -> 12
x /= 4 # same as x = x / 4 -> 3.0
Naming your variables
A name (an identifier) must start with a letter or underscore, and can only contain letters, digits, and underscores after that — 2nd_score is invalid, second_score is fine. Python convention (and this course's style throughout) is snake_case: lowercase words separated by underscores, like student_name or total_score, rather than studentName or TotalScore. A handful of words are reserved by the language itself (if, for, class, True, and others) and can't be used as variable names at all.
Names should describe what a value means, not just satisfy the syntax. x = 87.5 tells a reader nothing; quiz_score = 87.5 tells them everything they need at a glance. This matters more than it might seem — you will reread your own code far more often than you write it.
Types: what kind of value is this?
Every value has a type — the set it belongs to, in math terms:
| Type | Math analogy | Example |
|---|---|---|
int | (integers) | 42, -7 |
float | (reals, approximated) | 3.14, -0.5 |
str | a finite sequence of characters | "hello" |
bool | True, False |
Check a value's type with type(...):
type(42) # <class 'int'>
type(3.14) # <class 'float'>
type("hi") # <class 'str'>
type(True) # <class 'bool'>
Python is dynamically typed: a name isn't permanently tied to one type. x = 5 then x = "five" is legal — x just points somewhere new. This is convenient, but it also means the type of a name can only be known by looking at what it currently points to, not by declaring it up front.
Converting between types
You can explicitly convert a value from one type to another using int(...), float(...), str(...), and bool(...) as functions:
int("42") # 42 — str -> int
int(3.9) # 3 — float -> int, truncates (does NOT round!)
float("3.14") # 3.14 — str -> float
str(42) # "42" — int -> str
bool(0) # False — 0 (and 0.0, and "") are "falsy"
bool(1) # True — any nonzero number (and non-empty string) is "truthy"
int(3.9) giving 3, not 4, trips people up constantly — conversion to int always truncates toward zero, it never rounds. Use the built-in round(3.9) (giving 4) if rounding is what you actually want.
Not every conversion is possible: int("hello") raises a ValueError, since "hello" isn't a number in any base. Python fails loudly here rather than silently guessing — a design choice you'll come to appreciate once you're debugging real data in Section 2.
Operators
Arithmetic operators mirror the ones you already know, with two Python-specific additions:
7 // 2 # 3 — floor division: floor(7/2)
7 % 2 # 1 — remainder, i.e. 7 mod 2
7 ** 2 # 49 — exponentiation, i.e. 7^2
-7 // 2 # -4 — floor division always rounds toward negative infinity, not toward zero
That last line is a common surprise: -7 // 2 is -4, not -3, because floor division follows the mathematical floor function , which rounds down (toward ), not toward zero.
Comparison operators (==, !=, <, <=, >, >=) produce a bool. Note == (equality test) is not = (assignment) — a very common first bug. You can also chain comparisons the way you would in math: 0 <= x < 10 means exactly what means, evaluated as a single expression, not two separate ones you'd need and to join (you'll meet and/or properly next week).
Operators combine with the usual precedence: ** first, then *, /, //, %, then +, -, left to right — the same PEMDAS order you already know. Parentheses override it exactly like in math:
2 + 3 * 4 # 14, not 20
(2 + 3) * 4 # 20
Input and output
input() reads a line of text typed by the student — it always returns a str, even if they typed a number:
name = input("What's your name? ")
print("Hello,", name)
Because input() always gives you a str, converting is common:
age_text = input("How old are you? ")
age = int(age_text) # str -> int
print("In 10 years you'll be", age + 10)
print() accepts multiple comma-separated arguments (joined with a space) or an f-string for more control:
score = 87.5
print(f"Your score is {score}%") # f-strings interpolate {expressions} directly
f-strings can hold more than a bare variable name — any expression works inside the { }, and you can format numbers precisely:
price = 19.999
print(f"Total: ${price:.2f}") # "Total: $20.00" — :.2f rounds to 2 decimal places
print(f"Double: {price * 2}") # any expression, not just a variable
print(f"{'yes' if price > 10 else 'no'}") # even a conditional expression works inline
⚠️ Common pitfalls
- Confusing
=and==.if score = 60:is a syntax error (you'll meetifproperly next week) — Python won't let you assign inside a condition by accident, but the instinct to type=when you mean "equals" is common early on. - Forgetting
input()returns a string.age = input("Age? ")thenage + 1raisesTypeError: can only concatenate str (not "int") to str— convert first:age = int(input("Age? ")). - Expecting
int()to round.int(4.7)is4, not5. Useround(4.7)for rounding. - Mixing up
/and//./always gives afloat(even4 / 2is2.0);//gives the floored quotient.
Worked example: a small interactive program
Putting everything from this week together — variables, types, conversion, operators, and I/O — into one short program that computes a simple tip calculator:
bill = float(input("Bill amount: $"))
tip_percent = int(input("Tip percent (e.g. 15): "))
tip_amount = bill * tip_percent / 100
total = bill + tip_amount
print(f"Tip: ${tip_amount:.2f}")
print(f"Total: ${total:.2f}")
Notice how each line does one clear thing: read a value, convert it to the right type, compute with it, and print a formatted result. This "read → compute → report" shape is one you'll reuse constantly, all the way through this course.
🧩 Challenges
What is type(7 / 2)? Predict it before running it in the playground, then check.
Predict the output of "3" + "4". Is it the same as 3 + 4?
Write a program that asks for a name and a birth year (as two separate input() prompts), computes an approximate age, and prints a sentence like "Amina, you are about 21 years old." Try it in the playground.
Without running it, compute 15 // 4 and 15 % 4 by hand. Then verify: does 4 * (15 // 4) + (15 % 4) equal 15?
Predict int(-7.9) and -7.9 // 1. Are they the same? Run both in the playground and explain any difference using what you learned about truncation versus flooring.
Extend the worked example's tip calculator to also ask how many people are splitting the bill, and print each person's share.
🤔 Socratic Questions
input()always returns astr. What would go wrong if you triedage + 10without first convertingage = int(input(...))? What does the error message actually tell you?- If
x = 5and theny = x, and thenx = 10, what isy? Explain why in terms of "names point to values" rather than "boxes contain values." 0.1 + 0.2in Python does not print exactly0.3. Try it in the playground. Why might afloat— which approximates using finite binary digits — not represent exactly, the same way has no finite decimal expansion?bool(0)isFalseandbool(1)isTrue— but what do you predictbool(-1)andbool(2)are? Test your prediction. What single rule explains all four results?- The augmented assignment
x += 1and the plainx = x + 1produce the same result for numbers. Can you think of a reason a language might still bother providing both forms, rather than requiring everyone to always write outx = x + 1?