A certainl part of an if() structure never gets executed.

Jussi Piitulainen jpiitula at ling.helsinki.fi
Fri Jun 14 05:21:17 EDT 2013


Nick the Gr33k writes:

> On 14/6/2013 11:28 πμ, Jussi Piitulainen wrote:
> 
> >>>> 'Parker' and 'May' and '2001'
> > '2001'
> 
> But why?
> 
> that expression should return True since all stings are not empty.

It returns a value that counts as true in a conditional statement or
expression:

>>> if '2001': print('got a truish value')
... else: print("didn't")
... 
got a truish value

>>> if '': print('got a truish value')
... else: print("didn't")
... 
didn't

Zeroes and empty things tend to count as false in Python, other values
as true. The values are tested as is, not coerced to a boolean first,
so the value that decides the value of the whole expression is the
value of the whole expression.

>>> '' and whatever
''
>>> False and whatever
False
>>> 0 and whatever
0
>>> 1 and whatever
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
NameError: name 'whatever' is not defined



More information about the Python-list mailing list