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 String isupper()
Python oct()
Python Set intersection()
Python Program to Print the Fibonacci sequence
Python String isprintable()
Python Program to Add Two Matrices
Python Program to Convert String to Datetime
Python String isidentifier()
Python Set pop()
Python Machine Learning Second Edition - Sebastian Raschka & Vahid Mirjalili
Python Sets
Python help()
Python Program to Randomly Select an Element From the List
Python Program to Find the Largest Among Three Numbers
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
Python Tuple
Python Program to Check Armstrong Number
Python reversed()
Python List reverse()
Python String index()
Python List insert()
Python chr()
Python Program to Get Line Count of a File
Python Set remove()
Python any()
Python min()
Python Program to Solve Quadratic Equation
Python dict()
Python Program to Trim Whitespace From a String
Deep Learning with Python - Francois Chollet
Python @property decorator
Python pow()