Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #101332
| Path | csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: True/False value testing |
| Date | Thu, 7 Jan 2016 21:49:46 +1100 |
| Lines | 94 |
| Message-ID | <mailman.43.1452163794.2305.python-list@python.org> (permalink) |
| References | <568e3fb4$0$659$426a74cc@news.free.fr> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| X-Trace | news.uni-berlin.de Z8K5PF+dPDI2Z669nD275gL7WPoJeF2oT5UsPYUf2LsA== |
| Return-Path | <rosuav@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'else:': 0.03; 'received:209.85.223': 0.03; 'true,': 0.04; 'false,': 0.07; 'cc:addr:python-list': 0.09; 'ast': 0.09; 'collections': 0.09; 'integer,': 0.09; 'integers': 0.09; 'question?': 0.09; 'thx': 0.09; 'python': 0.10; 'jan': 0.11; 'def': 0.13; 'thu,': 0.15; '"is': 0.16; '1).': 0.16; '2016': 0.16; 'boolean': 0.16; 'bug,': 0.16; 'footnote': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'intuition': 0.16; 'non-empty': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'obviously': 0.16; 'integer': 0.18; 'string,': 0.18; 'tells': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'fraction': 0.22; 'import': 0.24; 'header:In-Reply-To:1': 0.24; "doesn't": 0.26; 'compare': 0.27; 'message-id:@mail.gmail.com': 0.27; 'values': 0.28; 'fine': 0.28; 'decimal': 0.29; 'subject:/': 0.30; 'checks': 0.30; 'fixed': 0.31; '[1]': 0.32; 'point': 0.33; 'true.': 0.33; 'values.': 0.33; 'equal': 0.34; 'list': 0.34; 'received:google.com': 0.35; '???': 0.35; 'false': 0.35; 'quite': 0.35; 'but': 0.36; 'received:209.85': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'difference': 0.38; 'received:209': 0.38; 'represent': 0.38; 'does': 0.39; 'save': 0.60; 'your': 0.60; 'other.': 0.64; 'between': 0.65; "they're": 0.66; 'fact,': 0.67; 'special': 0.73; '9:36': 0.84; 'chrisa': 0.84; 'subject:value': 0.84; 'to:none': 0.91; 'do:': 0.91; 'subject:True': 0.93 |
| 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:cc :content-type; bh=Gyj7Sqd+9CCF4Y2Jl4pFxzY3wJQP45Memz5sys+yUvQ=; b=a+VIRmSqybXgQ47MbcCZUCszZELrAfONIloAAPjSXU/F2Nqt6L+KxRAoRhKtSPEhzH rBALIE81rWu1KZcZK2oXnMfcSgPFShNCkKpyinHWG3OpyhslMLp66KKAiL+dTqfyNMZU 40+6LCAtk0sqKTO1rgEiQ6sjIiOdpGg5IH2SxLzm4w12drL8QXiLKB4FBaRVnehUYSf+ NNeScWiO4ldSc3zeDlaleZCvwYo5CZr/n6KAzSs3mTjo0WbqsQ5K2sa7axx2JRpVsX5y wbGkEUX0pTdxZNuKbkPxiabDVuk/1sY9sGxHBA70jORwOrxAg8K5DvNqEjcANr+HpRcO JdBg== |
| X-Received | by 10.107.40.76 with SMTP id o73mr33722277ioo.157.1452163786522; Thu, 07 Jan 2016 02:49:46 -0800 (PST) |
| In-Reply-To | <568e3fb4$0$659$426a74cc@news.free.fr> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.20+ |
| 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> |
| Xref | csiph.com comp.lang.python:101332 |
Show key headers only | View raw
On Thu, Jan 7, 2016 at 9:36 PM, ast <nomail@invalid.com> wrote:
> Hello
>
> For integer, 0 is considered False and any other value True
>
>>>> A=0
>>>> A==False
>
> True
>>>>
>>>> A==True
>
> False
>>>>
>>>> A=1
>>>> A==False
>
> False
>>>>
>>>> A==True
>
> True
>
> It works fine
>
> For string, "" is considered False and any other value True,
> but it doesn't work
>
>>>> A = ""
>>>> A==False
>
> False
>>>>
>>>> A==True
>
> False
>>>>
>>>> A = 'Z'
>>>> A==False
>
> False
>>>>
>>>> A==True
>
> False
>
>
> What happens ???
>
> thx
There's a difference between "is considered false" and "is equal to
False". In Python, most collections are considered false if they are
empty:
def truthiness(x):
if x:
print(repr(x) + " is true")
else:
print(repr(x) + " is false")
truthiness([])
truthiness([1,2,3])
truthiness("")
truthiness("hello")
truthiness(0)
truthiness(73)
truthiness({})
truthiness({1:2, 3:4})
However, none of these will compare *equal* to the Boolean values True
and False, save for the integers 1 and 0. In fact, True is a special
form of the integer 1, and False is a special form of the integer 0;
they are considered to represent those numbers just as much as the
floating point values 1.0 and 0.0 do:
from decimal import Decimal
from fractions import Fraction
# Caveat, see footnote [1]
if Fraction(0, 1) == 0.0 == 0 == False == Decimal("0"):
print("See? All representations of zero are equal")
Intuition tells you quite obviously that the integers 1 and 73 are not
equal, but both of them are very definitely truthy values. In the same
way, a non-empty list is truthy, but it's not equal to True.
Does that answer your question?
ChrisA
[1] In Python 2.7, Decimal("0") != Fraction(0, 1). I suspect this is a
bug, as it's been fixed in Python 3; both of them are equal to the
integer 0, but they're not equal to each other. So the order of checks
in that demo is actually significant.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
True/False value testing "ast" <nomail@invalid.com> - 2016-01-07 11:36 +0100
Re: True/False value testing Peter Otten <__peter__@web.de> - 2016-01-07 11:48 +0100
Re: True/False value testing Chris Angelico <rosuav@gmail.com> - 2016-01-07 21:49 +1100
Re: True/False value testing Marko Rauhamaa <marko@pacujo.net> - 2016-01-07 13:19 +0200
Re: True/False value testing Chris Angelico <rosuav@gmail.com> - 2016-01-07 22:50 +1100
Re: True/False value testing Marko Rauhamaa <marko@pacujo.net> - 2016-01-07 14:07 +0200
Re: True/False value testing Chris Angelico <rosuav@gmail.com> - 2016-01-07 23:38 +1100
Re: True/False value testing "ast" <nomail@invalid.com> - 2016-01-07 12:11 +0100
csiph-web