1

I'm learning about functions at the moment, and this one came up as an example:

def a(x, y, z):
     if x:
         return y
     else:
         return z

I'm not exactly sure what if x actually means, though. x is just a parameter, not a statement, so how can it be true or false?

Steve Smith
  • 101
  • 1
  • 7
  • 2
    http://stackoverflow.com/questions/20420934/python-booleans-if-x-vs-if-x-true-vs-if-x-is-true – blacksite Nov 19 '16 at 18:33
  • The condition is *never* a statement. A statement is an action such as `y = 0`. The condition is an *expression*, e.g. `len(my_list) > 1`, which has some value. `x` is also an expression. – Alex Hall Nov 19 '16 at 18:39

4 Answers4

6

It checks if x is "truthy", which means that it shouldn't be :

  • None
  • False
  • Zero
  • Empty

As an example :

if 1 and True and "string" and ["list"] and {'key' : 'value'} and\
    not None and not 0 and not False and not "" and not [] and not {}:
  print "I understood truthiness!"
# => I understood truthiness!
Eric Duminil
  • 52,989
  • 9
  • 71
  • 124
  • Why, then, if I type `2 == True`, does the console output `False`? – Steve Smith Nov 19 '16 at 18:39
  • 1
    2 is not `True`, it is only "truthy" : `bool(2)==True` – Eric Duminil Nov 19 '16 at 18:41
  • 2
    @SteveSmith: Because `True`, when evaluated as an integer, means `1`, and `1` is not the same as `2`. An operation between an integer and a logical value (Boolean) is usually treated as an operation between integers. To convert an integer such as `2` to a logical value, use `bool(2)`, which evaluates to `True`, while `bool(0)` is `False`. – Rory Daulton Nov 19 '16 at 18:42
3

In python, everything has a truth value associated with it. For example, all numbers except 0 are truthy (evaluate to True in a conditional statement), and 0 is falsey (evaluates to False in a conditional statement). This is because of the __bool__ method in the each class.

For example:

In [140]: def f(x,y,z):
   .....:     if x:
   .....:         return y+z
   .....:     else:
   .....:         return y-z
   .....:     

In [141]: f(True, 3, 4)
Out[141]: 7

In [142]: f(False, 3, 4)
Out[142]: -1

In [143]: f(1, 3, 4)
Out[143]: 7

In [144]: f(2, 3, 4)
Out[144]: 7

In [145]: f(863, 3, 4)
Out[145]: 7

In [146]: f(-24, 3, 4)
Out[146]: 7

In [147]: f(0, 3, 4)
Out[147]: -1

Similarly, empty collections (lists, sets, dictionaries, strings, tuples, etc) are falsey, while non-empty collections are truthy.

inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
2

If only a variable is given as the condition in an if statement, the condition will be evaluated by Python's standard truth testing procedure. You can try the method out for yourself in the console with the bool method, which maps directly to it. Here are some examples:

>>> bool(1)
True
>>> bool(0)
False
>>> bool([])
False
>>> bool([1,2,3])
True
J. P. Petersen
  • 4,871
  • 4
  • 33
  • 33
1

In Python, any non-zero value, non-empty string/list/set, etc is considered as True where as None, empty-string/list/set etc and 0 are considered as False by if. This evalution is done by the bool(). Below is the illustration.

my_list = ['a', 100, None, '', 0, {}, {'a': 1}, [], ['a']]
for item in my_list:   # Iterate over each value and checks the condition
    if item:
        print item, ": True", ", Bool value: ", bool(item)
    else:
        print item, ": False", ", Bool value: ", bool(item)

which prints (explicitly formatted for readability):

a :        True ,       Bool value:  True
100 :      True ,       Bool value:  True
None :     False ,      Bool value:  False
 :         False ,      Bool value:  False
0 :        False ,      Bool value:  False
{} :       False ,      Bool value:  False
{'a': 1} : True ,       Bool value:  True
[] :       False ,      Bool value:  False
['a'] :    True ,       Bool value:  True

As you see, execution of if is mapped to the bool() value.

Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126