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 Program to Print Colored Text to the Terminal
Python Program to Get the Last Element of the List
Python Program to Reverse a Number
Python Set issubset()
Python Set remove()
Python Set update()
Python Decorators
Python set()
Python sum()
Python divmod()
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
APIs in Node.js vs Python - A Comparison
Python range()
Python classmethod()
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python Function Arguments
Python Modules
Python Program to Multiply Two Matrices
Python len()
Python time Module
Python __import__()
Python String center()
Python List Comprehension
Python tuple()
Introduction to Machine Learning with Python - Andreas C.Muller & Sarah Guido
Python String rpartition()
Python String format()
Python Program to Convert String to Datetime
Python String lower()
Python Program to Make a Flattened List from Nested List
Python isinstance()
Python dict()