Sharing notes from my ongoing learning journey — what I build, break and understand along the way.
Python Practice Exercises – Part 1: Loops, Conditionals, and Algorithms
Python Practice Exercises – Part 1
In this week’s set of Python exercises, we practiced everything we’ve learned so far — from loops and conditionals to random number generation and string formatting.
The tasks below are sorted by difficulty, starting from basic loops and ending with more advanced algorithmic problems.
You can first try to solve each problem, then compare your results with the solutions provided below.
LEVEL 1 – Basic Exercises
1- Number Series 1–5
Write a loop that prints the following:1 2 3 4 5
2- Counting Down by Tens
Write a loop that prints:100 90 80 70 60 50 40 30 20 10
3- Counting by Thousands
Write a loop that prints:2000 3000 4000 5000 6000
4- Incrementing by Fours
Write a loop that prints:13 17 21 25 29
5- Half-Step Decimal Sequence
Write a loop that prints:2.0 1.5 1.0 0.5 0.0 -0.5 -1.0
6- Sum of Numbers 1–20
Write a loop that adds all numbers from 1 to 20 and prints the total.
7- Compare Two Numbers
Generate two random integers between 1 and 10 and display which one is greater or if they are equal.
8- Dog Years → Human Years
Ask for the age of a dog in years, then calculate its equivalent in human years:
- 1 dog year = 14 human years
- 2 dog years = 22 human years
- Each additional dog year = +5 human years
LEVEL 2 – Intermediate Exercises
9- Time-Based Greeting
Generate a random hour (0–23).
Depending on the time, display one of the following greetings:
- 22–23 or 0–4 → Good night
- 5–10 → Good morning
- 11–14 → Lunch time
- 15–17 → Good afternoon
- 18–21 → Good evening
10- Number Series – Missing 7
Print:1 2 3 4 5 6 8 9 10
11- Number Series – Missing 25 and 41
Print:13 17 21 29 33 37 45
12- Labeled Numbers
Print:Z5 Z7 Z9 Z11 Z13
13- Letter-Number Pattern
Print:a2b3 a12b13 a22b23
14- Perfect Squares (<100)
Print all square numbers smaller than 100 and display how many were found.
15- Mini Multiplication Table (1–10)
Create an algorithm that prints a small multiplication table from 1×1 to 10×10,
where each value is formatted with three digits:
001 002 003 004 005 006 007 008 009 010
002 004 006 008 010 012 014 016 018 020
003 006 009 012 015 018 021 024 027 030
004 008 012 016 020 024 028 032 036 040
005 010 015 020 025 030 035 040 045 050
006 012 018 024 030 036 042 048 054 060
007 014 021 028 035 042 049 056 063 070
008 016 024 032 040 048 056 064 072 080
009 018 027 036 045 054 063 072 081 090
010 020 030 040 050 060 070 080 090 100
16- Decimal Series
Print:1.0 2.2 3.4 4.6 5.8 7.0 8.2 9.4
17- Symmetrical Number Pattern
Print:1 2 3 4 5 4 3 2 1
LEVEL 3 – Advanced Exercises
18- Shipping Costs
Generate a random order value between 1.00 and 130.00 and determine the shipping cost:
- < 40 € → 3.99 €
- 40–69.99 € → 2.99 €
- 70–99.99 € → 1.99 €
- ≥ 100 € → Free shipping
Print the order value, shipping cost, and total.
19- Grade Average
Generate 20 random grades (1.0–6.0, one decimal place).
Then:
- Remove the best and worst grades.
- Calculate the average of the remaining 18 grades.
- Round the average to the nearest 0.5 (1.0, 1.5, 2.0, …, 5.5, 6.0).
- Print both the raw and rounded averages.
20- Star Patterns
Print the following patterns:
* * * *
* * * *
* * * *
*
* *
* * *
* * * *
* * * * *
*
* * *
* * * * *
* * * * * * *
Solutions
# 1
for i in range(1, 6):
print(i, end=" ")
print()
# 2
for i in range(100, 9, -10):
print(i, end=" ")
print()
# 3
for i in range(2000, 6001, 1000):
print(i, end=" ")
print()
# 4
for i in range(13, 30, 4):
print(i, end=" ")
print()
# 5
for i in range(4, -3, -1):
print(i / 2, end=" ")
print()
# 6
total = 0
for i in range(1, 21):
total += i
print(total)
# 7
from random import randint
z1, z2 = randint(1,10), randint(1,10)
print("Zahl 1:", z1, "Zahl 2:", z2)
if z1 == z2:
print("Equal")
elif z1 > z2:
print("Zahl 1 is greater")
else:
print("Zahl 2 is greater")
# 8
dog = int(input("Dog age in years: "))
if dog == 1:
print("14 human years")
elif dog == 2:
print("22 human years")
else:
print(f"{22 + (dog-2)*5} human years")
# 9
hour = randint(0,23)
if hour >= 22 or hour < 5:
print("Good night")
elif 5 <= hour < 11:
print("Good morning")
elif 11 <= hour < 15:
print("Lunch time")
elif 15 <= hour < 18:
print("Good afternoon")
else:
print("Good evening")
# 10
for i in range(1,11):
if i != 7:
print(i, end=" ")
print()
# 11
for i in range(13,46,4):
if i not in (25,41):
print(i, end=" ")
print()
# 12
for i in range(5,14,2):
print(f"Z{i}", end=" ")
print()
# 13
for i in range(2,23,10):
print(f"a{i}b{i+1}", end=" ")
print()
# 14
count = 0
for n in range(1,10):
print(n*n, end=" ")
count += 1
print("\nSquares found:", count)
# 15
for i in range(1,11):
for j in range(1,11):
print(f"{i*j:03}", end=" ")
print()
# 16
for i in range(5,48,6):
print(i/5, end=" ")
print()
# 17
for i in range(1,9):
print(i if i<=5 else 10-i, end=" ")
print()
# 18
import random
value = round(random.uniform(1,130),2)
if value < 40: s = 3.99
elif value < 70: s = 2.99
elif value < 100: s = 1.99
else: s = 0
total = round(value+s,2)
print(f"Order: {value}€, Shipping: {s}€, Total: {total}€")
# 19
grades = [round(random.uniform(1.0,6.0),1) for _ in range(20)]
trimmed = sorted(grades)[1:-1]
avg = sum(trimmed)/len(trimmed)
rounded = round(avg*2)/2
print("Grades:", grades)
print("Average:", avg, "Rounded:", rounded)
# 20
for _ in range(3):
print("* " * 4)
print()
for i in range(1,6):
print("*" * i)
print()
for i in range(4):
print(" "*(3-i) + "* "*(i+1))
