+ 2

Why is the answer zero for third line?

print(1 == 1 and 2 == 2) print(1 == 1 and 2 == 3) print((1 != 1 and 2 == 2)+(2 < 1 and 3 > 6))

18th May 2020, 11:19 AM
Ankit
Ankit - avatar
6 Answers
+ 3
1 != 1 is false and its zero. 2 == 2 is true and its one. It uses the and operator and since there is only one valid output, it outputs false,0. 2 < 1 is false and its zero. 3 > 6 is false and its zero. It uses the and operator and since there are no valid outputs, it outputs false, 0. Using the plus operator, 0 + 0 = 0.
18th May 2020, 11:28 AM
James Clark I. Vinarao
James Clark I. Vinarao - avatar
+ 1
In Python boolean is subclass of integer type. So when you use the '+' operator with boolean, it implicitly converts them to integer type. https://stackoverflow.com/questions/2406959/why-does-concatenating-a-boolean-value-return-an-integer
18th May 2020, 11:41 AM
Avinesh
Avinesh - avatar
+ 1
That is because 'print False + False' gives you the value zero
19th May 2020, 3:00 AM
Ahmed Muhammed
Ahmed Muhammed - avatar
0
James Clark I. Vinarao thanks for that explanation.
18th May 2020, 11:30 AM
Ankit
Ankit - avatar
0
Avinesh thanks for the link I will look in to it.
18th May 2020, 11:42 AM
Ankit
Ankit - avatar
0
19th May 2020, 1:24 PM
Agu C. Christopher
Agu C. Christopher - avatar