0

Explain this code please

x, y, z = 0, 1, 0 if x == 1 or y == 1 or z == 1: print('passed') if 1 in (x, y, z): print('passed') if x or y or z: print('passed') if any((x, y, z)): print('passed') #output passed passed passed passed

19th Apr 2020, 6:01 AM
Ramya Thiurmalisamy
Ramya Thiurmalisamy - avatar
6 Respuestas
+ 3
Ramya Thiurmalisamy any() returns true if any of the element in the iterable is true, else it returns false... So, any((0,1,0)) returns true... Coz, y=1 (ie., true)
19th Apr 2020, 9:19 AM
sarada lakshmi
sarada lakshmi - avatar
+ 4
The conditions you specified all are true... So, each if condition gets executed separately one after the other (since it's not nested if) and prints passed 4 times... The condition of or is : if any of the two conditions is tfue then it returns true.. So, in 1st condition( false or true or false) == true... Prints passed 1 time 2nd condition : 1 is in (0,1,0) so, true... Prints passed 2nd time... 3rd condition : 0 or 1 or 0 ==True.. So, prints passed 3rd time 4th condition : any((0,10)) gives true... So prints passed 4th time.... Hope you understand.. 👍👍
19th Apr 2020, 6:19 AM
sarada lakshmi
sarada lakshmi - avatar
+ 2
any(0,1,0) here, any one of from argument list, otherthan zero treated as true.. In 3rd condition also x or y or z if any one of x, y, z not zero results true.. any(0,2,0) =>true (0 or 5 or 0) results true..
19th Apr 2020, 8:30 AM
Jayakrishna 🇮🇳
19th Apr 2020, 4:50 PM
Ramya Thiurmalisamy
Ramya Thiurmalisamy - avatar
0
sarada lakshmi if you please explain how the 4 th condition becomes true?
19th Apr 2020, 7:06 AM
Ramya Thiurmalisamy
Ramya Thiurmalisamy - avatar
19th Apr 2020, 4:49 PM
Ramya Thiurmalisamy
Ramya Thiurmalisamy - avatar