Python Forum
Logic of using floor division and modulus for a different variable at different time
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Logic of using floor division and modulus for a different variable at different time
#1
Hi,

In the below code(Code_1), please help me understand the logic of dividing minutes and seconds by 10. The second program(Code_2) also gives the same result. But the below program's logic, I'm unable to understand.

Code_1
seconds=eval(input("Please enter the number of seconds: "))
hours=seconds//3600
seconds=seconds%3600
minutes=seconds//60
seconds=seconds%60
print(hours, ":", sep="", end="")
tens=minutes//10
ones=minutes%10
print(tens, ones, ":", sep="", end="")
tens=seconds//10
ones=seconds%10
print(tens, ones, sep="")

Code_2
seconds=eval(input("Please enter the number of seconds: "))
hours=seconds//3600
seconds=seconds%3600
minutes=seconds//60
seconds=seconds%60
print(hours,":",minutes,":",seconds)
Reply
#2
// operator is a floor division, so it will just keep the whole number part of the result and discard decimal part.
% is modulus operator, which returns a remainder when you divide two integers. e.g. 7 % 2 -> 1 ( 7 / 2 = 3, 1 remains to 7).
Knowing this, the code is quite easy to understand. I recommend you to take pen and paper, make up a number you will use for initial "seconds" parameter, and do the calculations line by line (of course you can execute single lines in python console too).
Reply
#3
Hi,
I did understand why he has used 3600 and 60 in Code_1 as well as Code_2. I didn't understand when he didn't end the program after these steps, why did he extend it by dividing with 10 and considering 10's and 1's places?

Thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Why was floor division used here? tmv 2 115 Jan-31-2026, 02:00 AM
Last Post: Pedroski55
  Division Mallard 9 891 Dec-17-2025, 03:47 PM
Last Post: Mallard
  division error nathanael 2 776 Dec-12-2025, 11:55 PM
Last Post: deanhystad
  cmath.rect accepts a negative modulus JMB 2 7,239 Jan-17-2024, 08:00 PM
Last Post: JMB
  Get numpy ceil and floor value for nearest two decimals klllmmm 4 4,896 Jun-07-2023, 07:35 AM
Last Post: paul18fr
  Division questions Dionysis 5 3,605 Feb-14-2023, 02:02 PM
Last Post: Dionysis
  Floor approximation problem in algorithm gradlon93 3 2,330 Dec-14-2022, 07:48 PM
Last Post: Gribouillis
  Division by zero and value of argument Lawu 5 10,682 Jul-01-2022, 02:28 PM
Last Post: Lawu
  Floor division problem with plotting x-axis tick labels Mark17 5 4,772 Apr-03-2022, 01:48 PM
Last Post: Mark17
  Division calcuation with answers to 1decimal place. sik 3 3,557 Jul-15-2021, 08:15 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020