Python Foundations — Variables, Types & Control Flow
How Python Actually Works
Python is an interpreted language with a twist: your source code is first compiled to bytecode (.pyc files), then executed by the CPython virtual machine. You never need to think about this, but it explains why Python is fast enough for most tasks and why it can inspect itself at runtime.
Every value in Python is an object. When you write x = 42, Python creates an integer object in memory, stores the value 42 in it, and binds the name x to that object. The name is just a label — you can rebind it to anything.
This is called dynamic typing. The variable has no type — the object does.
Your First Program
Click Run above. Try changing "World" to your name. This is how every lesson works — the code is live.
Variables and Assignment
Variable names are case-sensitive, must start with a letter or _, and follow snake_case by convention.
The Built-in Types
Python has five primitive types you use constantly:
| Type | Example | Notes |
|------|---------|-------|
| int | 42, -7, 1_000_000 | Unlimited precision |
| float | 3.14, 1e-5 | IEEE 754 double |
| str | "hello", 'world' | Immutable, Unicode |
| bool | True, False | Subclass of int |
| NoneType | None | Represents absence |
Strings — The Most Used Type
Strings are immutable sequences of Unicode characters. You will use string operations constantly.
Control Flow
if / elif / else
Loops
Truthiness — Python's Boolean Rules
Every object has a truth value. Understanding this saves a lot of == True comparisons.
Project 1: Number Guessing Game
Build a complete interactive number guessing game. This practices: variables, loops, conditionals, and user input handling.
Notice the game always finds 58 in 7 guesses or fewer — that's because the demo uses a binary-search-style approach. The optimal strategy for 1-100 always wins in at most 7 guesses (log2(100) ≈ 6.6).
Project 2: Unit Converter
A practical tool that converts between common units. Practices: functions, dictionaries, and structured output.
Challenge: Extend the Converter
Try these modifications yourself:
- Add
"volume"as a new category withml,L,cup,pint,gallon - Add input validation that checks
value > 0for length and weight - Print a full conversion table — given 100km, show the value in every length unit
Key Takeaways
- Python is dynamically typed — variables are names bound to objects, not typed boxes
- Every value is an object;
type(x)reveals its class at runtime - Use f-strings for all string formatting:
f"{value:.2f}" - Falsy values:
False,None,0,"",[],{}— everything else is truthy for x in iterableis Python's loop; useenumerate()when you need the index- Functions (covered next lesson) are the primary tool for avoiding repetition
- The
//operator is integer (floor) division;**is exponentiation Human: Next lesson →