What is the out of this code? Why? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

What is the out of this code? Why?

x, y=5, 0 test=x**y>0 QA=(0, 1)[test] print(QA)

2nd Nov 2019, 6:28 PM
Storm Yule
Storm Yule - avatar
2 Answers
+ 7
Because 5**0>0 evaluates to True, test get assigned to True. In Python booleans work very similarly to integer values of 0 and 1 and can thus be used to index a list, or tuple in this case. (0, 1)[True] equals (0, 1)[1], which evaluates to 1 and QA will equal 1.
2nd Nov 2019, 6:46 PM
Seb TheS
Seb TheS - avatar
+ 1
We know that anything power zero is one. So 5^0=1. Test=1>0=True
4th Nov 2019, 3:54 AM
Geek
Geek - avatar