How does logic work in combination with assigning values to variables

Question:

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.

Asked By: fabestah

||

Answers:

def range_in_list(lst, start=0, end=None):
    end = end or lst[-1]
    print(end) # added a print statement for better understanding
    return sum(lst[start : end + 1])

Case 1 when end=None:

range_in_list(lst=[1, 2])

If you run this code, you’ll see that 2 is printed out. This is the lst[-1] value.

Case 2 when end is not None:

range_in_list(lst=[1, 2], end=10)

If you run this, you’ll see that 10 is printed out. This is because the value of end is not None.

For better understanding:

def range_in_list(lst, start=0, end=None):
    if end is None:
        end = lst[-1]
    print(end)
    return sum(lst[start : end + 1])
Answered By: Junkrat

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)
Answered By: smac89

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.

Answered By: Snehal Patel
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.