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 Program to Sort Words in Alphabetic Order
Deep Learning with Python - A Hands-on Introduction - Nikhil Ketkar
Python String count()
Python frozenset()
Python classmethod()
Python Program to Print Output Without a Newline
Python iter()
Python Multiple Inheritance
Python Custom Exceptions
Python datetime
Python Dictionary popitem()
Python Tuple
Python RegEx
Python Set add()
Python Dictionary keys()
Python frozenset()
Deep Learning with Python - Francois Cholletf
Python Program to Slice Lists
Python Program to Get the Last Element of the List
Python Program to Print all Prime Numbers in an Interval
Python List append()
Python List index()
Python object()
Python __import__()
Python Program to Find Armstrong Number in an Interval
Python Keywords and Identifiers
Python Program to Shuffle Deck of Cards
Python String join()
Python Program to Find Factorial of Number Using Recursion
Python for Programmers with introductory AI case studies - Paul Deitel & Harvey Deitel
Python List insert()
Python pass statement