Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #101331
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: True/False value testing |
| Date | 2016-01-07 11:48 +0100 |
| Organization | None |
| Message-ID | <mailman.42.1452163742.2305.python-list@python.org> (permalink) |
| References | <568e3fb4$0$659$426a74cc@news.free.fr> |
ast 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
But not the way you think:
>>> 0 == False
True
>>> 1 == True
True
>>> 2 == True
False
>>> 2 == False
False
0 equals False, 1 equals True, and any other integer equals neither, but
"is true in a boolean context", i. e.
>>> if 42: print("42 considered true")
...
42 considered true
> For string, "" is considered False and any other value True,
> but it doesn't work
In a similar way there is no string that equals True or False, but
the empty string "" is considered false in a boolean context while all other
strings are considered true:
>>> bool("")
False
>>> bool("whatever")
True
>>>> A = ""
>>>> A==False
> False
>>>> A==True
> False
>>>> A = 'Z'
>>>> A==False
> False
>>>> A==True
> False
>
>
> What happens ???
>
> thx
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