EveryoneWork through each subsection in order. The mini-project at the end uses everything covered here. Do not just read the code: edit it, break it, and see what happens. That is how programming is learned.
New to CSIf you have never written a line of code before, start here and take your time. Every concept is introduced from scratch. Click the green Run button in any editor to execute the code.

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.

TypeNameExamplesNotes
intInteger42, -7Whole numbers, positive or negative.
floatFloat3.14, -0.5Numbers with a decimal point.
strString"hello"Text, always in quotes. Single or double both work.
boolBooleanTrue, FalseExactly two values. Capital letters required.
Variables and types: run it, then try changing the values
output Output will appear here after you click Run.

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().

New to CSThe editor below uses input(). When you click Run you will be prompted to type something. Try entering your name and birth year as instructed by the program.
Input: run and follow the prompts
output Output will appear here after you click Run.
Try it yourself

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.

Grading: try different scores
output Output will appear here after you click Run.

== 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.

Try it yourself

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.

For loops: edit range values and see what changes
output Output will appear here after you click Run.
While loop: runs until the condition is false
output Output will appear here after you click Run.
Infinite loopsA while loop whose condition never becomes false will run forever. If your program seems to hang, check that something inside the loop is changing the variable the condition depends on. You can stop a runaway program by refreshing the page.
Try it yourself

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.

Functions: defining and calling
output Output will appear here after you click Run.

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.

Global scope
total = 0
name = "Ada"
Local scope: calculate_grade()
score = 85 only exists inside this function
grade = "B" only exists inside this function
Local scope: get_input()
user_text = "..." only exists inside this function
Scope in action: notice what is and is not accessible
output Output will appear here after you click Run.
Try it yourself

Write three functions in a single editor:

  1. celsius_to_fahrenheit(c): already shown above. Can you write a fahrenheit_to_celsius(f) to go the other way?
  2. 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.
  3. 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.

Lists: indexing, slicing, and built-in operations
output Output will appear here after you click Run.
Try it yourself

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 random at the top, then random.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
Your project: write your game here
output Output will appear here after you click Run.
ExtensionWhat is the optimal strategy for this game? How many guesses does a perfect player always need, at most? This is directly related to a searching algorithm called binary search that you will study at A Level. Think about it before looking it up.
Save your workThe editors on this page do not save automatically. Once you are happy with your game, copy the code and paste it somewhere safe: a Google Doc, a local file, or an email to yourself. You will need to paste it into the baseline form in section 6.

Before you move on