Freedom Sale
Independence Day Special — Unlock the AI Path 70% off our most popular AI course · Limited time offer
--Days
--Hrs
--Min
--Sec
Claim Your Discount
✦ Beginner ⏱ 20 min

🍅 Build a Pomodoro Timer CLI with Python

🎯 What You'll Build

A command-line Pomodoro timer that cycles through work and break sessions, plays a sound alert, tracks completed sessions, and shows a daily productivity summary.

📋 What You'll Need

1

Core countdown function

Display a live countdown that updates in place on the same line.

import time, sys

def countdown(seconds, label):
    print()
    for remaining in range(seconds, 0, -1):
        mins = remaining // 60
        secs = remaining % 60
        sys.stdout.write(f'\r  ⏱  {label}: {mins:02d}:{secs:02d} remaining   ')
        sys.stdout.flush()
        time.sleep(1)
    sys.stdout.write(f'\r  ✅ {label} complete!                    \n')

# Test it
countdown(10, "Test session")
2

Full Pomodoro cycle with session tracking

Alternate between 25-minute work sessions and 5-minute breaks. Every 4 sessions, take a long break.

import os, datetime, json

WORK_MIN  = 25
SHORT_MIN = 5
LONG_MIN  = 15
LOG_FILE  = 'pomodoro_log.json'

def alert():
    # Cross-platform terminal bell
    print('\a', end='', flush=True)
    # Windows: additional beep
    if os.name == 'nt':
        import winsound
        winsound.Beep(1000, 500)

def log_session(session_type):
    today = datetime.date.today().isoformat()
    data = {}
    if os.path.exists(LOG_FILE):
        with open(LOG_FILE) as f:
            data = json.load(f)
    data.setdefault(today, {'work': 0, 'break': 0})
    data[today][session_type] += 1
    with open(LOG_FILE, 'w') as f:
        json.dump(data, f, indent=2)

def run_pomodoro(sessions=4):
    print(f"\n🍅 Pomodoro Timer — {sessions} work sessions\n")
    for i in range(1, sessions + 1):
        print(f"  Session {i}/{sessions}")
        input("  Press Enter to start work... ")
        countdown(WORK_MIN * 60, f"Work session {i}")
        alert()
        log_session('work')

        if i < sessions:
            break_min = LONG_MIN if i % 4 == 0 else SHORT_MIN
            break_type = "Long break" if i % 4 == 0 else "Short break"
            input(f"  Press Enter for {break_type} ({break_min} min)... ")
            countdown(break_min * 60, break_type)
            alert()
            log_session('break')

    print("\n🎉 All sessions complete! Great work today.")
    show_stats()

def show_stats():
    if not os.path.exists(LOG_FILE): return
    with open(LOG_FILE) as f:
        data = json.load(f)
    today = datetime.date.today().isoformat()
    if today in data:
        s = data[today]
        print(f"\n📈 Today: {s['work']} work sessions ({s['work'] * 25} minutes focused)")

run_pomodoro(sessions=4)
🍅 Pomodoro Timer — 4 work sessions

  Session 1/4
  Press Enter to start work...
  ⏱  Work session 1: 24:47 remaining
  ✅ Work session 1 complete!
  Press Enter for Short break (5 min)...
  ...

📈 Today: 4 work sessions (100 minutes focused)
💡 Tip: Set WORK_MIN = 1 for testing so you do not have to wait 25 minutes. The Pomodoro Technique was invented by Francesco Cirillo in the late 1980s — the original timer was tomato-shaped (pomodoro in Italian).

🎉 You Did It!

A 50-line script that could genuinely improve your productivity. Add desktop notifications using plyer (pip install plyer) for pop-up alerts instead of terminal beeps.

Found something wrong?

Spotted a bug, broken code, or something that doesn't look right? Tell us what's off and we'll fix it.