Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Basics (2015) Logic in Python Around and Around

In this video at 5:03 how is start going to become false at some point?

Any input would be much appreciated. thank you

2 Answers

andren
andren
28,558 Points

It becomes false once it hits 0.

This is due to the fact that when you use a non-Boolean value in a place where a Boolean is expected (like an if statement) Python will automatically treat the value as a Boolean. It does that based on some rules. Basically there is a list of values that will be treated as false, and if the value doesn't match that list it will be treated as true. The list of "false" values is this:

  • None
  • False
  • zero of any numeric type, for example, 0, 0L, 0.0, 0j.
  • any empty sequence, for example, '', (), [].
  • any empty mapping, for example, {}.
  • instances of user-defined classes, if the class defines a nonzero() or len() method, when that method returns the integer zero or bool value False.

Since start starts at 10 it is true, and it remains true until it becomes 0 which is considered a false value which ends the loop.

Hi Nayak,

Note that starts begins at 10 start = 10

but in every iteration of the while loop start is decreased by 1. Eventually start will be 0. In python zero is false.

start -=1

for instance see below
>>> start = 10
>>>start
10
>>> start -=1
>>> start
9

also note the following

>>> L = False
>>> L
False
>>> L == 0
True
>>>