Skip to Content
Course content

Problem-Solving Lab — 10 Python Exercises

Ten exercises that combine everything from this section. Try each for 10 minutes before reading the hint.

Exercises

  1. FizzBuzz: print 1–100; multiples of 3 → "Fizz", of 5 → "Buzz", of both → "FizzBuzz". Hint: check the combined condition first, or use a list of parts and "".join.
  2. Palindrome checker: return True if a string reads the same ignoring case, spaces and punctuation. Hint: filter with str.isalnum, then compare s == s[::-1].
  3. Word counter: read a text and return the 5 most frequent words with counts. Hint: dict.get(word, 0) + 1, then sorted(items, key=lambda kv: kv[1], reverse=True) or collections.Counter.
  4. Fibonacci: return the first N Fibonacci numbers, both as a list and as a generator. Hint: iterate with two variables a, b = b, a + b; yield for the generator.
  5. Duplicate remover: given a list, return it deduplicated while preserving order. Hint: list(dict.fromkeys(items)), or a set for seen membership in a loop.
  6. CSV reader summary: read sales.csv (date, product, amount) and print total and average amount per product. Hint: csv.DictReader + a dict of lists, or running totals with float(row["amount"]).
  7. Prime numbers: list all primes below N using a simple sieve. Hint: start from a list of True flags, cross out multiples of each prime up to √N.
  8. Password strength validator: score a password 0–4 for length ≥ 8, upper+lower, digit, symbol. Hint: sum boolean checks; use any(c.isupper() for c in pw).
  9. Contact book: dict-backed add/search/delete/list commands in a loop, persisted to JSON on exit. Hint: while True + input() dispatch, json.dump in a finally.
  10. Number guessing game: the program picks 1–100 and answers "higher/lower" until guessed; count attempts. Hint: random.randint, int(input()) wrapped in try/except for non-numeric input.

Deliverable

One lab01.py (or a lab01/ package) where each exercise is a function with a docstring, plus a small demo block that runs all ten.

Level: Beginner | Duration: ~1h. Ten small real exercises — from FizzBuzz to a CSV summarizer — each with a solution hint to unblock you.
Rating
0 0

There are no comments for now.