0

What is the correct way to initialize using or operator? This is what I am attempting to do:

a = 7
b = 4

c = a or b

if (c > 5):
    print(c)

but it seems to only pick the value of a. This is how I was attempting to apply the above:

ref = overall.dbs.reference('mycharacter')

first = ref.order_by_child('name')
first.start_at(sub_category)
first.end_at( sub_category + "\uf8ff").get()

second = ref.order_by_child('name')
second.start_at(sub_category.capitalize())
second.end_at(sub_category.capitalize() + "\uf8ff").get()

snapshot = first or second

Here I am attempting to perform an operation on snapshot which could return null if the first letter of sub_category which is a variable provided either capitalized or not...trying to work around Firebase's orderby() case sensitivity.

Johnn Kaita
  • 432
  • 1
  • 3
  • 13
  • What is the condition here! Please explain when you want `c`s value to be `a` and when you want it to be `b`? – Vikas Periyadath Nov 25 '20 at 04:41
  • What you want to achive> – kelvin Nov 25 '20 at 04:41
  • It's unclear what the code is supposed to do. For the evaluation of boolean expressions, see explanations [here](https://docs.python.org/3/reference/expressions.html#boolean-operations). – GZ0 Nov 25 '20 at 04:43

1 Answers1

0

When you initialize a variable using the or operator like this c = a or b, Python will evaluate the parts of the right side of the expression, looking for "truthy" or "falsy" values. First, it evaluates a. If a is a truthy value, then c will receive the value of a, and Python won't evaluate the value of b. If a is a falsy value, then c will receive the value of b (the last part of the expression).

A falsy value in Python depends on the context. For collections, whatever is empty is considered a falsy value ([], (), {}). For numbers, zero of any numeric type is considered falsy. None and False are also considered falsy values.

Truthy values are everything that is not falsy :). So, to keep it short, if you have something like this:

a = b or c or d or e or f

Python will evaluate every part of the right side of the expression (from left to right) until it finds a truthy value to return, OR the last part of the expression, in case all of the previous parts of the expression are evaluated to false, as the example below.

>>> a = 0
>>> b = []
>>> c = False
>>> d = a or b or c
>>> print(d)

Output:
False

Since all parts of the right side of the expression are falsy values, d will receive the value in the last part of the expression.

Ricardo Erikson
  • 435
  • 1
  • 4
  • 8