while var, but var ==16 != true

Larry Bates larry.bates at websafe.com`
Mon Jul 14 00:27:11 EDT 2008


maestro wrote:
> why does this work?  "while p" = "while p != 0" ? 1 is True and 0 is
> false in python but other numbers have no boolean value so why doesnt
> it abort.
> 
> 
> 
>>>> p=16
>>>> p
> 16
>>>> while p:
> 	print p
> 	p -= 1
> 
> 
> 16
> 15
> 14
> 13
> 12
> 11
> 10
> 9
> 8
> 7
> 6
> 5
> 4
> 3
> 2
> 1
> 
> 
> i can also do:
> 
>>>> k=[]
>>>> while k:
> 	k.pop()
> 
> 
>>>> k=[1,2,3]
>>>> while k:
> 	k.pop()
> 
> 
> 3
> 2
> 1
> 
> 
> so obv while var means while not empty or why not zero but it isnt
> something youd guess unless youd beeen shown it.

Python manuals outline that 0, None, empty list, empty dictionary are False 
anything else evaluates to True.  This is all over this list as well.

-Larry



More information about the Python-list mailing list