Skip to content
Hands-on Mentor
ENTR
Back to blog

Python exercises for beginners with solutions

Ten Python exercises for beginners with solutions and real output, from variables to dictionaries. Try each one first, then compare with the answer.

4 min read

Good Python exercises for beginners are short, use one idea each, and have an answer you can check. Here are 10 exercises with solutions, all about Corner Café, a small shop in the fictional city of Port Alder.

Try each one before you open the solution. All the output shown comes from running the code.

Which exercises cover variables and conditions?

1. A customer buys 3 lattes at $4.25 each. Print the total.

price = 4.25
quantity = 3
print(price * quantity)
Output12.75

2. Greet a customer by name using an f-string.

name = "Maya"
print(f"Hello, {name}! Welcome to Corner Café.")
OutputHello, Maya! Welcome to Corner Café.

3. Print "expensive" if a price is above $4, otherwise "affordable".

price = 4.25
print("expensive" if price > 4 else "affordable")
Outputexpensive

Which exercises use loops and functions?

4. Add the numbers from 1 to 5 with a loop.

total = 0
for n in range(1, 6):
    total += n
print(total)
Output15

range(1, 6) stops before 6, so it gives 1, 2, 3, 4, 5.

5. Write a function that adds 10% sales tax and rounds to cents.

def add_tax(price, rate=0.10):
    return round(price * (1 + rate), 2)

print(add_tax(4.25))
Output4.68

Which exercises use lists and strings?

6. For a list of prices, print the average, the highest, and the sorted list.

prices = [4.25, 1.25, 3.75, 0.75]
print(sum(prices) / len(prices))
print(max(prices))
print(sorted(prices))
Output2.5
4.25
[0.75, 1.25, 3.75, 4.25]

7. Keep only the prices above $2 with a list comprehension.

prices = [4.25, 1.25, 3.75, 0.75]
print([p for p in prices if p > 2])
Output[4.25, 3.75]

8. Format a name, count its characters, and replace a word.

text = "corner café"
print(text.title())
print(len(text))
print(text.replace("café", "cafe"))
OutputCorner Café
11
corner cafe

Which exercises use dictionaries?

9. Add an item to a menu dictionary and read a price.

menu = {"latte": 4.25, "tea": 1.25, "cookie": 2.00}
menu["mocha"] = 4.75
print(menu["tea"])
print(len(menu))
Output1.25
4

10. Count how many of each item were ordered.

orders = [("latte", 2), ("tea", 3), ("latte", 1)]
counts = {}
for item, qty in orders:
    counts[item] = counts.get(item, 0) + qty
print(counts)
Output{'latte': 3, 'tea': 3}

counts.get(item, 0) returns the current count, or 0 if the item is new. This pattern appears in almost every real program.

How do you get the most from exercises?

1. Cover the solution and write your own first. A wrong attempt teaches more than a read solution.

2. Run your code. When you see an error, read the last line first.

3. Change a number or a word and predict the new output.

4. Do the same exercise again the next day without looking.

What mistakes do beginners make in these exercises?

  • Forgetting the colon after for, if, or def. Python reports a syntax error and points near the line.
  • Wrong indentation. The lines inside a loop or function must start with the same number of spaces.
  • Off-by-one in `range`. range(1, 6) ends at 5, not 6.
  • Mixing up `print` and `return`. A function that only prints gives you nothing to use later. In exercise 5, return hands the value back so you can reuse it.
  • Using `=` instead of `==`. One equals sign stores a value. Two compare values.

Which harder exercises can you try next?

Use the same data and change the question. Print the cheapest item on the menu. Count how many prices are above the average. Write a function that takes an order list and returns the total. Each of these needs only the ideas above, combined in a new way.

Keep a short list of the exercises that went wrong the first time. Repeat those two days later. This is how you turn a mistake into a skill.

If you can solve all 10 without looking, move on to reading and writing files and to small projects that combine these ideas. Each lesson in the course ends with a short test, so you always know whether an idea has stuck.

What to read next

  • [Python course: 40 short lessons with exercises, free](/courses/python)
  • [Functions and return lesson](/learn/python/function-return)
  • [Python for loop explained](/blog/python-for-loop)
  • [Common Python errors](/blog/common-python-errors)
  • #python
  • #python exercises
  • #practice
  • #beginners
All posts