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
  cmath.rect accepts a negative modulus JMB 2 304 Jan-17-2024, 08:00 PM
Last Post: JMB
  Get numpy ceil and floor value for nearest two decimals klllmmm 4 1,198 Jun-07-2023, 07:35 AM
Last Post: paul18fr
  Division questions Dionysis 5 990 Feb-14-2023, 02:02 PM
Last Post: Dionysis
  Floor approximation problem in algorithm gradlon93 3 901 Dec-14-2022, 07:48 PM
Last Post: Gribouillis
  Division by zero and value of argument Lawu 5 2,968 Jul-01-2022, 02:28 PM
Last Post: Lawu
  Floor division problem with plotting x-axis tick labels Mark17 5 2,047 Apr-03-2022, 01:48 PM
Last Post: Mark17
  Division calcuation with answers to 1decimal place. sik 3 2,091 Jul-15-2021, 08:15 AM
Last Post: DeaD_EyE
  Conceptualizing modulus. How to compare & communicate with values in a Dictionary Kaanyrvhok 7 3,904 Mar-15-2021, 05:43 PM
Last Post: Kaanyrvhok
Photo Locate Noise floor level for a spectral data in Python Ranjan_Pal 1 2,991 Dec-19-2020, 10:04 AM
Last Post: Larz60+
  Floor division return value Chirumer 8 3,696 Nov-26-2020, 02:34 PM
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