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 staticmethod()
Python round()
Python time Module
Python Set pop()
Python Program to Differentiate Between type() and isinstance()
Python help()
Python Dictionary fromkeys()
Python Program to Catch Multiple Exceptions in One Line
Python Program to Find Hash of File
Python Dictionary keys()
Python String isdecimal()
Python String casefold()
Python float()
Building Machine Learning Systems with Python - Willi Richert & Luis Pedro Coelho
Python Operators
Java Program to Implement the Program Used in grep/egrep/fgrep
Python Program to Concatenate Two Lists
Python repr()
Python Program to Calculate the Area of a Triangle
Python String rindex()
Python range()
Python Program to Find ASCII Value of Character
Python Program to Count the Number of Each Vowel
Python String isprintable()
Python Program to Display Calendar
Node.js vs Python for Backend Development
Python List extend()
Python int()
Introduction to Scientific Programming with Python - Joakim Sundnes
How to get current date and time in Python?
Python reversed()
Python Program to Parse a String to a Float or Int