+ 3

Can you expalin this output?

y=list(range(10)) def h(x): if(x%2==0): return x*5 else: return False print([i for i in y if h(i)]) #output = [2,4,6,8] but why x is not multiply by 5 and where is 0 in the output list can anyone explain this.

8th Sep 2018, 6:39 AM
Maninder $ingh
Maninder $ingh - avatar
3 Answers
+ 5
Maninder Singh what made you think to add c++ in this question tag ? this is not applicable to c++
8th Sep 2018, 7:38 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 4
you prints i but not h(i). you checks if h(i) is true, but 0*5 == 0 == false.
8th Sep 2018, 8:48 AM
Sergey Ushakov
Sergey Ushakov - avatar
+ 2
print([x * 5 for x in range(1, 10) if x % 2 == 0]) #will print [10, 20, 30, 40] == lst = [] for x in range(1, 10): if x % 2 == 0: x *= 5 lst.append(x) print(lst) #will print [10, 20, 30, 40] == y = list(range(10)) def h(x): if x % 2 == 0: x *= 5 return x print([h(x) for x in y if x % 2 == 0]) #will print [0, 10, 20, 30, 40]
9th Sep 2018, 7:52 PM
Polina
Polina - avatar