Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder7.xlned.com!news2.euro.net!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.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'subsequent': 0.05; 'stops': 0.07; '0.1': 0.09; '[1,': 0.09; 'integers': 0.09; 'wrong,': 0.09; 'python': 0.11; "wouldn't": 0.14; '*always*': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'wrote:': 0.18; 'thu,': 0.19; '>>>': 0.22; 'either.': 0.24; 'integer': 0.24; 'equivalent': 0.26; 'header:In-Reply-To:1': 0.27; 'message-id:@mail.gmail.com': 0.30; "d'aprano": 0.31; 'steven': 0.31; "i'd": 0.34; 'could': 0.34; "can't": 0.35; 'received:209.85': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'wrong': 0.37; 'too': 0.37; 'received:209': 0.37; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; '30,': 0.65; 'actually,': 0.84; 'end.': 0.84; 'this!': 0.93; '2013': 0.98 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:to :content-type; bh=QajGr57uyi8c9YH3G206G6oVw4LfNVGOuFkNH2PDKKg=; b=0ffuAyyVFTtaLeYCJet43ABdksaQ6IEeI2Uxwqfk7ADyqyRLpHtTTi2Ng831z6zxqq E/WTK8D2PqVh3/AYVLR0VWu50AebrBPH1VXq8HT5sCMLKyBbpNWk8cCXfeNCtQT+WTwz byoHOXOEo7pZSxmlzkbp4SVOopR3xL4h3Xw7yeo1gHNjipyeFb8wX4BZANuHOi0UMylh J9ycGXQcz06pJG9FiuK3sGZzZF7YSb6UfKt80b+4dIZWVMdbGdD8RjKFwPi7+17YyZ7+ CjF0dbl2PRpJVn7I1ul0q7uIfDqC3UeKbLKoOHXOK3LnVk/s8vu2YTkVRbdYa7+36ReE T8/A== MIME-Version: 1.0 X-Received: by 10.68.88.194 with SMTP id bi2mr6136953pbb.12.1369891343908; Wed, 29 May 2013 22:22:23 -0700 (PDT) In-Reply-To: <51a6df59$0$11118$c3e8da3@news.astraweb.com> References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> <04b90c02-833a-4bad-88ad-ab71178b8f79@googlegroups.com> <51a6df59$0$11118$c3e8da3@news.astraweb.com> Date: Thu, 30 May 2013 15:22:23 +1000 Subject: Re: Short-circuit Logic From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 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: 30 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1369891346 news.xs4all.nl 15983 [2001:888:2000:d::a6]:56439 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:46435 On Thu, May 30, 2013 at 3:10 PM, Steven D'Aprano wrote: > # Wrong, don't do this! > x = 0.1 > while x != 17.3: > print(x) > x += 0.1 > Actually, I wouldn't do that with integers either. There are too many ways that a subsequent edit could get it wrong and go infinite, so I'd *always* use an inequality for that: x = 1 while x < 173: print(x) x += 1 Well, in Python I'd use for/range, but the equivalent still applies. A range() is still based on an inequality: >>> list(range(1,6)) [1, 2, 3, 4, 5] >>> list(range(1,6,3)) [1, 4] Stops once it's no longer less than the end. That's safe, since Python can't do integer wraparound. ChrisA