Python will do its best to evaluate the "truthiness" of an expression when a boolean value is needed from that expression.
The rule for strings is that an empty string is considered False, a non-empty string is considered True. The same rule is imposed on other containers, so an empty dictionary or list is considered False, a dictionary or list with one or more entries is considered True.
The None object is also considered false.
A numerical value of 0 is considered false (although a string value of '0' is considered true).
All other expressions are considered True.
Details (including how user-defined types can specify truthiness) can be found here: http://docs.python.org/release/2.5.2/lib/truth.html.
Answer from Larry Lustig on Stack OverflowPython will do its best to evaluate the "truthiness" of an expression when a boolean value is needed from that expression.
The rule for strings is that an empty string is considered False, a non-empty string is considered True. The same rule is imposed on other containers, so an empty dictionary or list is considered False, a dictionary or list with one or more entries is considered True.
The None object is also considered false.
A numerical value of 0 is considered false (although a string value of '0' is considered true).
All other expressions are considered True.
Details (including how user-defined types can specify truthiness) can be found here: http://docs.python.org/release/2.5.2/lib/truth.html.
In python, any string except an empty string defaults to True
ie,
Copyif "MyString":
# this will print foo
print("foo")
if "":
# this will NOT print foo
print("foo")
Videos
python - Empty String Boolean Logic - Stack Overflow
How to check if the string is empty in Python? - Stack Overflow
Why does my empty dictionary not evaluate to false?
Help with understanding “Truthy” and “Falsey” values.
I’m working through Automate the Boring Stuff and am having trouble understanding the "while not" bit in this piece of sample code:
name = ''
while not name:
print('Enter your name:')
name = input()So far the other examples in the book have been "while True:", which makes sense to me.
If the code is changed to:
name = False
while name == False:
print('Enter your name:')
name = input()The program will proceed if nothing is entered for name, whereas when using "while not name" the program will continue to ask for input for name.
If I change it again to:
name = False
while not name:
print('Enter your name:')
name = input()I get the same results as the first piece of code.
My questions are twofold:
How does while not work?
Why would one choose to define the name variable as '' instead of False?
Edited to fix formatting.
to check whether x is True or False:
Copybool("")
> False
bool("x")
> True
for details on the semantics of is and == see this question
Am I supposed to conclude that x is neither True nor False?
That's right. x is neither True nor False, it is "". The differences start with the type:
Copy>>> print(type(""), type("x"), type(True), type(False))
builtins.str, builtins.str, builtins.bool, builtins.bool
Python is a highly object oriented language. Hence, strings are objects. The nice thing with python is that they can have a boolean representation for if x: print("yes"), e. g.. For strings this representation is len(x)!=0.
Empty strings are "falsy" (python 2 or python 3 reference), which means they are considered false in a Boolean context, so you can just do this:
if not myString:
This is the preferred way if you know that your variable is a string. If your variable could also be some other type then you should use:
if myString == "":
See the documentation on Truth Value Testing for other values that are false in Boolean contexts.
From PEP 8, in the “Programming Recommendations” section:
For sequences, (strings, lists, tuples), use the fact that empty sequences are false.
So you should use:
if not some_string:
or:
if some_string:
Just to clarify, sequences are evaluated to False or True in a Boolean context if they are empty or not. They are not equal to False or True.
dict_a = {}
print(dict_a)
print(dict_a == False)
print(bool(dict_a) == False)Why do we need the bool function to check whether a sequence is empty not not? I keep thinking if a list or dictionary is empty it automatically evaluates to none. So why do we need to explicitly check the boolean of an object?