Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #110698

Re: Iteration, while loop, and for loop

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Michael Selik <michael.selik@gmail.com>
Newsgroups comp.lang.python
Subject Re: Iteration, while loop, and for loop
Date Tue, 28 Jun 2016 13:51:54 +0000
Lines 24
Message-ID <mailman.79.1467121926.2358.python-list@python.org> (permalink)
References <079ff112-8f18-4a27-938f-12e4e55a4a5b@googlegroups.com> <CAGgTfkOkhy8HMd2w-hTHMb4K4vsJhAhoAju2y=+c87Fj=ciZRA@mail.gmail.com> <00c301d1d140$af331e10$0d995a30$@gmail.com> <CAGgTfkMz0yxRXutEpaLFLoHYq1zuLzEfvVFnzBVYf4Bd3Nefbw@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de eTwLCudNPdhkhi4L+rrT4g+bpjfULrAwd7p5/EEbujrg==
Return-Path <michael.selik@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.012
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'part,': 0.09; 'preferable': 0.09; 'subject:while': 0.09; 'python': 0.10; 'message-----': 0.15; '2016': 0.16; 'loops': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'email addr:gmail.com&gt;': 0.18; 'input': 0.18; 'handles': 0.20; 'elements': 0.23; 'header:In-Reply-To:1': 0.24; 'not.': 0.27; 'message-id:@mail.gmail.com': 0.27; 'version,': 0.30; 'michael': 0.33; 'skip:- 10': 0.34; 'tue,': 0.34; 'file': 0.34; 'received:google.com': 0.35; 'sent:': 0.35; 'quite': 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'files': 0.38; 'from:': 0.39; 'well.': 0.40; 'to:addr:python.org': 0.40; 'is.': 0.63; 'times': 0.63; 'better.': 0.66
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=O1vU/8syNBPLqxUj6p9VGCyhLgjGJY+rG/t5LqOCa0s=; b=qAw/oxYoed1RmUWlhGDSWcQJFmkk2AJQdKaogZ2ZRok9n8/2Gf0udNt0NzpvY+jpWM yGeQr75VdEVbl7zaYZKuQzeojhERoou0Y0EANCFUyVTHUgFD+RyX9etXMERd/blOtzOY 2FSPV8zJAKUJhFkoq7/N459kHryERBIAvV8CARjmjka+iyGiYtXK0vi4g/ml+vVRWMcw Er5fGHf7lCPpYu/Q+7EzUmk+uMcIGQW3CmEmL3Tc4BpoX1LUT3XGmFUV4AtMgk00dmB7 ZVHznCwDYggiwcJjvbrAR/AYLinQEvCZHu70x1E+sNg2+i9vIWCcHdqSBZTY4fRs/WJ8 5qjw==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=O1vU/8syNBPLqxUj6p9VGCyhLgjGJY+rG/t5LqOCa0s=; b=hTEPzAUZPSuyycRRAjqwfh51ojaDlwaMOS8RbGDVMDIjg85C3ghWBim3Ox1wyhUqxv hq5Sq7r+M77xQfZhd9+BFp98Iazy7ktrIQsHvG6aVL/LnXVxUG+4xGWLS/1eWD8XnzBb v+QEnHnfx2/ZjGCJCbxjvJrlqINY7aC971UeOmuGeSKSlk89biMFUz3xaGrsypiol5IF bjVNvgTJTmTJXloakTedjYjNpFGYaES3+yXHcyStV9TNveN0pkSuaO87Nq0aIpyjZXYw Zm28eo5NuAJyQnNEdKg85H0jN3S5dQgsJNkhDht6PLCkMM14AOFEFYZfL8zNK5Ofbjj5 JiMg==
X-Gm-Message-State ALyK8tKVLoUEy1Ml3G0Hh5KreNMyFy2oREb/1p1DYltVRiUa5bH2640vlrP/kAC0YQIF1uzob8uqRVcWVNkM4w==
X-Received by 10.237.37.8 with SMTP id v8mr2007431qtc.28.1467121924022; Tue, 28 Jun 2016 06:52:04 -0700 (PDT)
In-Reply-To <00c301d1d140$af331e10$0d995a30$@gmail.com>
X-Content-Filtered-By Mailman/MimeDel 2.1.22
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.22
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID <CAGgTfkMz0yxRXutEpaLFLoHYq1zuLzEfvVFnzBVYf4Bd3Nefbw@mail.gmail.com>
X-Mailman-Original-References <079ff112-8f18-4a27-938f-12e4e55a4a5b@googlegroups.com> <CAGgTfkOkhy8HMd2w-hTHMb4K4vsJhAhoAju2y=+c87Fj=ciZRA@mail.gmail.com> <00c301d1d140$af331e10$0d995a30$@gmail.com>
Xref csiph.com comp.lang.python:110698

