+ 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.
3 Answers
+ 5
Maninder Singh what made you think to add c++ in this question tag ? this is not applicable to c++
+ 4
you prints i but not h(i).
you checks if h(i) is true, but 0*5 == 0 == false.
+ 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]