Python Code Execution Results

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)

Answer: [4, 8]
Explanation
  1. The list comprehension iterates through a, checks if num is even (num % 2 == 0), and if true, multiplies num by 2.
  2. For the list a = [1, 2, 3, 4], the even numbers are 2 and 4. Multiplying them by 2 gives 4 and 8.

Output: [4, 8]

This question appeared in Past Papers (1 times)
This question appeared in Subjects (1 times)

Install this app on your device for quick access right from your home screen.