Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: "Joseph Lee" Newsgroups: comp.lang.python Subject: RE: Iteration, while loop, and for loop Date: Tue, 28 Jun 2016 06:26:30 -0700 Lines: 40 Message-ID: References: <079ff112-8f18-4a27-938f-12e4e55a4a5b@googlegroups.com> <00c301d1d140$af331e10$0d995a30$@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de tapciRJXCtpC0lrMR9NbLw2+TAU3D4fwjEyOc1IUZn9w== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.010 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'elegant': 0.07; 'indexing': 0.07; 'part,': 0.09; 'preferable': 0.09; 'subject:while': 0.09; 'python': 0.10; 'message-----': 0.15; '2016': 0.16; 'iteration,': 0.16; 'loops': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'skip:[ 60': 0.16; 'variable.': 0.16; 'wrote:': 0.16; 'input': 0.18; 'assign': 0.22; 'simpler': 0.22; 'cheers,': 0.22; 'code.': 0.23; 'second': 0.24; 'header:In- Reply-To:1': 0.24; 'not.': 0.27; 'actual': 0.28; 'looks': 0.29; 'loop,': 0.29; 'url:wikipedia': 0.29; 'array': 0.29; 'url:wiki': 0.30; 'url:mailman': 0.30; 'code': 0.30; 'version,': 0.30; 'useful': 0.33; 'url:python': 0.33; 'michael': 0.33; 'message- id:@gmail.com': 0.34; 'skip:- 10': 0.34; 'url:listinfo': 0.34; 'tue,': 0.34; 'received:google.com': 0.35; 'sent:': 0.35; 'question,': 0.35; 'something': 0.35; 'subject:': 0.35; 'item': 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'url:org': 0.36; '(and': 0.36; 'email addr:python.org': 0.36; 'to:addr:python- list': 0.36; 'subject:: ': 0.37; 'charset:us-ascii': 0.37; 'wanted': 0.37; 'someone': 0.38; 'hi,': 0.38; 'why': 0.39; 'url:en': 0.39; 'from:': 0.39; 'url:mail': 0.40; 'to:addr:python.org': 0.40; 'more': 0.63; 'different': 0.63; 'times': 0.63; 'better.': 0.66; 'python-list': 0.66; 'email name :python-list': 0.67; 'elizabeth': 0.72; 'fragment': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:references:in-reply-to:subject:date:message-id:mime-version :content-transfer-encoding:thread-index:content-language; bh=SMbONemmp2We+mqUiHRxwH0KTGBC5DYWznJ4UGtt0kA=; b=l8U6Ojbk/JeOWd+/Gg+FcvCFo3/Z8d2Ld+WhSR3E2g0UGqoNtBq5JxcR5vJcCI3ZwY IJnKfaB4tOH3qSgSZE/wRjF/pUUdN/7mCmMUEK5CbStqbZb9Lm1RYkaMNmXht/UH2Ztv ve/hlkVZYDXBbfKCE9a7jZBWAs9fKNNBSdSTDaaZZp+21zT7Vawp5o0nNcrmUQ4MDuuk +6vJqG+5UJ30B5QvM1lQwTnFEE1rZ6I5T3khNC+8uSRoECF1AHippoXGYXQ63US0w1vH xXEhSYz4Kk4pNyCklWgJUGjRFMz4oJGWdcrjhP2u9iY5grJpXEyB2tg6zu83KvjuKCQS 3eNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:references:in-reply-to:subject:date :message-id:mime-version:content-transfer-encoding:thread-index :content-language; bh=SMbONemmp2We+mqUiHRxwH0KTGBC5DYWznJ4UGtt0kA=; b=iPYZwIXBjEkNhGHlzhGTN2LaJEC/rxvHe5acPv5iyHRn59eqyesEkb/OOsGxwDuMOO L2TIkm2i9QfsxIfl3ui4lO6GCpewgrYeVxhN+d6e71LBt+3PxSzGJptFKa4y7UrRbeRn UYlRhp/2XCeROKzdfN4fm5E3KYeE2mNC7eS6r2TnZiuhMdvnBofSLUsWsUXAI/xX80qe WQStic5T51eyVPqwGVkQwJis2Nt4VtUGvn5ngDrAea4BOMnPhC5pwv1twrxBYY48iZKq BNDOuF11aIS8zk828gCAqO5szlj9QZH87cIFQdFEngdg1l69+tkTlLWYTGqmTLlHtb+J p0Cg== X-Gm-Message-State: ALyK8tLBsalh2ziS897WDKZMDlA5xGRFOllI9bpH6ur+Z+uElJFU4x0Dli95qqFvjHRU5Q== X-Received: by 10.157.11.59 with SMTP id a56mr1772793ota.128.1467120392220; Tue, 28 Jun 2016 06:26:32 -0700 (PDT) In-Reply-To: X-Mailer: Microsoft Outlook 15.0 Thread-Index: AQDNHn+bXXAM/wczACBDRZZDJib4KwJAOAeYofZBnDA= Content-Language: en-us X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <00c301d1d140$af331e10$0d995a30$@gmail.com> X-Mailman-Original-References: <079ff112-8f18-4a27-938f-12e4e55a4a5b@googlegroups.com> Xref: csiph.com comp.lang.python:110692 Hi, Answers inline. -----Original Message----- From: Python-list [mailto:python-list-bounces+joseph.lee22590=gmail.com@python.org] On Behalf Of Michael Selik Sent: Tuesday, June 28, 2016 6:16 AM To: Elizabeth Weiss ; python-list@python.org Subject: Re: Iteration, while loop, and for loop On Tue, Jun 28, 2016 at 8:41 AM Elizabeth Weiss wrote: > I do not understand the second code. What is counter? > It looks like someone wanted to make a loop induction variable. https://en.wikipedia.org/wiki/Induction_variable JL: Or access the array item via indexing. > Why do we use this code if we can use the simpler for loop? > You should not. Use the first version, it's much better. Python for-loops are preferable to while-loops. JL: Indexing is useful if one wishes to assign something different to the position represented by container[index]. But if you want a more elegant way to access the actual object in question, then using the first fragment is better (and more readable). For the most part, for loops are better, but there are times when while loops are preferable such as when you don't know the size of the input beforehand. Cheers, Joseph - https://mail.python.org/mailman/listinfo/python-list