1

Let's say this is the example code:

def first():
  print(1)
  return True


def second():
  print(2)
  return False


def third():
  print(3)
  return False


first() and second() and third()

The code output is 1 2, but I cannot get it why it happens. What are the order of evaluation and printing the values on the screen there?

  • Your output surely tells you exactly what the order of operations is? Python's [boolean operations](https://docs.python.org/3/reference/expressions.html#boolean-operations) are evaluated _lazily_, so third never gets called. – jonrsharpe Apr 27 '22 at 22:23
  • Because `and` does short-circuiting. It stops evaluating arguments as soon as it gets a falsey result. – Barmar Apr 27 '22 at 22:23
  • 1
    Perhaps it would help to assign the results of your functions to a variable to see what they return, and then it may make more sense: f = first(), s = second(), t = third, then inspect the values of f, s, t. The and operator is working on those returned values, not the printed output. – Metropolis Apr 27 '22 at 22:28

0 Answers0