Pomodoro Timer Project (code) in Python

← Back to Projects

Pomodoro Timer in Python.

About the project: This is a Python project for a Pomodoro Timer. This is standalone script that runs in your console.

This program will help you implement the Pomodoro Technique, which uses a timer to break down work into focused intervals (traditionally 25 minutes) separated by short breaks.

How to use this program:

To use this Pomodoro Timer, simply save the code as a Python file and run it from your terminal:

  • Save the code: Save the code above as a Python file (e.g., pomodoro_timer.py).
  • Run the script: Open your terminal or command prompt, navigate to the directory where you saved the file, and run python pomodoro_timer.py .

  • The timer will guide you through work and break sessions.
  • The script includes a winsound library to play a beep on Windows when a session ends.
  • For other operating systems like macOS or Linux, I've added comments suggesting alternative commands you could use.

Project Level: Intermediate

You can directly copy the below snippet code with the help of green copy button, paste it and run it in any Python editor you have.

Steps: Follow these steps

Step 1: Copy below code using green 'copy' button.

Step 2: Paste the code on your chosen editor.

Step 3: Save the code with filename and .py extention.

Step 4: Run (Press F5 if using python IDLE)




# pomodoro_timer.py

import time
import os
import winsound # For Windows, use this for a simple beep sound.
# On macOS, you could use a command like 'afplay /System/Library/Sounds/Tink.aiff &'
# On Linux, you could use 'aplay /usr/share/sounds/freedesktop/stereo/complete.oga &'

def play_sound():
    """
    Plays a simple system beep sound.
    This function is specific to Windows.
    """
    try:
        # Play a sound at 500 Hz for 1 second
        winsound.Beep(500, 1000)
    except:
        # Fallback for other operating systems or environments
        pass

def clear_console():
    """Clears the console screen."""
    os.system('cls' if os.name == 'nt' else 'clear')

def main():
    """
    Main function to run the Pomodoro Timer.
    """
    print("--- Python Pomodoro Timer ---")
    print("The Pomodoro Technique helps you stay focused.")
    
    # Timer intervals in seconds
    pomodoro_duration = 25 * 60  # 25 minutes
    short_break_duration = 5 * 60 # 5 minutes
    long_break_duration = 15 * 60 # 15 minutes
    
    cycle_count = 0
    
    while True:
        cycle_count += 1
        
        # Work session
        print(f"\n--- Pomodoro Cycle {cycle_count} ---")
        print(f"Starting work session (25 minutes).")
        input("Press Enter to begin...")
        
        start_time = time.time()
        while time.time() - start_time < pomodoro_duration:
            elapsed_time = int(time.time() - start_time)
            remaining_time = pomodoro_duration - elapsed_time
            minutes = remaining_time // 60
            seconds = remaining_time % 60
            
            clear_console()
            print(f"Work Session: {minutes:02}:{seconds:02}")
            time.sleep(1)
            
        print("\nWork session complete!")
        play_sound()
        
        # Determine break type
        if cycle_count % 4 == 0:
            # Long break after 4 cycles
            break_duration = long_break_duration
            break_message = f"Long break (15 minutes)."
        else:
            # Short break
            break_duration = short_break_duration
            break_message = f"Short break (5 minutes)."
        
        # Break session
        print(f"Starting {break_message}")
        input("Press Enter to begin break...")
        
        start_time = time.time()
        while time.time() - start_time < break_duration:
            elapsed_time = int(time.time() - start_time)
            remaining_time = break_duration - elapsed_time
            minutes = remaining_time // 60
            seconds = remaining_time % 60
            
            clear_console()
            print(f"Break Session: {minutes:02}:{seconds:02}")
            time.sleep(1)
            
        print("\nBreak complete!")
        play_sound()
        
        continue_choice = input("Do you want to continue to the next cycle? (yes/no): ").strip().lower()
        if continue_choice != 'yes':
            print("Exiting Pomodoro Timer. Goodbye!")
            break

# This ensures that main() is called only when the script is executed directly.
if __name__ == "__main__":
    main()





← Back to Projects