Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Michael Selik 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: References: <079ff112-8f18-4a27-938f-12e4e55a4a5b@googlegroups.com> <00c301d1d140$af331e10$0d995a30$@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: 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>': 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <079ff112-8f18-4a27-938f-12e4e55a4a5b@googlegroups.com> <00c301d1d140$af331e10$0d995a30$@gmail.com> Xref: csiph.com comp.lang.python:110698 On Tue, Jun 28, 2016 at 9:26 AM Joseph Lee 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.