top of page
learn_data_science.jpg

Data Scientist Program

 

Free Online Data Science Training for Complete Beginners.
 


No prior coding knowledge required!

Countdown timer in Python

In this article, we are going to see how we can write a countdown timer with python. Lets first review the whole bunch of code and look deeper into each piece.



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")

So basically, the first statement imports the time module which is shipped with Python. This module provides various time-related functions.


The function accepts an integer which is the time in seconds the functions starts counting down from. Out of the module's methods, the time.sleep function is used in this function to add an interval of 1 second before the next iteration is executed in the loop.


The other python built-in method used here is the divmod() function, which takes two numbers and returns a pair of numbers (a tuple) consisting of their quotient and remainder. In our code, it is specifically used to calculate the minutes and second part of the function's output at each iteration.





 
 
 

Commentaires


COURSES, PROGRAMS & CERTIFICATIONS

 

Advanced Business Analytics Specialization

Applied Data Science with Python (University of Michigan)

Data Analyst Professional Certificate (IBM)

Data Science Professional Certificate (IBM)

Data Science Specialization (John Hopkins University)

Data Science with Python Certification Training 

Data Scientist Career Path

Data Scientist Nano Degree Program

Data Scientist Program

Deep Learning Specialization

Machine Learning Course (Andrew Ng @ Stanford)

Machine Learning, Data Science and Deep Learning

Machine Learning Specialization (University of Washington)

Master Python for Data Science

Mathematics for Machine Learning (Imperial College London)

Programming with Python

Python for Everybody Specialization (University of Michigan)

Python Machine Learning Certification Training

Reinforcement Learning Specialization (University of Alberta)

Join our mailing list

Data Insight participates in affiliate programs and may sometimes get a commission through purchases made through our links without any additional cost to our visitors.

bottom of page