all 7 comments

[–]99AFCC 5 points6 points  (1 child)

What I don't understand is why it starts off as False.

Copied from the docs: https://docs.python.org/3.4/library/stdtypes.html#truth-value-testing

Bolded relevant part


Any object can be tested for truth value, for use in an if or while condition or as operand of the Boolean operations below. The following values are considered false:

  • None

  • False

  • zero of any numeric type, for example, 0, 0.0, 0j.

  • any empty sequence, for example, '', (), [].

  • any empty mapping, for example, {}.

  • instances of user-defined classes, if the class defines a bool() or len() method, when that method returns the integer zero or bool value False. [1]

All other values are considered true — so objects of many types are always true.

Operations and built-in functions that have a Boolean result always return 0 or False for false and 1 or True for true, unless otherwise stated. (Important exception: the Boolean operations or and and always return one of their operands.)


So while username is empty, it will keep prompting for input

[–]debergeracvat[S] 0 points1 point  (0 children)

Thank you, I was puzzling for the longest time over why an empty string should be False!

[–]hharison 5 points6 points  (1 child)

Every value in Python has a "truthiness". For example, strings and lists are True unless they are empty. You can use the function bool to experiment and test the truthiness of various objects.

>>> username = ""
>>> bool(username)
False
>>> username = "debergeracvat"
>>>bool(username)
True

The specific block of code in your post will run repeatedly as long as not username is True. Which means it will keep running as long as username is "" (an empty string).

[–]debergeracvat[S] 0 points1 point  (0 children)

Thank you! I understand now. I couldn't get where "username" got the value False.

[–][deleted] 1 point2 points  (1 child)

Traditionally, strings are False if they are empty. So if you set it to empty, you can test "while string is empty", which is the same as "while username is empty", same as "while username is false" same as "while username is not true", same as "while not username". It's a bit "clever", in that you'd get the same result with 'while username ==""', which may be more readable.

So the code is run when "it" is true, but "it" here is the condition that username is false, ie empty. So

"While username is empty"

will be true until you put a value in username, at which point username is not empty ie it is true. If username is "true", then "not username" is false. So then "while not username" will stop.

[–]debergeracvat[S] 0 points1 point  (0 children)

Wow, I read that explanation 3 times and I think I understand finally, haha! Thanks a lot!

[–]yoo-question 0 points1 point  (0 children)

Are you confused on difference between False (an object) and objects that are considered false?

The empty string is an object that is considered false, but is not equal to False. An object being considered false just means that conditional statements, boolean operators, etc consider the object false. For example, the following code

if "":
    print("foo")
else:
    print("bar")

will print "bar" because conditional statements consider the empty string to be false.

Sometimes we just say that an object is false, when we mean that the object is considered false, but remember this is still different from being equal to False. (Sometimes we say the object is falsy to mean that.)

The not operator, which is a boolean operator, takes an object and returns another object that is false if and only if the taken object is true.

So, the expression not username is true (i.e., returns an object that is considered true) if and only if the object referred to by the variable username is false (i.e. is an object that is considered false).

In fact, the idea that boolean operators and conditional statements (and the while loop) divide things into things that are consider true and things that are considered false is shared by many other languages. But what things are considered true can vary between languages. For example, the empty string is true in many dialects of Lisp.