Show key headers only | View raw


On Tue, Jun 28, 2016 at 9:26 AM Joseph Lee <joseph.lee22590@gmail.com>
wrote:

>
> -----Original Message-----
> From: Michael Selik
> Sent: Tuesday, June 28, 2016 6:16 AM
>
> MS: You should not. Use the first version, it's much better. Python
> for-loops are preferable to while-loops.
>
> JL: 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.


Not knowing the number of elements is not a reason to prefer a while-loop.
A for-loop handles that quite well.

    with open('example.txt') as f:
        for line in f:
            print(line)

We often loop over files without knowing how big a file is.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Iteration, while loop, and for loop Elizabeth Weiss <cake240@gmail.com> - 2016-06-28 05:36 -0700
  Re: Iteration, while loop, and for loop Michael Selik <michael.selik@gmail.com> - 2016-06-28 13:15 +0000
    Re: Iteration, while loop, and for loop BartC <bc@freeuk.com> - 2016-06-28 14:29 +0100
      Re: Iteration, while loop, and for loop Michael Selik <michael.selik@gmail.com> - 2016-06-28 13:58 +0000
      Re: Iteration, while loop, and for loop Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-06-28 22:58 -0700
  Re: Iteration, while loop, and for loop BartC <bc@freeuk.com> - 2016-06-28 14:24 +0100
  RE: Iteration, while loop, and for loop "Joseph Lee" <joseph.lee22590@gmail.com> - 2016-06-28 06:26 -0700
  Re: Iteration, while loop, and for loop Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-06-28 16:42 +0300
  Re: Iteration, while loop, and for loop Michael Selik <michael.selik@gmail.com> - 2016-06-28 13:51 +0000
  Re: Iteration, while loop, and for loop Steven D'Aprano <steve@pearwood.info> - 2016-06-29 01:20 +1000
    Re: Iteration, while loop, and for loop Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-06-28 18:28 +0300
    Re: Iteration, while loop, and for loop Tim Chase <python.list@tim.thechases.com> - 2016-06-28 12:23 -0500
    Re: Iteration, while loop, and for loop Grant Edwards <grant.b.edwards@gmail.com> - 2016-06-28 17:58 +0000
    Re: Iteration, while loop, and for loop Ian Kelly <ian.g.kelly@gmail.com> - 2016-06-29 09:29 -0600
      Re: Iteration, while loop, and for loop Steven D'Aprano <steve@pearwood.info> - 2016-06-30 09:59 +1000
        Re: Iteration, while loop, and for loop Tim Chase <python.list@tim.thechases.com> - 2016-06-30 05:44 -0500
        Re: Iteration, while loop, and for loop Ian Kelly <ian.g.kelly@gmail.com> - 2016-06-30 07:04 -0600
        Re: Iteration, while loop, and for loop Ian Kelly <ian.g.kelly@gmail.com> - 2016-06-30 07:10 -0600
        Re: Iteration, while loop, and for loop jfong@ms4.hinet.net - 2016-06-30 19:12 -0700
  Re: Iteration, while loop, and for loop Veek M <vek.m1234@gmail.com> - 2016-10-27 14:35 +0530

csiph-web