We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

12 Python Practice Problems for Beginners (With Solutions)

Boot.dev Team
Boot.dev TeamProgramming course authors and video producers

Last published

Table of Contents

I designed this set of free Python exercises to start with small functions, go through lists, dictionaries, and sets, then finish with two testing exercises. You can run every snippet in our free online Python playground without installing anything.

PLEASE actually try each problem before reading its solution. I've moved the solutions to the bottom of the page so that it's harder to accidentally cheat.

These exercises come from the Practice chapter of Learn Python for Beginners, with a few extra drills from its lists and sets chapters. If functions, loops, or collections are still new, the course teaches them in order and gives you a lot more guided practice.

Easy Python Practice Problems

These first four practice challenges use the fundamentals covered in our guides to Python functions and Python loops.

1. Add the Numbers From 1 to N

Complete the number_sum function. It should add all the numbers from 1 to n and return the result. If n is less than 1, return 0.

  • number_sum(5) returns 15 because 1 + 2 + 3 + 4 + 5 = 15
  • number_sum(3) returns 6 because 1 + 2 + 3 = 6
  • number_sum(0) returns 0

Remember that range() does not include its stop value.

def number_sum(n: int) -> int:
    pass

2. Divide Every Number in a List

Complete divide_list. It takes a list of numbers and a divisor, then returns a new list containing each original number after division. Don't change the input list, and don't round the results.

divide_list([6, 8, 10], 2) should return [3.0, 4.0, 5.0].

def divide_list(numbers: list[int], divisor: int) -> list[float]:
    pass

3. Join Strings With Commas

Complete join_strings. It takes a list of strings and returns one string containing the values in order, with a comma between them. Return an empty string for an empty list.

  • join_strings(["Annie", "Reiner", "Bertholdt"]) returns "Annie,Reiner,Bertholdt"
  • join_strings([]) returns ""

Don't use str.join(). The point is to practice the loop and handle the commas yourself.

def join_strings(strings: list[str]) -> str:
    pass

4. Reverse a List With a Loop

Write reverse_list. It should return a new Python list with the items in reverse order. Don't call list.reverse() or use the [::-1] shortcut.

  • reverse_list([1, 2, 3]) returns [3, 2, 1]
  • reverse_list(["a", "b", "c", "d"]) returns ["d", "c", "b", "a"]
def reverse_list(items: list[object]) -> list[object]:
    pass

Medium Python Practice Problems

These exercises add type checks, sets, and a few edge cases. Write down the expected result before running your code. That makes debugging a lot less random.

5. Find the Smallest Number Without min()

Write find_min, which returns the smallest number in a list. If the list is empty, return positive infinity using float("inf"). Don't call the built-in min() function.

  • find_min([1, 3, -1, 2]) returns -1
  • find_min([18, 3, 7, 2]) returns 2
  • find_min([]) returns float("inf")
def find_min(numbers: list[int]) -> int | float:
    pass

6. Remove Non-Integers From a List

Complete remove_nonints. It takes a list and returns a new list containing only values whose exact type() is int. Don't change the input list.

remove_nonints(["1", 1, "3", 4.0, 4, 500]) should return [1, 4, 500].

def remove_nonints(values: list[object]) -> list[int]:
    pass

7. Count Vowels and Track the Unique Ones

Complete count_vowels. It takes a string and returns two values:

  1. The total number of vowels in the string
  2. A set containing the unique vowels found

Count uppercase and lowercase vowels separately. For example, A and a are different values.

def count_vowels(text: str) -> tuple[int, set[str]]:
    pass

8. Find IDs Missing From the Second List

Complete find_missing_ids. It accepts two lists and returns a new set containing every ID found in the first list but not the second. The result shouldn't contain duplicates.

find_missing_ids([1, 1, 2, 3, 5], [1, 2, 4]) should return {3, 5}.

Python's set difference operation does most of the work once you've converted both lists.

def find_missing_ids(first_ids: list[int], second_ids: list[int]) -> set[int]:
    pass

Harder Python Practice Problems

The last four combine several fundamentals. You'll use loops with math, dictionaries, and tests that expose an exception. None of the solutions are long, but you need to account for the edge cases.

9. Calculate a Factorial

A factorial is the product of all positive integers less than or equal to a number:

  • 3! = 3 * 2 * 1 = 6
  • 5! = 5 * 4 * 3 * 2 * 1 = 120
  • 0! = 1

