while var, but var ==16 != true

M

maestro

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.


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.pop()

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.
 
T

Tim Roberts

maestro said:
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.

Because your statement is incorrect. Everything has a boolean value in
Python. 0, None, False, '' (empty string), [] (empty list), () (empty
tuple), and {} (empty dictionary) all have a False value. Everything else
has a True value.

Python didn't even have a boolean type (True and False) until rather
recently (2.2?).

This is a very handy feature, and it's one of the things I love about
Python.
so obv while var means while not empty or why not zero but it isnt
something youd guess unless youd beeen shown it.

It's clearly stated in the documentation. I don't know how you concluded
that True and False were the only boolean values.
 
J

John Machin

maestro said:
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.

Because your statement is incorrect.  Everything has a boolean value in
Python.  0, None, False, '' (empty string), [] (empty list), () (empty
tuple), and {} (empty dictionary) all have a False value.  Everything else
has a True value.

Not quite; for example:

According to section 5.10 of the Reference Manual:
"""
In the context of Boolean operations, and also when expressions are
used by control flow statements, the following values are interpreted
as false: False, None, numeric zero of all types, and empty strings
and containers (including strings, tuples, lists, dictionaries, sets
and frozensets). All other values are interpreted as true.
"""

... and for the definition for what a user-written class needs to do,
see section 3.4.1:
"""
__nonzero__( self)

Called to implement truth value testing, and the built-in operation
bool(); should return False or True, or their integer equivalents 0 or
1. When this method is not defined, __len__() is called, if it is
defined (see below). If a class defines neither __len__() nor
__nonzero__(), all its instances are considered true.
"""

Cheers,
John
 
B

bruno.desthuilliers

Everything has a boolean value in
Python. 0, None, False, '' (empty string), [] (empty list), () (empty
tuple), and {} (empty dictionary) all have a False value. Everything else
has a True value.

Unless the author of the class specified it otherwise (implementing
the appropriate __magic_method__).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,044
Latest member
RonaldNen

Latest Threads

Top