World's Best AI Learning Platform with profoundly Demanding Certification Programs
Designed by IITians, only for AI Learners.
Designed by IITians, only for AI Learners.
New to InsideAIML? Create an account
Employer? Create an account
8,3,2 are not divisible by 5 then it should have got appended to a. But in the result of a not getting those numbers. Why those numbers 8,3,2 are not reflecting.
l = [4, 7, 5, 10, 20, 50, 50, 8, 3, 2]
a = []
for i in l:
if (i%5)==0:
print(i)
break
else:
a.append(i)
print(a)
5 [4, 7]
The correct program is:
l = [4, 7, 5, 10, 20, 50, 50, 8, 3, 2] a = [] for i in l: if (i%5)==0: print(i) #break - remove break statement else: a.append(i) print(a)
Output is:
5 10 20 50 50 [4, 7, 8, 3, 2]