Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.016 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; '(at': 0.04; 'subject:Python': 0.06; '(so': 0.07; 'variables': 0.07; 'subject:help': 0.08; 'ascii': 0.09; 'modulo': 0.09; 'subject:modules': 0.09; 'cc:addr:python-list': 0.11; '23,': 0.16; '60,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'i\xe2\x80\x99m': 0.16; 'left:': 0.16; 'remainder': 0.16; 'seconds,': 0.16; 'seconds.': 0.16; 'spit': 0.16; 'splits': 0.16; 'which,': 0.16; 'language': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'bit': 0.19; 'split': 0.19; 'written': 0.21; 'feb': 0.22; '>>>': 0.22; 'saying': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'hours,': 0.24; 'cc:2**0': 0.24; 'purposes': 0.26; 'second': 0.26; 'header:In-Reply-To:1': 0.27; 'rest': 0.29; 'on,': 0.29; '[1]': 0.29; 'am,': 0.29; 'character': 0.29; 'unix': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'gives': 0.31; '>>>>': 0.31; 'division': 0.31; 'probably': 0.32; 'weeks': 0.32; "we're": 0.32; 'beginning': 0.33; 'guess': 0.33; 'programmers': 0.33; "i'd": 0.34; 'subject:with': 0.35; 'display': 0.35; 'problem.': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'minutes,': 0.36; 'next': 0.36; 'method': 0.36; 'possible': 0.36; 'seconds': 0.37; 'so,': 0.37; 'two': 0.37; 'whatever': 0.38; 'sure': 0.39; 'how': 0.40; 'skip:u 10': 0.60; 'number,': 0.60; 'matter': 0.61; 'simply': 0.61; 'first': 0.61; 'back': 0.62; "you'll": 0.62; 'name': 0.63; 'field': 0.63; 'more': 0.64; 'total': 0.65; 'finally': 0.65; 'talking': 0.65; 'number:': 0.66; '(first)': 0.84; 'about?': 0.84; 'confusing': 0.84; 'divide': 0.84; 'exercise,': 0.84; 'happened.': 0.84; 'it\xe2\x80\x99s': 0.84; 'or:': 0.84; '1970': 0.91; 'you\xe2\x80\x99re': 0.91; 'to:none': 0.92; 'scott': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type:content-transfer-encoding; bh=qLOnpJyhr0DexIt8wSAQoCF0+5ytGPj+I7kU7Dfs4UE=; b=NyLggMKkQp0FsYHmtZzPtopu84XuyChYf/bVTHCcgwUXR7923eXwtxsqDPtaVWgWz+ VEnfwie2saVMb5wRe8sJfM81Kl0oMmeu6Ug0sD2eID/CK4ImtdkPpN/OAe7W+WqsBMDs 3+8kyggaeu11eNb2c5Y8ffhiJ+MiFhmF4Lk+WRMLEZq9l3/y7aZXDehlKhQwlG+JlCgu N4bim6j8iRpAMpUoObHDLMV9c0vPo3FmWnyAXNySgcfCoMH6kIlmJ/gyA8F00d6dGOg5 tdcboVr8fW6Svk0+4MWS5TytgDFR+jlS9/obCPsaP7dzKiipVBZy5gzz6tBzyYRWV7he U6nA== MIME-Version: 1.0 X-Received: by 10.68.40.138 with SMTP id x10mr28872071pbk.8.1391907366703; Sat, 08 Feb 2014 16:56:06 -0800 (PST) In-Reply-To: <07C379F9-3BD4-4DC4-8FEF-BA3B0067A7B1@cox.net> References: <032F2E23-6983-4710-B087-C1771B66C3EF@cox.net> <635C857D-1F7A-4F95-B3A7-F1A3C69BF137@cox.net> <07C379F9-3BD4-4DC4-8FEF-BA3B0067A7B1@cox.net> Date: Sun, 9 Feb 2014 11:56:06 +1100 Subject: Re: Python 2.7.6 help with modules From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 94 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1391907848 news.xs4all.nl 2977 [2001:888:2000:d::a6]:36432 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:65701 On Sun, Feb 9, 2014 at 11:28 AM, Scott W Dunning wrote: > So, this is more like what you=E2=80=99re talking about? > >>>> first =3D number / 10 >>>> second =3D number % 10 >>>> last =3D first %10 >>>> rest =3D second / 10 > > I feel stupid saying this and it=E2=80=99s probably because of the variab= les I=E2=80=99m using but I=E2=80=99m not sure what the above is doing. Fo= r some reason it=E2=80=99s confusing me. I used the number 55664, so the v= ariable (first) is saying take 55664 and / it by 10, then (second) is sayin= g take the original number divide it by 10 and spit out the remainder? The= n 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 fin= ally (rest) is saying take the remainder of the original number after divid= ing by ten and divide that number by 10? I guess I=E2=80=99m confused on th= e result I=E2=80=99m looking for. > Let's try them in the interactive interpreter: >>> number =3D 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 =3D number % 10 >>> all_but_last =3D number / 10 Now we can get the next digit: >>> second_last =3D all_but_last % 10 >>> all_but_last_two =3D 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 =3D number / 10 # or: number /=3D 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 =3D 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 =3D total_seconds % 60 minutes_etcetera =3D 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.