Python practice problems with solutions you can run

Once you know loops, lists and dictionaries, you need problems to practise on. These six come up again and again in courses and interviews. Each solution is one of several valid ways, and every one was run to get the output shown.

FizzBuzz

Print the numbers 1 to 15. For multiples of 3 print Fizz, for multiples of 5 print Buzz, and for multiples of both print FizzBuzz. The trick is to test the combined case first, because 15 is also a multiple of 3 and of 5.

for n in range(1, 16):
    if n % 15 == 0:
        print("FizzBuzz")
    elif n % 3 == 0:
        print("Fizz")
    elif n % 5 == 0:
        print("Buzz")
    else:
        print(n)

Output:

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz

Palindrome check

A palindrome reads the same backwards. Lower-case the text and remove spaces so capitals and spacing do not matter, then compare it with its reverse.

def is_palindrome(text):
    cleaned = text.lower().replace(" ", "")
    return cleaned == cleaned[::-1]

print(is_palindrome("Never odd or even"))
print(is_palindrome("Python"))

Output:

True
False

Count the vowels

Loop over the characters and count the ones that are in the string of vowels. sum() over a generator of 1s is a compact way to count.

def count_vowels(text):
    return sum(1 for ch in text.lower() if ch in "aeiou")

print(count_vowels("Pip and the Scrap Isle"))

Output:

6

The most common word

Use a dictionary as a counter. counts.get(word, 0) returns 0 for a word you have not seen yet, so you can add 1 without checking. max with key=counts.get picks the word with the highest count.

text = "the robot and the island and the sea"
counts = {}
for word in text.split():
    counts[word] = counts.get(word, 0) + 1
best = max(counts, key=counts.get)
print(best, counts[best])

Output:

the 3

Remove duplicates and keep the order

Converting to a set removes duplicates but loses the order. To keep the first occurrence of each item, keep a list of what you have seen.

items = ["a", "b", "a", "c", "b"]
seen = []
for item in items:
    if item not in seen:
        seen.append(item)
print(seen)

Output:

['a', 'b', 'c']

Two numbers that add up to a target

Given a list and a target, return the positions of two numbers that add up to it. A dictionary of numbers already seen answers the question in one pass: for each number, check whether the number you still need was seen before.

def two_sum(numbers, target):
    seen = {}
    for i, n in enumerate(numbers):
        if target - n in seen:
            return seen[target - n], i
        seen[n] = i

print(two_sum([2, 7, 11, 15], 9))

Output:

(0, 1)

Common questions

Where can I find more Python practice problems?
Start with the ideas above and change them: count consonants instead of vowels, find the least common word, or return all pairs for two-sum. Changing a solved problem is the best next problem.
Should I look at the solution if I am stuck?
Try for at least ten minutes first. Then read the solution, close it, and write it again from memory the next day. Copying without rewriting does not build the skill.
Are these problems good for interview practice?
Yes, FizzBuzz, palindromes, counting and two-sum are all classic warm-up questions. See the interview guide on this site for the concept questions (lists versus tuples, is versus ==).

Practise it