-1

name=''
eggs=0
pony=0.0
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==''.

4
Contributors
3
Replies
29
Views
1 Year
Discussion Span
Last Post by Sean Francis
2

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()

>>> bool('')
False
>>> bool('hello')
True
>>> bool([])
False
>>> bool([1, 2, 3])
True
>>> bool(0)
False
>>> bool(7)
True
1
>>> 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

Edited by ddanbe: rewrite

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.