So why we write " while name:"
Instead of " while name=='': "
What are these Truthy and Falsey values?
And what are the rules for writing name:
Instead of name==''.
When you write
while expression: ...
python evaluates expression as a boolean value (which can only be True or False). The result is the same as using
bool(expression). Here are a few examples of using
>>> bool('') False >>> bool('hello') True >>> bool() False >>> bool([1, 2, 3]) True >>> bool(0) False >>> bool(7) True
>>> eggs = 6 >>> # means the same as: make yourself a box, write eggs on the side and store the number 6 in it >>> # = is called an assignment operator: assign a value to a variable >>> eggs == 5 False >>> # == is a logical comparision operator >>> # because we assigned the value 6 to eggs, the expression eggs == 5 returns False
Loops and conditional expressions will only execute the code inside them when their conditions are
True. Once their conditions result into
False, they will stop executing the code inside them.