+ 1

Explanation needed

a=[i for i in range(2,101) if all(i%j!=0 for j in range(2,i))] Here in first loop 2 gets divided by 2 itself so remainder is zero, which doesnt satisfy the condition and same is applicable for every prime number, we get one 0 in last. Then also we get a list of prime number. why is it so? Thank you in advance :)

19th Jun 2018, 8:15 PM
Amrish Mishra
Amrish Mishra - avatar
1 Réponse
+ 3
for i = 2, j in range(2,2) so no range so 2 passes if test. for i = 3, j in range(2,3) so [2] so 3 passes if test. for i = 4, j in range(2,4) so [2,3] so 4 fails if test as 4%2 is 0.
20th Jun 2018, 1:16 AM
John Wells
John Wells - avatar