0

The line is:

key1 = dictionary_name.get('key1', ['']) [0] or 0

I understand the get method and the default value but I have no clue what's happening with the or after the ). The line seems to have the function to read the value of a key out of a directory and return 0 if the value is ' '.
But how does the line, especially the boolean or, work in details?

Vincent Savard
  • 32,695
  • 10
  • 65
  • 72
braasch
  • 57
  • 4

2 Answers2

-1

The empty string is falsy in python, so in this situation, if key1 is not found, the value will be 0

Python documentation

SO example

Community
  • 1
  • 1
David
  • 10,427
  • 3
  • 38
  • 45
-1

To quote the docs:

The expression x or y first evaluates x; if x is true, its value is returned; otherwise, y is evaluated and the resulting value is returned.

[...]

Note that neither and nor or restrict the value and type they return to False and True, but rather return the last evaluated argument.

Jasper
  • 3,840
  • 1
  • 17
  • 35