In this example, you will learn to create a countdown timer.
To understand this example, you should have the knowledge of the following Python programming topics:
- Python while Loop
- Python divmod()
- Python time Module
Countdown time in Python
import time
def countdown(time_sec):
while time_sec:
mins, secs = divmod(time_sec, 60)
timeformat = '{:02d}:{:02d}'.format(mins, secs)
print(timeformat, end='\r')
time.sleep(1)
time_sec -= 1
print("stop")
countdown(5)
- The
divmod()method takes two numbers and returns a pair of numbers (a tuple) consisting of their quotient and remainder. end='\r'overwrites the output for each iteration.- The value of
time_secis decremented at the end of each iteration.
Related posts:
Natural Language Processing with Python - Steven Bird & Ewan Klein & Edward Loper
Python Operator Overloading
Python Variables, Constants and Literals
Python String title()
Python Program to Convert Decimal to Binary, Octal and Hexadecimal
Learning scikit-learn Machine Learning in Python - Raul Garreta & Guillermo Moncecchi
Python Dictionary update()
Python Program to Print Output Without a Newline
Python Dictionary copy()
Python Set copy()
Python Program to Find Hash of File
Python oct()
Debug a JavaMail Program
Deep Learning in Python - LazyProgrammer
Python id()
Python Program to Create a Long Multiline String
Python Matrices and NumPy Arrays
Python String islower()
Python Set issubset()
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
Python Program to Solve Quadratic Equation
Python max()
Python Program to Print Hello world!
Python File I/O Operation
Python Program to Safely Create a Nested Directory
Python hex()
Python Program to Extract Extension From the File Name
Deep Learning with Applications Using Python - Navin Kumar Manaswi
Python Program to Trim Whitespace From a String
Python complex()
Python String lstrip()
Python Strings