PART 1 · PYTHON FROM ZERO · PRACTICE
Practice set 0: first steps
FreeLesson 22 of 78
Beginner questions that still come up as warm-ups in interviews. Try each one in your editor before reading the answer.
Q1Print the numbers 1 to 15, but "Fizz" for multiples of 3, "Buzz" for multiples of 5 and "FizzBuzz" for both.DADEEASY
Approach: Check the "both" case first, because 15 is also a multiple of 3. Use % for "is a multiple of".
out = []
for n in range(1, 16):
if n % 15 == 0:
out.append("FizzBuzz")
elif n % 3 == 0:
out.append("Fizz")
elif n % 5 == 0:
out.append("Buzz")
else:
out.append(str(n))
print(" ".join(out))Output
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz
Q2Find the largest and smallest amount without using max() or min().DADEEASY
Approach: Track the best so far (Lesson 8). Start from the first item, not from 0, or negative numbers break it.
amounts = [1198, 796, 1499, 1999, 747, 3498]
low = high = amounts[0]
for a in amounts[1:]:
if a > high:
high = a
if a < low:
low = a
print(low, high)Output
747 3498
Q3Count how many times each word appears in a sentence, ignoring case.DADEEASY
Approach: Lowercase, split into words, then count with a dict (Lesson 12).
text = "Data pipelines move data and data pipelines fail"
counts = {}
for word in text.lower().split():
counts[word] = counts.get(word, 0) + 1
print(counts)Output
{'data': 3, 'pipelines': 2, 'move': 1, 'and': 1, 'fail': 1}Q4Check whether a string reads the same backwards, ignoring spaces and case.DADEEASY
Approach: Keep only letters, lowercase them, compare with the reversed slice [::-1].
def is_palindrome(text):
letters = "".join(ch.lower() for ch in text if ch.isalpha())
return letters == letters[::-1]
print(is_palindrome("Never odd or even"), is_palindrome("Python"))Output
True False
Q5Given a list of order statuses, return each status with the share of orders it makes up, as a percentage.DAEASY
Approach: Count first, then divide each count by the total. Format with f-strings (Lesson 4).
statuses = []
with open("orders.csv") as f:
next(f)
for line in f:
statuses.append(line.strip().split(",")[3])
counts = {}
for s in statuses:
counts[s] = counts.get(s, 0) + 1
for s, n in counts.items():
print(f"{s:<10} {n:>2} {n / len(statuses):.1%}")Output
delivered 12 80.0% cancelled 2 13.3% returned 1 6.7%
Practice before you move on
- Solve Q1 to Q5 in the Try it editor before you read each solution, then compare.
- Change Q3 to count the words in the status column of orders.csv instead of a sentence.
Watch the free lesson
The same topic in the Free Lessons library, free with a sign-in.