0

For example:

x = [1, 2, 3]
y = [4, 5, 6]
print(x and y)

When I run this, the result is [4, 5, 6]. Can someone explain what is happening?

David Pham
  • 187
  • 1
  • 4
  • 14

1 Answers1

1

From the Python documentation (https://docs.python.org/3/reference/expressions.html#and):

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

So it will be evaluating x, which is not false, then returning y.

Ollie
  • 1,641
  • 1
  • 13
  • 31