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 Deep Learning Cookbook - Indra den Bakker
Python zip()
Python Program Read a File Line by Line Into a List
Python Program to Randomly Select an Element From the List
Python Program to Slice Lists
APIs in Node.js vs Python - A Comparison
Python Dictionary values()
Python Program to Get the File Name From the File Path
Python for Programmers with introductory AI case studies - Paul Deitel & Harvey Deitel
Python str()
Python datetime
Python Set add()
Python Data Analytics with Pandas, NumPy and Matplotlib - Fabio Nelli
Python tuple()
Python Machine Learning Third Edition - Sebastian Raschka & Vahid Mirjalili
Python Set copy()
Python divmod()
Python compile()
Deep Learning in Python - LazyProgrammer
Python Numbers, Type Conversion and Mathematics
Python String join()
Python complex()
Python String rindex()
Python Program to Find the Square Root
Python issubclass()
Python String ljust()
Python 3 for Absolute Beginners - Tim Hall & J.P Stacey
Python min()
Deep Learning with Python - Francois Cholletf
Python strptime()
Python String center()
Python memoryview()