Truthiness
Picture checking your mailbox. You do not care what the letters say: all that matters is whether anything is in there. An empty mailbox means "no mail". A mailbox with a single sheet means "there is mail", even if the sheet says "nothing".
The condition after if does not have to be a comparison or a value of type bool. Python accepts any value there and looks at it the way you look at the mailbox: is it empty or not.
name = ""
if name:
print("Hello,", name)
else:
print("no name") # no nameLet's go through the example line by line:
name = ""puts an empty string into the variable: a mailbox with no letters.if name:asks whether the string has at least one character. It has none, and the condition counts as false.- So the
elsebranch runs, and the program printsno name.
This rule is called the truthiness of a value: every value in a condition behaves either as True or as False.
Falsy values
Among the types you already know, five values are falsy:
False;None;- the zeros
0and0.0; - the empty string
"".
Every other value is truthy: any nonzero number, negative ones included, and any non-empty string. A string of one space " " is not empty. The string "0" is not empty either: it holds one character, the digit zero.
For numbers the mailbox analogy does not quite work. The number 0 is not "empty", it is an ordinary number. Numbers have a rule of their own: only zero is falsy, while -1 and 0.5 are truthy.
Interviewers often ask which values are falsy. Answer by type: for numbers it is zero, for strings the empty string, and False and None are falsy too.
The bool function
The bool() function shows how a value behaves in a condition:
print(bool(0), bool(-1), bool(0.0)) # False True False
print(bool(""), bool(" "), bool("0")) # False True True
print(bool(None), bool("False")) # False TrueLet's go through the lines:
0and0.0are falsy, while-1is truthy: for numbers only zero is falsy.- The empty string is falsy, while the strings
" "and"0"are truthy: each holds a character. Noneis falsy, while the string"False"is truthy.
bool("False") gives True: the function does not read the text of the string, it only checks whether the string is empty. This is that sheet saying "nothing" in the mailbox. The condition if name: checks the value by the same rule as bool(name).
Checking with not
Writing if name: is shorter than if name != "": and means the same for strings. The opposite check is written with not: the condition not name is true when name is falsy. This is handy for a value that may turn out to be None:
discount = None
if not discount:
print("no discount") # no discountThis check has a downside. Both None and 0 are falsy, so with discount = 0 the program also prints no discount. When zero must be told apart from a missing value, write a comparison, not a truthiness check: if discount == 0: runs only for zero.
A fallback value with or
As you already know from the operators theme, and and or follow the same rules. The expression name or "guest" returns name if the string is non-empty and "guest" if it is empty. This is how a fallback value is given for when there is no real one: an empty name turns into "guest".
Common mistake
A number arrived as a string and is checked without a conversion. This happens with any data a program receives as text:
count = "0"
if count: # ✗
print("has items") # has itemsThere is no error, but the program prints has items. The string "0" is not empty, so it is truthy, even though the number in it is zero. Convert the string to a number first: the condition if int(count): is false.
Key points
- In a condition any value behaves as
TrueorFalse, andbool(x)shows which. False,None, zero and the empty string are falsy, and every other value is truthy.- The strings
"0"and"False"are truthy: what matters is whether a string is empty, not its text. - When zero must be told apart from
None, write a comparison, notif not x:.
Subscribe to take the course
The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.