here in Japan, we have a python exam using a book called ‘python tutorial’ by Guido von Rossum.
so far, the book has tons of interesting information, and lots of things that get into the nitty gritty of Python (for me at least, it’s nitty gritty).
there are a couple of things I don’t understand in the book… this being one of them:
string1, string2, string3 = ‘’, ‘Trondheim’, ‘Hammer Dance’
#the first string ‘’ is two single quotes with nothing inside
non_null = string1 or string2 or string3
I’m not really sure why this yields ‘Trondheim’
Can someone explain in a bit of detail?
Thank you in advance for any help I get. Good energy your way!
The “non_null = string1 or string2 or string3” assignment basically
means set non_null to:
the value of string1
or if string1 isn’t a true value, then the value of string2
or if string2 isn’t a true value, then the value of string3
Since string1 was empty and string2 was not empty, non_null gets the
string2 value (and string3 is never reached).
For some details regarding this topic, see Python 3: Truth Value Testing.
Operations and built-in functions that have a Boolean result always return
False for false and
True for true, unless otherwise stated. (Important exception: the Boolean operations
and always return one of their operands.)
the return value of a short-circuit operator is the last evaluated argument
string1 is evaluated first, but is considered False because it is empty. string2 is evaluated next, and is considered True because it is not empty. string3 is not evaluated because string2 was considered True. The return value is string2, the last evaluated argument.
To note, this is actually a very useful feature that even some experienced Python developers are unaware of. Here’s a common use case to illustrate: instead of doing
if not ham:
ham = Ham()
spam = cook(ham)
You can do
ham = ham or Ham()
spam = cook(ham)
spam = cook(ham or Ham())
It does come at some cost—the code can be less clear to some users, and if you want to check that
ham is a specific sentinel (e.g.
None) rather than any falsey value, it won’t quite do what you want.
Thank you very much for the thorough and easy to understand answers.
Incidentally, the link that Peter included seems to be exactly the same as the book that we are using (Japanese text)…
Very nice to learn in both languages, thank you!
Seems I have a lot to learn since I understood the text and reasoning, but didn’t know that ‘’ would be evaluated as False, and that the variable would be assigned a string, and not True.
Lots to learn, so back to it👍
By Loki Black via Discussions on Python.org at 05May2022 22:55:
Seems I have a lot to learn since I understood the text and reasoning,
but didn’t know that ‘’ would be evaluated as False, and that the
variable would be assigned a string, and not True.
In Python, “or” (and “and” etc) evaluate “truthiness”: values considered
to be true or false in terms of making decisions, but not necessarily
False values which are the only valid values
There are some rules of thumb which we try use consistently:
bools are true if
True and “false” if
- numeric values are “false” if zero, and “true” otherwise
- containers like
sets are “false” if empty
and “true” otherwise
- strings (
str) look a lot like lists of characters, and so we treat
an empty string as “false” and nonempty strings as “true”
None, the common placeholder for “no value specified”, is “false”
- other objects are “true” if the rules above do not apply
When you use “or”, it returns the left value if that value is “true”,
otherwise the right value. Note: not
False, but the value
>>> 3 or 4
>>> 0 or 4
This works with while-loops and if-statements and other things because
they also test “truthiness”.
You can get real
bools if you need them:
>>> bool(3 or 4)
>>> bool(0 or 4)
>>> bool(3 and 4)
>>> bool(0 and 4)
Cameron Simpson firstname.lastname@example.org
Yup, because the “book” you have is presumably just a static printout of the Japanese translation of the official Python tutorial (which was originally written by Guido van Rossum, the initial creator of Python). The latest, up to date official version of it is hosted online on the Python website for free in a variety of languages (including Japanese) and is included with Python, so there’s no need to pay for a static version in book form (unless you really want to).
Just to note, custom classes (yours or third-party libraries, like NumPy arrays and Pandas DataFrames) can evaluate false, true, or even raise an error when used in a boolean context. But this isn’t that common, and you don’t usually need to worry about it until you get into more advanced Python programming.
Best of luck!