what is line 5 means? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
22nd Nov 2019, 8:42 AM
Ansab Khan
Ansab Khan - avatar
3 Answers
+ 9
x & 1 produces a value that is either 1 or 0, depending on the least significant bit of x: if the last bit is 1, the result of x & 1 is 1; otherwise, it is 0. This is a bitwise AND operation. So every number [0, 3, 6, 9 , 12....] lsb is checked and if if the bit is 1 return 1 else 0 so 3 = 11 lsb is 1 so output 1 6 = 110 lsb is 0 so output 0 9 = 1001 lsb is 1 so output 1 this way it is working in line 5 And before that list range 0 to 17 where multiply with 3 is done in the range numbers Ansab Khan then sum of that eight ones is done which is gives final result as 8
22nd Nov 2019, 9:15 AM
GAWEN STEASY
GAWEN STEASY - avatar
+ 5
Line 5 is list comprehension. What's happening is that the loop is iterating over the list 'l' and each element of list is assigned to variable 'x' which is checked if it is even or odd using Bitwise AND operator. The result of each check is stored in the list 'm'. The sum of 8 one's is 8 obviously.
22nd Nov 2019, 8:54 AM
blACk sh4d0w
blACk sh4d0w - avatar
22nd Nov 2019, 9:51 PM
Ansab Khan
Ansab Khan - avatar