-2

Saw this while searching and I couldn't understand the logic behind. How does and & or methods work in print()?

T=[int(s) for s in input().split()]
print(T and sorted(sorted(T,reverse=True),key=abs)[0] or 0)

I've tried simplifying it to understand how it handles different inputs.

print(A and B or C)

Returns B when B is not 0, and returns C when B is 0 but never gets to A.

Xeculus
  • 36
  • 4
  • 2
    Check [this answer](https://stackoverflow.com/a/36551857/3717691): _"The `and` and `or` operators don't simply perform a boolean operation on their operands, giving a boolean result. The result they give is always one of their operands. These operators evaluate from left to right, with `and` having a higher precedence than `or`, and they short-circuit, meaning that they stop evaluating their operands as soon as possible."_ – Jeppe Sep 02 '19 at 13:48
  • `print` always returns `None` and `A and B or C` works the same whether you print it or not. – Stop harming Monica Sep 02 '19 at 13:48

2 Answers2

0

This is an old style way to use ternary condition operator.

It has nothing related to print.

See this answer.

In most cases it does the same as this code

print(T and sorted(sorted(T,reverse=True),key=abs)[0] or 0)
print(sorted(sorted(T,reverse=True),key=abs)[0] if T else 0)

It works because of the way and and or works in Python - they do not return bool, but they return either first or second operand, depending if it casts to True or False.

This answer has more details.

Basically it's two operation

first = T and sorted(sorted(T,reverse=True),key=abs)[0]

and Return the first Falsy value if there are any, else return the last value in the expression.

So it either returns [] (possible Falsy value for T), or first element in sorted list.

result = first or 0

or Return the first Truthy value if there are any, else return the last value in the expression.

So if T is Truthy, we already have a Truthy value in first (unless it's 0, which is special case, but 0 or 0 is 0 anyway), it will be returned.

If T is [] - it will return 0.

Igor
  • 2,863
  • 17
  • 29
0

As part of the answer, if given a non-empty list, T, of integers, the expression

sorted(sorted(T,reverse=True),key=abs)[0]

returns the integer in the list which is closest to zero. For example, if T = [3, -2, 4, 5], it will return -2.

The subtle point is what is does in a case like T = [3, -2, 4, 2] where -2 and 2 are tied in being closest. In this case it will return 2 rather than -2. The logic for this particular case is that sorted(T, reverse = True) will put all of the positive numbers before any of the negative numbers, so that 2 will be before -2. When the result of that sort is then sorted again with key = abs, the 2 and -2 are already in sorted order (relative to key = abs), so they won't be swapped, hence 2 rather than -2 will be the first element of the list. The code is actually somewhat subtle and depends on the fact that Timsort is a stable sorting algorithm.

John Coleman
  • 46,420
  • 6
  • 44
  • 103