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 Statement, Indentation and Comments
Python Program to Check If a List is Empty
Python Set isdisjoint()
Python next()
Python Functions
Python isinstance()
Python List pop()
Python String rfind()
Python Program to Find Sum of Natural Numbers Using Recursion
Python Program to Compute all the Permutation of the String
Python Program to Multiply Two Matrices
Python Package
Python Sets
Python List extend()
Python Machine Learning - Sebastian Raschka
Python String isspace()
Python reversed()
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
Python sorted()
Python frozenset()
Python Program to Add Two Matrices
Python Program to Trim Whitespace From a String
Python Input, Output and Import
Python Program to Illustrate Different Set Operations
Python Program to Find the Sum of Natural Numbers
Python String rindex()
Node.js vs Python for Backend Development
APIs in Node.js vs Python - A Comparison
Python Program to Display Calendar
Python Type Conversion and Type Casting
Python object()