Python में नीचे दिए गए कोड का आउटपुट क्या होगा?
What will be the output of the following Python code?
L = [1, 2, 3, 4, 5]
print([x & 1 for x in L])
A
[1, 2, 3, 4, 5]
B
[1, 0, 1, 0, 1]
C
[0, 1, 0, 1, 0]
D
Error
Explanation
Given code:
L = [1, 2, 3, 4, 5]
print([x & 1 for x in L])
यह एक list comprehension है, जिसमें x & 1 का इस्तेमाल हो रहा है।
🔸 क्या होता है x & 1?
यह एक bitwise AND operation है, जो x के last bit की जांच करता है:
-
यदि number odd है, तो
x & 1 = 1 -
यदि number even है, तो
x & 1 = 0x Binary x & 1 Result 1 0001 0001 1 2 0010 0000 0 3 0011 0001 1 4 0100 0000 0 5 0101 0001 1 🔸 Output:
[1, 0, 1, 0, 1]
Correct Answer: B) [1, 0, 1, 0, 1]