Python coding practice for beginners
Practice by writing a small program, checking its output, then testing a boundary case. These five exercises cover variables, conditions, collections and functions. You can use the free PyNex Python compiler without installing Python.
How to use these exercises
- Open the compiler in another tab and write your own solution first.
- Run it and compare the printed result with the expected output.
- Try every boundary test before opening the reference solution.
- Explain why the solution works, then change the inputs and run it again.
1. Add an order total
A notebook costs ₹45 and a pen costs ₹12. Buy two notebooks and three pens. Calculate the total using variables, then print the number.
Expected printed output
126
Check your solution: Change the notebook quantity to 3. The total should become 171.
Common mistake: Use numbers for calculation. Quoted values such as "45" are strings.
Show reference solution
notebook = 45
pen = 12
total = 2 * notebook + 3 * pen
print(total)2. Choose a delivery fee
Set order_total to 450. Delivery is free for orders of 500 or more, otherwise it costs 40. Print the delivery fee.
Expected printed output
40
Check your solution: Test 499, 500 and 501. Expected fees: 40, 0 and 0.
Common mistake: The boundary includes 500, so use >= rather than >.
Show reference solution
order_total = 450
if order_total >= 500:
delivery = 0
else:
delivery = 40
print(delivery)3. Filter passing scores
Given scores = [42, 75, 60, 35, 90], create a list containing scores of at least 60. Keep the original order.
Expected printed output
[75, 60, 90]
Check your solution: An empty input list should produce []. A score of exactly 60 must be included.
Common mistake: Do not remove items from the list while iterating over it. Build a new list.
Show reference solution
scores = [42, 75, 60, 35, 90]
passing = []
for score in scores:
if score >= 60:
passing.append(score)
print(passing)4. Count repeated words
Count words in ["python", "ai", "python", "data", "ai"]. Return a dictionary mapping each word to its count.
Expected printed output
{'python': 2, 'ai': 2, 'data': 1}Check your solution: Test an empty list and three copies of the same word. Dictionary key order does not affect the counts.
Common mistake: Initialize a missing key before incrementing it; get(word, 0) supplies that starting value.
Show reference solution
words = ["python", "ai", "python", "data", "ai"]
counts = {}
for word in words:
counts[word] = counts.get(word, 0) + 1
print(counts)5. Write a reusable average function
Write average(values). Return None for an empty list; otherwise return the arithmetic mean. Print average([10, 20, 30]).
Expected printed output
20.0
Check your solution: average([]) should be None. average([5]) should equal 5.0. Try negative values too.
Common mistake: Check the empty case before division to avoid ZeroDivisionError.
Show reference solution
def average(values):
if not values:
return None
return sum(values) / len(values)
print(average([10, 20, 30]))What should you learn next?
Read the free Python foundations curriculum for explanations and more lessons, or explore the 12-phase Python to AI Engineer track. If a solution fails, inspect the input, the condition and the printed value separately before changing the code.
Exercises authored for PyNex. General language reference: the official Python tutorial.