-2

I don't know if this has been asked before but here I go.

A while loop takes a bool, something like while a<5 or while True and proceeds to do something.

n = 10000
count = 0
while n:
  count = count + 1
  n = n / 10
  n=int(n)

print (count)

This code will execute the while loop, but why, I understand that I could use 1 instead of True, but here it's going like "while 10000", and 10000 != True, so how does that work?

mateo_io
  • 398
  • 2
  • 3
  • 12

1 Answers1

3

Boolean values are the two constant objects False and True. They are used to represent truth values (although other values can also be considered false or true). In numeric contexts (for example when used as the argument to an arithmetic operator), they behave like the integers 0 and 1, respectively. The built-in function bool() can be used to convert any value to a Boolean, if the value can be interpreted as a truth value (see section Truth Value Testing above).

Mazdak
  • 105,000
  • 18
  • 159
  • 188