What is the output of the following python code? a= [1, 2, 3, 4] b= [num * 2 for num in a if num % = = θ] print(b)
What is the output of the following python code? a= [1, 2, 3, 4] b= [num * 2 for num in a if num % = = θ] print(b)
Explanation
- The list comprehension iterates through
a
, checks ifnum
is even (num % 2 == 0
), and if true, multipliesnum
by 2. - For the list
a = [1, 2, 3, 4]
, the even numbers are2
and4
. Multiplying them by 2 gives4
and8
.
Output: [4, 8]