1. Variables and data types
A variable is a named container for a value. In Python you create one by assigning a value to a name. Python works out the type automatically.
| Type | Name | Examples | Notes |
|---|---|---|---|
| int | Integer | 42, -7 | Whole numbers, positive or negative. |
| float | Float | 3.14, -0.5 | Numbers with a decimal point. |
| str | String | "hello" | Text, always in quotes. Single or double both work. |
| bool | Boolean | True, False | Exactly two values. Capital letters required. |
Getting input from the user
input() waits for the user to type something and returns it as a string. To use it as a number you must convert it with int() or float().
input(). When you click Run you will be prompted to type something. Try entering your name and birth year as instructed by the program.Modify the editor above to also ask for a subject the user likes, then print a sentence using all three pieces of information: name, age, and subject.
2. Selection: if, elif, else
Selection lets your program make decisions. Python uses indentation to define which code belongs to which block. This is not optional: wrong indentation causes a syntax error.
== and !=
Equal to, not equal to. == compares values. = assigns. Confusing them is one of the most common bugs in Python.
< > <= >=
Less than, greater than, and their or-equal-to variants. Work on numbers and strings (strings compare alphabetically).
and, or, not
Combine conditions. Same Boolean logic as section 03, just written as words instead of symbols.
in
Tests membership. "a" in "cat" is True. Very useful for checking whether a value is in a list or a character is in a string.
Write a program in the editor above that asks for a number and prints whether it is positive, negative, or zero. Then extend it to also say whether the number is odd or even. Hint: n % 2 == 0 is True when n is even.
3. Iteration: for and while loops
Iteration repeats a block of code. Use a for loop when you know how many times to repeat. Use a while loop when you want to keep going until a condition is met.
Write a program that prints the times table for a number the user enters. Use a for loop. Then extend it: use a while loop to keep asking for numbers until the user types "quit".
4. Functions and scope
A function is a named, reusable block of code. Functions let you write something once and use it many times. They are decomposition in practice: breaking a large problem into smaller, testable pieces.
Scope
Scope determines where a variable can be accessed. Variables inside a function are local: they only exist within that function. Variables outside are global.
Write three functions in a single editor:
celsius_to_fahrenheit(c): already shown above. Can you write afahrenheit_to_celsius(f)to go the other way?is_prime(n): returns True if n is prime, False otherwise. A number is prime if it has no divisors other than 1 and itself. Try dividing by every number from 2 up to n-1.count_vowels(text): returns the number of vowels in a string.
Test each one with several inputs to make sure it works correctly.
5. Lists
A list is an ordered, mutable collection of values. Lists can hold any type. You can change them after creation: add items, remove them, sort them.
Write a program that lets the user enter student names one at a time (stopping when they type "done"), stores them in a list, then prints the list sorted alphabetically, the total number of students, and whether anyone called "Alex" is in the list.
6. Mini-project: number guessing game
This project uses everything from this section. Build it step by step, testing at each stage before adding the next feature.
The brief
Build a number guessing game where the computer picks a secret number and the player tries to guess it.
Core requirements
- The computer picks a random integer between 1 and 100
- The player is told how many guesses they have (10)
- After each wrong guess, tell the player if they were too high or too low
- The game ends when the player guesses correctly or runs out of guesses
- Track and display the player's previous guesses after each attempt
Extension requirements
- Add difficulty levels: Easy (1-50, 15 guesses), Medium (1-100, 10 guesses), Hard (1-200, 7 guesses)
- Score the player: fewer guesses used = higher score
- Let the player play multiple rounds and track their best score
- Validate input: handle the user typing letters without crashing
Starter hints
- Use
import randomat the top, thenrandom.randint(1, 100)to pick the secret number - Structure it as functions: one to run a game, one to get valid input from the user
- A list is the natural choice for storing previous guesses