0

Python: any and all

Please, help me: What's happening in the following code: if any(x == y +z or z == x + y for x in range(5) for y in range(-5, 5) for z in range(-10, 10, 2)): print(True) else: print(False) Its the same this? for x in range(5): for y in range(-5, 5): for z in range(-10, 10, 2): if (x == y + z or z == x + y): check = 1 else: check = 0 if check == 1: print(True) else: print(False) The sequence of the "for var in range(number)" in the Second code is same in the First? Thanks

21st Jan 2021, 11:41 PM
Filipe Duarte Vieira
Filipe Duarte Vieira - avatar
5 Respuestas
+ 2
Actually they are not the same. They would be if there were no else statement in the second one (adding check=0 at the start) Also, there is a performance issue: the second one checks over all the values, even when it's not necessary (when one set of values already turned out to be true)
22nd Jan 2021, 2:21 AM
Angelo
Angelo - avatar
+ 2
Yes they both are same.
21st Jan 2021, 11:52 PM
Abhay
Abhay - avatar
+ 2
Sorry I didn't looked at the code correctly, Angelo is right.
22nd Jan 2021, 8:08 AM
Abhay
Abhay - avatar
0
Abhay Thanks. I will follow you 😁
22nd Jan 2021, 12:53 AM
Filipe Duarte Vieira
Filipe Duarte Vieira - avatar
0
Angelo Thanks. I did not know that. I wil follow you. 😁
22nd Jan 2021, 2:47 AM
Filipe Duarte Vieira
Filipe Duarte Vieira - avatar