Will sub in for @appylpye as he is on vacation.
Have you studied the truth values, truthy and falsy? They have a bearing on what those all mean.
Any non-zero number has a truth value of, truthy which translates to boolean, True.
Any data structure when empty is falsy. When not empty, is truthy. Even strings behave this way. Essentially, any empty string or structure if falsy, signals they are empty.
>>> x = ''
>>> if x: print ("Not empty")
else: print ("Empty")
Empty
>>>
>>> x = 'a'
>>> if x: print ("Not empty")
else: print ("Empty")
Not empty
>>>
To recap the falsy values:
0
, ''
, ""
, ''''''
, """"""
, []
, {}
, ()
, and, None
.
Does False
fit into that list? Not really. It IS False
and needs no evaluation, unlike all those above.
Also note that in Python, and perhaps other languages, a boolean is also an integer, either 0
for False
or 1
for True
.
>>> 1 == True
True
>>> True == 1
True
>>> 0 == False
True
>>> False == 0
True
>>>
And.
>>> True + True
2
>>> True - True
0
>>> True * True
1
>>> True / True
1.0
>>> True // True
1
>>> True + False
1
>>> True * False
0
>>>
And now to throw some true weirdness your wayā¦
>>> def foo(a=[]):
if a: a += [a[-1]*2]
else: a += [1]
return a
>>> foo()
[1]
>>> foo()
[1, 2]
>>> foo()
[1, 2, 4]
>>> foo()
[1, 2, 4, 8]
>>>
This is truly coming from out in left field as likely none of it has come up yet, so ease up and take a breath. When you get to the unit on default values in functions, come back and take another look. At that time you will be better equipped and can expand on your knowledge with a Google search. This particular example (or one similar) comes up in the Intermediate Python 3 course, so donāt let this distract your progress.
>>> for _ in range(11):
print (foo())
[1]
[1, 2]
[1, 2, 4]
[1, 2, 4, 8]
[1, 2, 4, 8, 16]
[1, 2, 4, 8, 16, 32]
[1, 2, 4, 8, 16, 32, 64]
[1, 2, 4, 8, 16, 32, 64, 128]
[1, 2, 4, 8, 16, 32, 64, 128, 256]
[1, 2, 4, 8, 16, 32, 64, 128, 256, 512]
[1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024]
>>>