Python exercises for beginners, with answers you can run
The fastest way to learn Python is to type small programs and see what they do. These seven exercises cover the ideas every beginner needs, in the order most courses teach them. Try each one yourself first, then compare with the answer. Every answer below was run, and the output shown is exactly what it prints.
1. Print and variables
A variable is a name for a value. Store a name and a number, then use an f-string to put them in a sentence. An f-string starts with f and puts variables in curly brackets.
Exercise: store your own name and a number, and print a sentence that uses both.
name = "Pip"
battery = 80
print(f"{name} has {battery}% battery")Output:
Pip has 80% battery
2. Strings
Strings are text. Methods like upper() change them, len() counts the characters, and slicing with [::-1] reverses them.
Exercise: take a word and print it in capitals, its length, and backwards.
word = "robot" print(word.upper()) print(len(word)) print(word[::-1])
Output:
ROBOT 5 tobor
3. Decisions with if
if runs a block only when a condition is true, and else covers the rest. The block is indented, and the line before it ends with a colon.
Exercise: print Low battery when the battery is under 20, and Battery OK otherwise.
battery = 15
if battery < 20:
print("Low battery")
else:
print("Battery OK")Output:
Low battery
4. Loops
A for loop repeats work. range(1, 11) gives the numbers 1 to 10 (the end is not included). A variable that collects a result as the loop runs is called an accumulator.
Exercise: add up the numbers from 1 to 10.
total = 0
for n in range(1, 11):
total += n
print(total)Output:
55
5. Lists
A list keeps many values in order. append adds to the end, and a for loop can walk straight through the list.
Exercise: start with two parts, add a third, and print every part on its own line.
parts = ["wrench", "cable"]
parts.append("chip")
for part in parts:
print(part)Output:
wrench cable chip
6. Dictionaries
A dictionary looks values up by key instead of by position. items() gives each key with its value.
Exercise: store prices for two items and print each item with its price.
prices = {"wrench": 3, "cable": 1}
for item, price in prices.items():
print(item, price)Output:
wrench 3 cable 1
7. Functions
A function is a named piece of code. It takes inputs, and return hands a result back. A function that forgets return gives back None, which is a classic beginner bug.
Exercise: write double(n) and use it to print the double of 21.
def double(n):
return n * 2
print(double(21))Output:
42
Common questions
- How should a complete beginner practise Python?
- Write small programs every day instead of reading for hours. Ten minutes a day on one idea (variables, then if, then loops) beats a long weekend session. Check your answer by running it, and change one thing to see what breaks.
- Do I need to install Python to do these exercises?
- No. The lessons on this site run Python in your browser, so there is nothing to install. When you want to run programs on your own computer, install Python from python.org and use any text editor.
- How many exercises do I need before I can build something?
- Around thirty small ones covering the seven topics above is enough to start a real project. This site mixes the exercises with missions that make a small game work, so you build something from the first lesson.