2

I just did an exercise in my Python course and stumbled across a solution that doesn't really make sense to me.

def range_in_list(lst, start=0, end=None):
    end = end or lst[-1]
    return sum(lst[start : end + 1])

How does "or" work when assigning basically two values to a variable, does the variable have both values assigned to it? Unfortunately, I can't find any information on this in my course nor anywhere else.

fabestah
  • 77
  • 1
  • 1
  • 9
  • 1
    'or' doesn't assign 2 values. In your case if end is None it (end) will receive the value of lst[-1] last item in the list. If end is not None - it will be stay the same (assume end is 5 it will remain 5) – adhg Oct 15 '22 at 21:07

2 Answers2

3

You may have learnt how to use the or and and operators when working with boolean values, but there is one detail you might be missing, and that is: how they actually work. The python docs say (Emphasis mine):

Note that neither and nor or restrict the value and type they return to False and True, but rather return the last evaluated argument. This is sometimes useful, e.g., if s is a string that should be replaced by a default value if it is empty, the expression s or 'foo' yields the desired value....)

As you can see, the or and and operators are not restricted to returning True or False, and I will add they are not restricted to working explicitly with boolean expressions, instead they act like expression evaluators, and return the last expression which is Truthy.

Two lessons about the or operator

Or evaluates expressions

or will either return the value of the expression on the right side if it is truthy, otherwise the expression on the left side is evaluated, and that becomes the result of the entire statement.

Or is lazy

Implicit in the first lesson, is the notion of lazy evaluation. It is important to understand that evaluation of the expression is not done immediately, this is another way of saying the evaluation is lazy. Consider the following:

v = True or <some expensive database fetch which might take long time>

In the above, <some expensive database fetch which might take long time> will never be evaluated because the left expression was truthy (actually true in this case), therefore the value of v will be True.

This concept is further solidified if you understand why the following will not raise a ZeroDivisionError exception:

n = 45 or (1/0)
smac89
  • 39,374
  • 15
  • 132
  • 179
  • 2
    Now I fully get what's happening I suspected the direction but just wasn't able to piece together the right answer. I think I have gotten to that point now where I definitely need to read the Python doc more Thanks for your fast and awesome answer, really appreciate it ❤️ – fabestah Oct 15 '22 at 21:46
  • @fabestah Yes the docs are very helpful and can be beneficial if you are self-studying. Glad it cleared your confusions, that's always a joy to hear. – smac89 Oct 16 '22 at 02:03
0

In python, or is a Boolean operator that evaluates as follows:

True or True
> True
True or False
> True
False or True
> True
False or False
> False

None is neither True nor False (see article), but it evaluates to False in an or operation.

On a separate note, it is not clear what this code is intended to do. Moreover, what the code actually does, does not seem useful. This code does not calculate the range of a list of numbers, if that is the intent.

Second, the purpose of None is not clear either. My guess is that it is supposed to be a fail safe for when the list is empty, but it isn't working that way.

Snehal Patel
  • 192
  • 10
  • This is just an exercise and the task of the exercise doesn't actually have something to do with the question itself, I just included it to add a bit more context to the exercise. Thanks for your answer ❤️ – fabestah Oct 15 '22 at 21:51