Complete factorial for non-negative integers. The ! symbol isn't a Python factorial operator, and you shouldn't use math.factorial() for this exercise.

def factorial(number: int) -> int:
    pass

10. Sum the Areas of Several Rectangles

Complete area_sum. It accepts a list of rectangles, where each rectangle is a dictionary with "height" and "width" keys. Calculate each rectangle's area and return the total.

rectangles = [
    {"height": 3, "width": 5},
    {"height": 2, "width": 4},
]

For that input, area_sum(rectangles) should return 23.

def area_sum(rectangles: list[dict[str, int]]) -> int:
    pass

11. Write a Test for an Empty List

The avg_luck_boost function below returns the average of a list of numbers. Its current tests all pass, but they don't cover an empty list.

def avg_luck_boost(luck_boosts: list[int]) -> float:
    total = 0
    for boost in luck_boosts:
        total += boost
    return total / len(luck_boosts)

Write a pytest test named test_empty_luck_boosts. It should expect avg_luck_boost([]) to return 0.0. The test is supposed to fail against the current implementation.

12. Fix the Failing Test

Now fix avg_luck_boost so the test passes. Return 0.0 when the input list is empty instead of raising ZeroDivisionError. Keep its behavior unchanged for non-empty lists.

Python Practice Problem Solutions

Solution 1

def number_sum(n: int) -> int:
    total = 0
    for number in range(1, n + 1):
        total += number
    return total

Solution 2

def divide_list(numbers: list[int], divisor: int) -> list[float]:
    divided: list[float] = []
    for number in numbers:
        divided.append(number / divisor)
    return divided

Solution 3

def join_strings(strings: list[str]) -> str:
    if len(strings) == 0:
        return ""
    joined = strings[0]
    for string in strings[1:]:
        joined += "," + string
    return joined

Solution 4

def reverse_list(items: list[object]) -> list[object]:
    reversed_items: list[object] = []
    for index in range(len(items) - 1, -1, -1):
        reversed_items.append(items[index])
    return reversed_items

Solution 5

def find_min(numbers: list[int]) -> int | float:
    smallest = float("inf")
    for number in numbers:
        if number < smallest:
            smallest = number
    return smallest

Solution 6

def remove_nonints(values: list[object]) -> list[int]:
    integers: list[int] = []
    for value in values:
        if type(value) is int:
            integers.append(value)
    return integers

Solution 7

def count_vowels(text: str) -> tuple[int, set[str]]:
    vowels = {"a", "e", "i", "o", "u", "A", "E", "I", "O", "U"}
    count = 0
    unique: set[str] = set()
    for character in text:
        if character in vowels:
            count += 1
            unique.add(character)
    return count, unique

Solution 8

def find_missing_ids(first_ids: list[int], second_ids: list[int]) -> set[int]:
    first_set = set(first_ids)
    second_set = set(second_ids)
    return first_set - second_set

Solution 9

def factorial(number: int) -> int:
    result = 1
    for factor in range(1, number + 1):
        result *= factor
    return result

Solution 10

def area_sum(rectangles: list[dict[str, int]]) -> int:
    total = 0
    for rectangle in rectangles:
        total += rectangle["height"] * rectangle["width"]
    return total

Solution 11

def test_empty_luck_boosts():
    assert avg_luck_boost([]) == 0.0

Solution 12

def avg_luck_boost(luck_boosts: list[int]) -> float:
    if len(luck_boosts) == 0:
        return 0.0
    total = 0
    for boost in luck_boosts:
        total += boost
    return total / len(luck_boosts)

Want More Python Practice?

Finished all 12? Head to the Training Grounds for more Python practice challenges. Browse over 40,000 student-rated challenges or generate one for the exact topic you want to work on.

Frequently Asked Questions

Where can I practice Python for free?

You can solve all 12 problems on this page for free and run your code in Boot.dev's free online Python playground. Boot.dev's beginner Python course is also free to start.

Are these Python exercises suitable for complete beginners?

They are designed for beginners who already know basic Python syntax, functions, conditionals, loops, lists, dictionaries, and sets. Start with the beginner Python course if those concepts are still new.

When should I look at a Python exercise's solution?

Write and test your own solution first. If you get stuck, compare the solution with your attempt, identify the idea you missed, then close the solution and write the function again yourself.

How often should a beginner practice Python?

Short, consistent sessions beat occasional marathons. Practice for 30 to 60 minutes several days per week, and spend most of that time writing and debugging code yourself.