Python Program to Convert Seconds to Hours and Minutes
You can convert seconds to hours and minutes in Python by using integer division and modulus operators like this:
hours = seconds // 3600 and minutes = (seconds % 3600) // 60.Examples
Input3600
Output1 hour(s) and 0 minute(s)
Input7265
Output2 hour(s) and 1 minute(s)
Input59
Output0 hour(s) and 0 minute(s)
How to Think About It
To convert seconds into hours and minutes, first find how many full hours fit into the total seconds by dividing by 3600 (seconds in an hour). Then find the leftover seconds after removing hours and convert those into minutes by dividing by 60. This breaks down the total seconds into understandable time units.
Algorithm
1
Get the total number of seconds as input.2
Calculate hours by dividing seconds by 3600 using integer division.3
Calculate remaining seconds after hours using modulus operator.4
Calculate minutes by dividing the remaining seconds by 60 using integer division.5
Print or return the hours and minutes.Code
python
seconds = int(input("Enter seconds: ")) hours = seconds // 3600 minutes = (seconds % 3600) // 60 print(f"{hours} hour(s) and {minutes} minute(s)")
Output
Enter seconds: 7265
2 hour(s) and 1 minute(s)
Dry Run
Let's trace the input 7265 seconds through the code
1
Input seconds
seconds = 7265
2
Calculate hours
hours = 7265 // 3600 = 2
3
Calculate remaining seconds
remaining = 7265 % 3600 = 65
4
Calculate minutes
minutes = 65 // 60 = 1
5
Print result
"2 hour(s) and 1 minute(s)"
| Variable | Value |
|---|---|
| seconds | 7265 |
| hours | 2 |
| remaining seconds | 65 |
| minutes | 1 |
Why This Works
Step 1: Divide seconds by 3600
Using // gives the number of full hours because 3600 seconds equal one hour.
Step 2: Find leftover seconds
The modulus operator % finds remaining seconds after extracting full hours.
Step 3: Convert leftover seconds to minutes
Dividing leftover seconds by 60 using // gives full minutes.
Alternative Approaches
Using divmod function
python
seconds = int(input("Enter seconds: ")) hours, remainder = divmod(seconds, 3600) minutes, _ = divmod(remainder, 60) print(f"{hours} hour(s) and {minutes} minute(s)")
divmod returns quotient and remainder in one step, making code cleaner and easier to read.
Using datetime module
python
import datetime seconds = int(input("Enter seconds: ")) time = datetime.timedelta(seconds=seconds) hours = time.seconds // 3600 minutes = (time.seconds % 3600) // 60 print(f"{hours} hour(s) and {minutes} minute(s)")
Using datetime.timedelta is useful for more complex time calculations but adds extra import.
Complexity: O(1) time, O(1) space
Time Complexity
The program performs a fixed number of arithmetic operations regardless of input size, so it runs in constant time O(1).
Space Complexity
Only a few variables are used to store intermediate results, so space complexity is constant O(1).
Which Approach is Fastest?
All approaches run in constant time, but using divmod is slightly more readable and efficient by combining division and modulus.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Basic arithmetic operators | O(1) | O(1) | Simple and clear code |
| divmod function | O(1) | O(1) | Cleaner code with combined operations |
| datetime module | O(1) | O(1) | Complex time calculations, readability |
Use integer division
// to get whole hours and minutes without decimals.Beginners often forget to use integer division and get float results instead of whole hours and minutes.