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 zfill()
Python String maketrans()
Python Program to Count the Number of Digits Present In a Number
Python Set clear()
Python Program to Copy a File
Python String isnumeric()
Python Program to Reverse a Number
Python Program to Get Line Count of a File
Python Program to Remove Duplicate Element From a List
Python String lower()
Python String isalpha()
Python Program to Find ASCII Value of Character
Python Program to Check Prime Number
Python Package
Python Program to Access Index of a List Using for Loop
Python getattr()
Python Program to Slice Lists
Python String index()
Python Program to Create a Long Multiline String
Deep Learning in Python - LazyProgrammer
Python isinstance()
Python Numbers, Type Conversion and Mathematics
Python tuple()
Python oct()
Python String title()
Python Program to Print all Prime Numbers in an Interval
Python property()
Python Program to Differentiate Between del, remove, and pop on a List
Python Program to Print Hello world!
Python String rindex()
Python Program to Remove Punctuations From a String
Python Errors and Built-in Exceptions