Preplp
Daily Python
Day 5Python~35 min

Loops

Repeat code with for and while

Track progress0/15 days
x

What you'll learn

forrange()whiletime.sleep()
  • Loop with for and range()
  • Repeat until a condition with while
  • Build a countdown timer step by step
  • Pause between prints with the time module

Lesson & examples

Read the code, then the explanation — try changing values before the project.

for loop with range
for i in range(5, 0, -1):
    print(i)

range(5, 0, -1) counts down: 5, 4, 3, 2, 1.

while loop
count = 0
while count < 5:
    print(count)
    count += 1
Countdown with pause
import time

for i in range(10, 0, -2):
    print(i)
    time.sleep(2)
print("Happy New Year!")

Today's exercise: Countdown Timer

Ask where to start counting down from, then print each second until zero.

Steps

  1. 1

    Import time

    Add import time at the top so you can pause between numbers.

  2. 2

    Get starting number

    Ask the user for an integer to start the countdown from.

  3. 3

    Count down with while

    While start > 0: print the number, sleep 1 second, subtract 1.

  4. 4

    Finish

    After the loop, print "Countdown Complete!"

Full project source — copy into a folder or use the zip above

starter.py
# Countdown Timer
import time

start = int(input("Enter the number to start the countdown from: "))

print("\n--- Countdown Begins ---")
while start > 0:
    print(start)
    time.sleep(1)
    start -= 1

print("Countdown Complete!")

Code explained

  1. 1.while vs for

    while repeats until start reaches 0 — good when you don't know the exact count upfront. You control start -= 1 each loop.

  2. 2.time.sleep(1)

    Pauses the program for 1 second so the countdown feels real. import time must be at the top of the file.

    Snippet
    time.sleep(1)

Sample output

Enter the number to start the countdown from: 5

--- Countdown Begins ---
5
4
3
2
1
Countdown Complete!

One rehearsal platform

Certification mocks, daily lessons, project labs, and in-browser drills

Structured for exam day and portfolio proof — timed tests, guided builds, and quick reps on one platform.