-5

Why does (1+1) and 1 return 1 instead of 2?

Why does 1/0 or True evaluate to error instead of True?

Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160

2 Answers2

5

Your first result is due to short-circuit evaluation. Here's how Python evaluates the expression:

  1. Reduce (1 + 1) to 2.

  2. Consider 2 to be "truthy" in a boolean context.

  3. Evaluate and return the right hand operand of and.

Note that if the left hand operand had been "falsy", it would have been returned immediately, and Python would not have attempted to evaluate the right hand operand.

By the way, or works in a similar way: it returns the left hand operand if it's "truthy", and the right hand operand if not.

Note that in no case do and or or ever independently decide to return True or False, or convert any value to or from True or False. It's always one of the two operands that is returned (unless your program crashes – see below), regardless of whether they are of type bool.

Your second result is simply a division by zero error. Like most languages, Python treats division by zero as an error, because the result is undefined in mathematics for real numbers. As soon as Python tries to evaluate 1/0, your program crashes and everything stops – Python never gets the chance to even look at the right hand operand.

However, because of the short-circuiting behaviour described above, the following expression:

True or 1/0

evaluates to True even though 1/0 would have caused an error, because Python never tries to evaluate the right hand operand at all in this case.

Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
  • but 1/0 or True returns error according to above note python should give back true because in an or statement if 1/0 evaluates to false then it should give back true but instead it returns error – raghav singh Mar 01 '17 at 16:25
  • 2
    `1/0` **doesn't** evaluate to false. It doesn't evaluate to anything, because it's literally meaningless and so raises `ZeroDivisionError` and halts execution *before anything else at all can happen*. When attempting to evaluate something causes a fatal error, all bets are off. – Zero Piraeus Mar 01 '17 at 16:37
  • It's important to realise that `1/0` doesn't "return" an error. It **is** an error, and crashes your program. – Zero Piraeus Mar 01 '17 at 16:39
  • 8
    Think of it this way - if you're in a taxi, and you're approaching a junction, the driver normally has to choose which way to go. But if an asteroid falls out of the sky and hits the taxi just as the driver is deciding, the taxi is blown to bits and you don't go down either road. `1/0` is an asteroid. – Zero Piraeus Mar 01 '17 at 17:04
4

Hard to argue with the interpreter/bytecode:

In [1]: import dis

In [2]: dis.dis('(1+1) and 1')
  1           0 LOAD_CONST               1 (2)
              2 JUMP_IF_FALSE_OR_POP     6
              4 LOAD_CONST               0 (1)
        >>    6 RETURN_VALUE

First, Python precompiles 1+1 to 2.

Then it says, "If the value is a falsey value, jump to 6 (i.e. return)". 2 is plainly not false, so it loads 1 and that's the result.

Wayne Werner
  • 49,299
  • 29
  • 200
  • 290