[Tutor] While truth

C Smith illusiontechniques at gmail.com
Tue May 20 20:44:41 CEST 2014


You can test out a condition like this in IDLE like so:
while 6:
    print "yes its true"
    break


while 0:
    print "yes its true"
    break


while -1:
    print "yes its true"
    break


emptyList = []
while emtpyList:
    print "yes its true"
    break

This way you don't have to deal with an infinite loop.
It will print "yes its true" once, if it IS true and then "break" will
break you out of the loop.

On Tue, May 20, 2014 at 2:00 PM, Danny Yoo <dyoo at hashcollision.org> wrote:
> 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.
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list