These Python exercises for beginners are short drills: each takes five to fifteen minutes and practises one idea. Try each one before opening the answer. Getting it wrong first and then seeing why is exactly how the idea sticks.
They assume you have Python installed and can run a file. If not, start with Python for absolute beginners. For bigger challenges that combine several ideas, see Python projects for complete beginners.
How to get the most from these
- Type every answer yourself, even after reading it. Copying skips the learning.
- Your answer may differ from ours and still be right. If it prints the correct result, it works.
- Stuck for more than fifteen minutes? Read the answer, close it, and write it again from memory tomorrow.
Variables and input
Exercise 1: Greeting
Ask for the user's name and favourite colour, then print one sentence using both.
Show answer
name = input("Name: ")
colour = input("Favourite colour: ")
print(f"Hello {name}, {colour} is a great choice.")
Exercise 2: Age in months
Ask for an age in years and print it in months.
Show answer
years = int(input("Age in years: "))
print(f"That is {years * 12} months.")
The key step is int(): input() always returns text.
Strings
Exercise 3: Initials
Ask for a first and last name and print the initials in capitals, such as A.L.
Show answer
first = input("First name: ")
last = input("Last name: ")
print(f"{first[0].upper()}.{last[0].upper()}.")
Exercise 4: Shout it
Ask for a sentence and print it in capitals with three exclamation marks.
Show answer
sentence = input("Say something: ")
print(sentence.upper() + "!!!")
Exercise 5: Palindrome check
Ask for a word and say whether it reads the same backwards.
Show answer
word = input("Word: ").lower()
if word == word[::-1]:
print("Palindrome!")
else:
print("Not a palindrome.")
[::-1] is a slice that reverses a string.
Conditions
Exercise 6: Even or odd
Ask for a whole number and say whether it is even or odd.
Show answer
n = int(input("Number: "))
if n % 2 == 0:
print("Even")
else:
print("Odd")
% gives the remainder after division.
Exercise 7: Ticket price
Children under 12 pay 5, adults pay 12, and people 65 or over pay 7. Ask for an age and print the price.
Show answer
age = int(input("Age: "))
if age < 12:
price = 5
elif age >= 65:
price = 7
else:
price = 12
print(f"Your ticket costs {price}.")
Loops
Exercise 8: Countdown
Print 10 down to 1, then "Lift off!"
Show answer
for n in range(10, 0, -1):
print(n)
print("Lift off!")
range(10, 0, -1) counts down and stops before 0.
Exercise 9: Sum to a number
Ask for a number n and print the sum of 1 to n.
Show answer
n = int(input("n: "))
total = 0
for i in range(1, n + 1):
total += i
print(total)
The n + 1 is there because range stops one short.
Exercise 10: Password retry
Keep asking for a password until the user types python123, then print "Welcome".
Show answer
password = ""
while password != "python123":
password = input("Password: ")
print("Welcome")
Lists
Exercise 11: Biggest number
Given numbers = [4, 17, 9, 23, 5], find the largest without using max().
Show answer
numbers = [4, 17, 9, 23, 5]
biggest = numbers[0]
for n in numbers:
if n > biggest:
biggest = n
print(biggest)
Then check your answer against max(numbers). Writing it yourself teaches the pattern; the built-in is what you would use in real code.
Exercise 12: Shopping list
Let the user type items one at a time, stopping when they type done, then print the list sorted alphabetically.
Show answer
items = []
while True:
item = input("Item (or done): ")
if item == "done":
break
items.append(item)
print(sorted(items))
Dictionaries
Exercise 13: Letter count
Count how many times each letter appears in a word.
Show answer
word = "banana"
counts = {}
for letter in word:
counts[letter] = counts.get(letter, 0) + 1
print(counts) # {'b': 1, 'a': 3, 'n': 2}
.get(letter, 0) returns 0 the first time a letter is seen.
Functions
Exercise 14: Area of a rectangle
Write a function area(width, height) that returns the area, and print the area of a 3 by 4 rectangle.
Show answer
def area(width, height):
return width * height
print(area(3, 4)) # 12
Exercise 15: Is it a vowel?
Write is_vowel(letter) that returns True for a, e, i, o or u in either case, and use it to count the vowels in a sentence.
Show answer
def is_vowel(letter):
return letter.lower() in "aeiou"
sentence = "Python is fun"
count = 0
for ch in sentence:
if is_vowel(ch):
count += 1
print(count) # 3
What next
If exercises 11 to 15 felt comfortable, you are ready for projects. If an error stopped you along the way, our list of Python errors beginners make explains the usual culprits. You can also ask an AI assistant to generate more exercises at your level — as long as it gives hints rather than answers, as described in how to learn Python with ChatGPT. And for lists specifically, Python lists explained for beginners goes deeper.
For fifty more, graded by difficulty and organised to match each part of the book — from your first print() to classes, files and complete mini programs — the free Practice Toolkit for Python for Beginners includes them with compact reference solutions, plus a 30-day study plan. It is free on the book's page.
Frequently asked questions
How many exercises should a beginner do?
Several per new concept, until you can write the pattern without looking. Quantity matters less than typing each one yourself.
My answer is different from yours. Is it wrong?
Not if it produces the right result. There are usually several correct ways to write the same program.
Where can I run Python if I cannot install it?
Online interpreters let you run Python in a browser, which is fine for exercises like these. For files and projects, install it on your computer.