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==''.

Recommended Answers

All 3 Replies

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
>>> 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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.