[Tutor] While truth

Danny Yoo dyoo at hashcollision.org
Tue May 20 20:00:41 CEST 2014


On Tue, May 20, 2014 at 1:25 AM, Ian D <duxbuz at hotmail.com> wrote:
> I was reading a tutorial that had these examples in it:
>
>
>>>> while False:
>   print("False is the new True.")
>
>
>>>> while 6:
>   print("Which numbers are True?")
>
>
> while -1:
>   print("Which numbers are True?")
>
>
> while 0:
>   print("Which numbers are True?")
>
> Unfortunately the author never explained these statements.


The statements above are trying to talk about what Python considers to
be "true".  In some languages, there is a single distinguished true
value.  Python chooses a broader definition that allows everything to
be considered true, with the exception of the following values:

    False
    None
    Numeric zero
    Empty collection
    Empty string

Reference: https://docs.python.org/3/reference/expressions.html#booleans

We care about what values are true, because they are the switch that
controls which way we're flowing through a conditional statement like
"if" or "while".

As people are pointing out, the examples above are a bit
disappointing.  They are demonstrating this with infinite while loops,
and that's probably not a great idea because the output will be so
overwhelming to be actively distracting.


More information about the Tutor mailing list