Here we will learn about time and How to convert seconds to hours, minutes, and seconds which we can do using three different ways like simple calculations, using built in function divmod(), and using the datetime Module.
Calculating Time in Different Ways
There are different ways to get convert the seconds into different parameters of time like minutes and hours. That we will discuss here.
Simple Calculation
This is the way to find the minutes and hours in a given second using simple calculations of division and multiplications or using direct formulas. To do so follow the code given below and get execute it as per you want
seconds = 12601 seconds = seconds % (24 * 3600) hour = seconds // 3600 seconds %= 3600 minutes = seconds // 60 seconds %= 60 print("%d:%02d:%02d" % (hour, minutes, seconds)
Here we simply divide the seconds by 60 to convert them into minutes and to convert the same into hours I simply divide the seconds by 3600 which is 60X60.
Using DivMod() function
It is a build function that can directly convert the given seconds into minutes or hours as per our choice to implement the same user in built function following the example code given.
seconds = 31045 minutes, seconds = divmod(seconds, 60) hours, minutes = divmod(minutes, 60) print("%d:%02d:%02d" % (hours, minutes, seconds))
Using Datetime Module
We can also use a very powerful in- built module DateTime to do the same task.
import datetime sec = 9506 convert = str( datetime. timedelta( seconds = sec)) print( convert)
As this is also a way to get the seconds converted into minutes or hours.
To learn more about the conversions of seconds into minutes and hours in python using different ways visit: Different ways to convert the seconds into minutes and hours.
Also to learn more about python programming concepts and solutions of the problems related to their visit: Some Basic Python Dictionary Programs-1.
And to learn more about different other programming languages visit: beta python programming languages Solutions