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_sec
is decremented at the end of each iteration.
Related posts:
Python Data Structures and Algorithms - Benjamin Baka
Java – Create a File
Python hash()
Python id()
Python issubclass()
Python Anonymous / Lambda Function
Python while Loop
Python ord()
Python Program to Check Armstrong Number
Python List copy()
Python timestamp to datetime and vice-versa
Deep Learning with Python - Francois Chollet
Python compile()
Python Program to Print Output Without a Newline
Python input()
Python Dictionary fromkeys()
Statistical Methods for Machine Learning - Disconver how to Transform data into Knowledge with Pytho...
Python List index()
Python String rfind()
Python Strings
Python Multiple Inheritance
Python dir()
Python Dictionary copy()
Python Shallow Copy and Deep Copy
Python Deep Learning Cookbook - Indra den Bakker
Python Recursion
Python Program to Make a Simple Calculator
Python Set isdisjoint()
Python Program to Find the Factorial of a Number
Python tuple()
Python Program to Access Index of a List Using for Loop
Python Set symmetric_difference_update()