100 Python Beginner Exercises with Solutions
While books and videos support learning, the key to mastering Python lies in consistent coding practice. Below are selected problems from a curated set of 100 beginner exercises covering syntax, data structures, and basic algorithms—each with a clear solution.
Exercise 1: Unique Three-Digit Numbers
Problem: Using digits 1, 2, 3, and 4, how many unique three-digit numbers can be formed without repeating any digit?
count = 0
for hundreds in range(1, 5):
for tens in range(1, 5):
for units in range(1, 5):
if len({hundreds, tens, units}) == 3:
print(hundreds, tens, units)
count += 1
print("Total:", count)
Exercise 2: Bonus Calculation
Problem: Calculate bonus based on profit tiers with decreasing commission rates.
profit = int(input("Enter profit: "))
bonus = 0
brackets = [100000, 100000, 200000, 200000, 400000]
rates = [0.1, 0.075, 0.05, 0.03, 0.015, 0.01]
for i in range(len(brackets)):
if profit <= brackets[i]:
bonus += profit * rates[i]
break
else:
bonus += brackets[i] * rates[i]
profit -= brackets[i]
else:
bonus += profit * rates[-1]
print(bonus)
Exercise 3: Perfect Square Condition
Problem: Find an integer such that adding 100 and then 168 results in two perfect squares.
n = 0
while (n + 1) ** 2 - n ** 2 <= 168:
n += 1
limit = (n + 1) ** 2
for x in range(limit):
if x**0.5 == int(x**0.5) and (x + 168)**0.5 == int((x + 168)**0.5):
print(x - 100)
Exercise 4: Day of Year
Problem: Given a date, determine its ordinal day in the year.
def is_leap(y):
return y % 400 == 0 or (y % 4 == 0 and y % 100 != 0)
days_in_month = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30]
year = int(input("Year: "))
month = int(input("Month: "))
day = int(input("Day: "))
if is_leap(year):
days_in_month[2] = 29
total_days = sum(days_in_month[:month]) + day
print(total_days)
Exercise 5: Sort Three Itnegers
Problem: Input three inteegrs and output them in ascending order.
nums = [int(input(f"Enter number {i+1}: ")) for i in range(3)]
nums.sort()
print(nums)
Exercise 6: Fibonacci Sequence
Problem: Generate the nth Fibonacci number.
n = int(input())
a, b = 1, 1
for _ in range(n - 1):
a, b = b, a + b
print(a)
Exercise 7: List Copying
Problem: Demonstrate different list copying methods.
import copy
original = [1, 2, 3, 4, ['x', 'y']]
shallow1 = original[:]
shallow2 = copy.copy(original)
deep = copy.deepcopy(original)
original.append(99)
original[4].append('z')
print("Original:", original)
print("Shallow [:]:", shallow1)
print("Shallow copy():", shallow2)
print("Deep copy:", deep)
Exercise 8: Multiplication Table
Problem: Print the 9×9 multiplication table.
for i in range(1, 10):
for j in range(1, i + 1):
print(f"{i}*{j}={i*j:2}", end=" ")
print()
Exercise 9 & 10: Time Delay and Formatting
Problem: Pause execution for one second and display formatted time.
import time
for _ in range(4):
print(time.strftime('%Y-%m-%d %H:%M:%S'))
time.sleep(1)
Exercise 11: Rabbit Population Growth
Problem: Simulate rabbbit breeding where rabbits mature after 3 months.
months = int(input("Number of months: "))
newborn, one_month, two_months, adult = 1, 0, 0, 0
for m in range(months):
newborn, one_month, two_months, adult = adult + two_months, newborn, one_month, adult + two_months
total = newborn + one_month + two_months + adult
print(f"Month {m+1}: {total} pairs")
Exercise 12: Prime Numbers Between 100–200
Problem: Find all prime numbers in the range [101, 200].
import math
for num in range(101, 201):
for divisor in range(2, int(math.sqrt(num)) + 1):
if num % divisor == 0:
break
else:
print(num)
Exercise 13: Narcissistic Numbers
Problem: Print all 3-digit narcissistic (Armstrong) numbers.
for num in range(100, 1000):
digits = [int(d) for d in str(num)]
if sum(d ** 3 for d in digits) == num:
print(num)