0

I can't get the 'x' part... Please make me understand step wise.

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

15th May 2020, 6:16 PM
Bhanu Tripathi
Bhanu Tripathi - avatar
3 Answers
+ 1
List comprehension https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2454/ In case you don't understand it after completing the above lesson, The 4th line is similar to, m = [ ] for x in l: m.append(x&1) I guess you know what bitwise AND(&) operation is.
15th May 2020, 7:31 PM
Mahmud Nabil
Mahmud Nabil - avatar
0
I can't get it...can you please explain?
16th May 2020, 7:34 PM
Bhanu Tripathi
Bhanu Tripathi - avatar
0
I think there's a lot of good tutorial, documentation about List comprehension out there in the web. Well the bitwise AND op x&1 gives you the last bits(binary digits) here which should be one if the num is odd and zero otherwise. Multiplying with binary 11 i.e. 3 doesn't change the last bit of a number if I'm not wrong. Then summing the last bits gives you the total number of odd numbers in the range(17)
17th May 2020, 6:43 AM
Mahmud Nabil
Mahmud Nabil - avatar