0

Can anyone explain the 4th line of this code??

l = [] for i in range(17): l.append(i*3) m=[x & 1 for x in l] print (sum(m))

3rd Nov 2020, 11:57 AM
Manish Kumar
1 Antwort
+ 3
The fourth line is a list comprehension which is basically equivalent to m = [] for x in l: m.append(x & 1) See here list comprehensions https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2454/ If you don't know about the & operator, you can see here https://www.sololearn.com/learn/4072/ Basically what the code is doing is that it is replacing the multiples of 6 in list `l` with 0 and others with 1 and putting them in `m`. Experiment a bit and think why.
3rd Nov 2020, 12:34 PM
XXX
XXX - avatar