Python Programming - Tricky Questions - Discussion

Discussion Forum : Tricky Questions - General Questions (Q.No. 9)
9.
What is the output of the following Python code?
my_list = [1, 2, 3, 4]
result = [x if x % 2 == 0 else -x for x in my_list]
print(result)
[1, -2, 3, -4]
[-1, 2, -3, 4]
[1, -2, -3, -4]
[1, -2, 3, 4]
Answer: Option
Explanation:
List comprehension is used to create a new list where even numbers are unchanged, and odd numbers are negated.
Discussion:
Be the first person to comment on this question !

Post your comments here:

Your comments will be displayed after verification.