Digital Clock in Python
About the project:This program will be standalone script that displays the current time in your console, updating every second.
This is a straightforward and useful tool for extracting information from email addresses.
This project uses Python's built-in datetime and time modules to handle time, and os to clear the console, giving it a true "digital clock" feel.
How to use this Digital Clock:
- Run the Python script.
- Your console will immediately start displaying the current time, updating every second.
- To stop the clock, simply press Ctrl+C in your terminal or command prompt.
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 stepsStep 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)
# digital_clock.py
import datetime
import time
import os
def display_digital_clock():
"""
Displays a digital clock in the console, updating every second.
It attempts to clear the console for a cleaner display.
"""
print("--- Python Digital Clock ---")
print("Press Ctrl+C to stop the clock.")
while True:
# Clear the console screen for a cleaner display
# 'cls' is for Windows, 'clear' is for macOS/Linux
os.system('cls' if os.name == 'nt' else 'clear')
# Get the current time
current_time = datetime.datetime.now()
# Format the time as HH:MM:SS
# You can customize the format string as needed (e.g., "%I:%M:%S %p" for 12-hour with AM/PM)
formatted_time = current_time.strftime("%H:%M:%S")
# Print the formatted time
print(f"Current Time: {formatted_time}")
# Wait for 1 second before updating the display again
time.sleep(1)
# This ensures that display_digital_clock() is called only when the script is executed directly.
if __name__ == "__main__":
try:
display_digital_clock()
except KeyboardInterrupt:
# Handle Ctrl+C to exit gracefully
print("\nDigital Clock stopped. Goodbye!")