What’s one thing you learned? What’s still confusing?
Mini-Project: Mad Libs Generator
Ship your first interactive Python program in 20 minutes — collect input, weave a story with f-strings.
Control Flow: Decisions, Loops & Modern Python
if/elif/else, for/while loops, break/continue, walrus operator, and match/case pattern matching.
Mini-Project: Number Guessing Game
Build a 1-100 guessing game with hot/cold feedback — your first program combining randomness, loops, and conditionals.
Interactive Labs for This Track
Loop Visualizer
You're a factory robot repeating the same task on an assembly line — watch how loops automate repetitive work
List Slicing
You have a playlist of 50 songs — grab just tracks 10 through 20 with a single slice expression
Sorting Algorithms
You're organizing a library of 10,000 books — which sorting method is fastest?
Ask questions, share insights
=:age = 16
name = "Alex"
is_student = True
gpa = 3.85That is it. No special keywords, no type declarations. Python figures out the type automatically.
Try it! Open the Python REPL (bottom-right of the screen: click Quick Actions, then Python) and type these lines yourself. Change the values and see what happens.
Variables as References
Python has strict rules about variable names:
# VALID names
student_name = "Alex" # snake_case (Python convention)
age2 = 17 # can contain numbers (but not start with one)
_private = "secret" # can start with underscore
MAX_RETRIES = 3 # ALL_CAPS for constants (by convention)# INVALID names -- these cause errors
2cool = "nope" # ERROR: cannot start with a number
my-name = "nope" # ERROR: hyphens are not allowed (use underscores)
class = "nope" # ERROR: 'class' is a reserved keyword
my name = "nope" # ERROR: spaces are not allowed
snake_case for variable names (lowercase words separated by underscores). This is different from JavaScript's camelCase. Every Python developer follows this convention.Variables can be reassigned at any time:
score = 0
print(score) # 0
score = 100
print(score) # 100
score = score + 50 # use the old value to compute the new value
print(score) # 150
score += 25 # shorthand for score = score + 25
print(score) # 175+= shorthand works for all arithmetic operators: -=, *=, /=, //=, **=, %=.After running `x = 5; y = x; x = 10`, what is the value of y?
int -- Integers (Whole Numbers)age = 16
population = 8_000_000_000 # underscores for readability
negative = -42
zero = 0
print(type(age)) # <class 'int'>huge = 2 ** 1000 # a number with 302 digits. Python handles it.
print(huge)float -- Floating-Point Numbers (Decimals)pi = 3.14159
temperature = -40.0
learning_rate = 0.001 # very common in ML
tiny = 1e-8 # scientific notation: 0.00000001
print(type(pi)) # <class 'float'>print(0.1 + 0.2) # 0.30000000000000004 (not exactly 0.3!)This happens because computers store decimals in binary, and some decimal fractions cannot be represented exactly. It rarely matters in practice, but it is good to know.
str -- Strings (Text)name = "Alex"
prompt = 'What is the meaning of life?'
multiline = """This is a string
that spans multiple
lines."""
empty = "" # empty string -- still a string, just with no characters
print(type(name)) # <class 'str'>
print(len(name)) # 4New word alert: Immutable means "cannot be changed." Think of a printed book — you can read it but can't edit the pages. Strings and numbers are immutable in Python.
bool -- Booleans (True/False)is_raining = True
has_gpu = False
print(type(is_raining)) # <class 'bool'>
# Booleans come from comparisons
print(10 > 5) # True
print(10 == 5) # False
print(10 != 5) # True
print(10 >= 10) # Trueif statement, every while loop, every filter -- they all depend on booleans.None -- The Absence of a Valueresult = None
print(type(result)) # <class 'NoneType'>
print(result) # NoneNone is not zero, not an empty string, not False. It means "no value at all." It is Python's way of saying "nothing here yet." You will see it when a function does not explicitly return anything.What is type(3.14)?
<class 'float'>. Any number with a decimal point is a float in Python, even 3.0 (which is mathematically an integer but has a decimal point, so Python treats it as a float).Sometimes you need to convert between types. Python gives you built-in functions for this:
# String to int
age_str = "16"
age_num = int(age_str)
print(age_num + 1) # 17 (math works now)
# String to float
price_str = "9.99"
price = float(price_str)
print(price * 2) # 19.98
# Number to string
score = 100
message = "Your score: " + str(score)
print(message) # Your score: 100
# Float to int (truncates -- does not round!)
pi = 3.99
print(int(pi)) # 3 (not 4!)
# Everything to bool
print(bool(0)) # False
print(bool(42)) # True (any non-zero number is True)
print(bool("")) # False (empty string is False)
print(bool("hello")) # True (any non-empty string is True)
print(bool(None)) # FalseTrue or False) are important. In Python, these values are "falsy" (treated as False): 0, 0.0, "", None, [], {}, set(). Everything else is "truthy."What happens when you run int("3.14")?
int() refuses to silently discard data. Strings like "3.14" contain a decimal point, and int() only parses clean integers like "42". To convert a decimal string to an int, you need two steps: int(float("3.14")) → 3. This protects you from silent data loss — Python errors loudly instead of giving you a wrong answer.When you build an ML pipeline, you constantly convert types:
# User enters a number as text
user_input = "0.001" # this is a string from a web form
learning_rate = float(user_input) # convert to float for the model
# Model outputs a probability as a float
probability = 0.87
prediction = "spam" if probability > 0.5 else "not spam" # float to decision
# Pixel values are integers 0-255, but models want floats 0.0-1.0
pixel = 200
normalized = pixel / 255.0 # now it is 0.784...What happens when you run `int("3.14")`?
Hit aTypeErrororValueError? Conversion mistakes (int("abc"),"3" + 1) are the #1 source of these in your first month. See the error decoder for plain-English fixes.
f before the opening quote, and put variables inside curly braces {}:name = "Alex"
age = 16
gpa = 3.856
# f-strings -- clean and readable
print(f"My name is {name} and I am {age} years old.")
# Output: My name is Alex and I am 16 years old.
# You can put any expression inside the braces
print(f"Next year I will be {age + 1}.")
# Output: Next year I will be 17.
# Format numbers with precision
print(f"My GPA is {gpa:.2f}")
# Output: My GPA is 3.86 (rounded to 2 decimal places)
# Format large numbers with commas
params = 175_000_000_000
print(f"GPT-3 has {params:,} parameters")
# Output: GPT-3 has 175,000,000,000 parameters
# Format as percentage
accuracy = 0.9234
print(f"Model accuracy: {accuracy:.1%}")
# Output: Model accuracy: 92.3%% formatting and .format() method. Always use f-strings -- they are cleaner and faster.input() function pauses your program and waits for the user to type something:This code calls input(). Python here can't pause to ask you, so type the answers below before running — one value per line, in the order the program asks for them.
name = input("What is your name? ")
print(f"Hello, {name}! Welcome to Python.")
# IMPORTANT: input() always returns a string!
age_str = input("How old are you? ")
age = int(age_str) # convert to int for math
print(f"In 10 years you will be {age + 10}.")
# Common pattern: convert inline
temperature = float(input("Enter temperature in Fahrenheit: "))
celsius = (temperature - 32) * 5 / 9
print(f"{temperature}F = {celsius:.1f}C")input() always returns a string, even if the user types a number. You must convert it with int() or float() if you want to do math.Python has comparison and logical operators that produce boolean values:
x = 10
print(x == 10) # True (equal to)
print(x != 5) # True (not equal to)
print(x > 5) # True (greater than)
print(x < 5) # False (less than)
print(x >= 10) # True (greater than or equal to)
print(x <= 9) # False (less than or equal to)What does (5 > 3) and (10 < 2) evaluate to?
and operator requires BOTH sides to be truthy. The first part (5 > 3) is True, but the second (10 < 2) is False. Since and short-circuits as soon as it finds a falsy value, the whole expression becomes False. A subtle gotcha: Python's and actually returns the falsy value itself (here, False), not always a clean True/False — so 0 and 5 returns 0, not False. They behave the same in if statements, but printing them shows the difference.age = 16
has_permit = True
# and -- both must be True
can_drive = age >= 16 and has_permit
print(can_drive) # True
# or -- at least one must be True
is_weekend = False
is_holiday = True
day_off = is_weekend or is_holiday
print(day_off) # True
# not -- flips True to False and vice versa
is_raining = False
go_outside = not is_raining
print(go_outside) # Truevowels = "aeiou"
print("a" in vowels) # True
print("x" in vowels) # False
print("z" not in vowels) # TrueThese operators are the building blocks of control flow, which you will learn in the next lesson.
Tests · Complete each challenge and run to verify. Try printing the type of each variable!
Here is a mini-program that uses everything from this lesson:
This code calls input(). Python here can't pause to ask you, so type the answers below before running — one value per line, in the order the program asks for them.
# AI Model Report Card Generator
model_name = input("Enter model name: ")
params_billions = float(input("Parameters (in billions): "))
accuracy = float(input("Accuracy (0 to 1): "))
is_open_source = input("Open source? (yes/no): ").lower() == "yes"
params_total = int(params_billions * 1_000_000_000)
accuracy_pct = accuracy * 100
print("\n--- MODEL REPORT CARD ---")
print(f"Model: {model_name}")
print(f"Parameters: {params_total:,}")
print(f"Accuracy: {accuracy_pct:.1f}%")
print(f"Open Source: {'Yes' if is_open_source else 'No'}")
print(f"Type check: name is {type(model_name).__name__}, "
f"params is {type(params_total).__name__}, "
f"accuracy is {type(accuracy).__name__}, "
f"open_source is {type(is_open_source).__name__}")This tiny program demonstrates variables, all five types, type conversion, f-strings, and input. That is a lot of Python in just 15 lines.
== to compare values. Only use is for checking None:if x is None: # ✅ Correct way to check for None
if x == None: # ❌ Works but wrong styleInteractive Lab
Step through code and watch variables get assigned, updated, and used — see exactly what happens in memory
=, and Python figures out the type automatically. Use snake_case for namesint (whole numbers), float (decimals), str (text), bool (True/False), and None (nothing)int(), float(), str(), bool() to convert between types. input() always returns a stringf"Hello, {name}!" to embed variables directly in strings, with optional formatting like :.2f for decimalsThis program tries to add two values to compute a total, but it crashes with a TypeError. Fix it so it prints the numeric sum.
Sum: 7
Ask the user for a temperature in Fahrenheit, then print the equivalent in Celsius (rounded to 1 decimal place). Formula: C = (F - 32) * 5 / 9.
Enter Fahrenheit: 100
100.0F = 37.8CThis code calls input(). Python here can't pause to ask you, so type the answers below before running — one value per line, in the order the program asks for them.
# Temperature converter
fahrenheit_str = input("Enter Fahrenheit: ")
# TODO: convert fahrenheit_str to a float
# TODO: compute Celsius
# TODO: print "<F>F = <C>C" using an f-stringWhat does type('42') return?
TypeError==, !=, >, <) and logical operators (and, or, not) are the building blocks of decision-making in code