Python 2.7.6 help with modules

Chris Angelico rosuav at gmail.com
Sat Feb 8 19:56:06 EST 2014


On Sun, Feb 9, 2014 at 11:28 AM, Scott W Dunning <swdunning at cox.net> wrote:
> So, this is more like what you’re talking about?
>
>>>> first = number / 10
>>>> second = number % 10
>>>> last = first %10
>>>> rest = second / 10
>
> I feel stupid saying this and it’s probably because of the variables I’m using but I’m not sure what the above is doing.  For some reason it’s confusing me.  I used the number 55664, so the variable (first) is saying take 55664 and / it by 10, then (second) is saying take the original number divide it by 10 and spit out the remainder?  Then from there (last) is saying take the original number / it by 10 then take that number and divide it by 10 again and give me the remainder?  Then finally (rest) is saying take the remainder of the original number after dividing by ten and divide that number by 10? I guess I’m confused on the result I’m looking for.
>

Let's try them in the interactive interpreter:

>>> number = 16384
>>> number / 10
1638
>>> number % 10
4

The first division and modulo splits the last digit off the number,
and gives us the rest of the number. So here's what I'd name them:

>>> last_digit = number % 10
>>> all_but_last = number / 10

Now we can get the next digit:

>>> second_last = all_but_last % 10
>>> all_but_last_two = all_but_last / 10

And so on, through five more steps (at the end of which, the "all but"
will have nothing in it).

(Aside: notice that we're not using 'number' any more. So we can
simply put the quotient back into number, and we can make this a loop
that goes until there's nothing left:

>>> while number:
    print number % 10
    number = number / 10 # or: number /= 10

4
8
3
6
1


Et voila! That's how assembly language programmers display long
numbers. (Converting a single digit into an ASCII character is simply
a matter of adding 48 (hex 30), and then that can be written out.)

End of aside.)

So, going back to the units-of-time problem. We start with a total
number of seconds; note that this is a bit confusing, so let's call
that "total_seconds" and then we can use the name "seconds" to mean
just the seconds component, which will be less than 60. [1] Then we
need to get minutes, hours, and days; whatever remains will be weeks
(so if you want to count the time from the beginning of 1970 to now,
you'll have over two thousand weeks).

total_seconds = 1907043

Okay. How do we get the last digit, pretending that that's in base 60?
Because, if we treat that number as base 60, the last digit is the
number of seconds, and the rest is what we need to split up into
minutes, hours, etc.

seconds = total_seconds % 60
minutes_etcetera = total_seconds / 60

Carry on with that method - work out the number of minutes, and then
the "hours_etc" which has the rest. Then do the same to split off
hours, and then days. See how you go!

ChrisA

[1] For the purposes of this exercise, I'm pretending that this is
Unix time and has no leap seconds. Technically, when you write out
HH:MM:SS, the HH field can go from 00 to 23, the MM field can go from
00 to 59, and the SS field can go from 00 to 61 - yes, it's possible
to have *two* consecutive leap seconds, although this has never yet
happened. But for this, we're working in a system that has seconds
going from 00 to 59.



More information about the Python-list mailing list