X-Received: by 10.129.27.3 with SMTP id b3mr27036591ywb.0.1458645821649; Tue, 22 Mar 2016 04:23:41 -0700 (PDT) X-Received: by 10.50.83.34 with SMTP id n2mr307242igy.2.1458645821590; Tue, 22 Mar 2016 04:23:41 -0700 (PDT) Path: csiph.com!usenet.blueworldhosting.com!feeder01.blueworldhosting.com!peer03.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!y89no8575907qge.0!news-out.google.com!pn7ni16534igb.0!nntp.google.com!av4no1670723igc.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.python Date: Tue, 22 Mar 2016 04:23:40 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=24.63.27.64; posting-account=Jod8CwoAAAD06WqLVFnlf8nIDonFoRWK NNTP-Posting-Host: 24.63.27.64 References: <56e44258$0$1598$c3e8da3$5496439d@news.astraweb.com> <8737rvxs89.fsf@elektro.pacujo.net> <56e7483d$0$1608$c3e8da3$5496439d@news.astraweb.com> <56f09973$0$1601$c3e8da3$5496439d@news.astraweb.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: Subject: Re: The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?) From: Ned Batchelder Injection-Date: Tue, 22 Mar 2016 11:23:41 +0000 Content-Type: text/plain; charset=ISO-8859-1 X-Received-Bytes: 3680 X-Received-Body-CRC: 293645853 Xref: csiph.com comp.lang.python:105463 On Tuesday, March 22, 2016 at 7:05:20 AM UTC-4, BartC wrote: > On 22/03/2016 01:01, Steven D'Aprano wrote: > > Pythonic code probably uses a lot of iterables: > > > > for value in something: > > ... > > > in preference to Pascal code written in Python: > > > > for index in range(len(something)): > > value = something[index] > > (Suppose you need both the value and its index in the loop? Then the > one-line for above won't work. For example, 'something' is [10,20,30] > and you want to print: > > 0: 10 > 1: 20 > 2: 30 ) Then you use enumerate: for index, value in enumerate(something): print("{}: {}".format(index, value)) Python has a number of iteration features that you don't find in other languages. You might find this introduction to them helpful: Loop Like a Native: http://nedbatchelder.com/text/iter.html > > > ... > > or worse: > > > > index = 0 > > while index < len(something): > > value = something[index] > > ... > > index += 1 > > > (I don't know where that while-loop idiom comes from. C? Assembly? Penitent > > monks living in hair shirts in the desert and flogging themselves with > > chains every single night to mortify the accursed flesh? But I'm seeing it > > a lot in code written by beginners. I presume somebody, or some book, is > > teaching it to them. "Learn Python The Hard Way" perhaps?) > > Are you suggesting 'while' is not needed? Not everything fits into a > for-loop you know! Steven wasn't saying 'while' is not needed. He was wondering about the idiom of manually maintaining an integer count of the number of times around a while loop ("I don't know where *that* while-loop idiom comes from"). While-loops are still useful in Python, but for-loops are much more common. --Ned.