Logic of using floor division and modulus for a different variable at different time - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Logic of using floor division and modulus for a different variable at different time (/thread-13815.html) |
Logic of using floor division and modulus for a different variable at different time - SB_J - Nov-01-2018 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) RE: Logic of using floor division and modulus for a different variable at different time - j.crater - Nov-01-2018 // 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). RE: Logic of using floor division and modulus for a different variable at different time - SB_J - Nov-01-2018 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! |