Source Code:
(back to article)
# Create a list of numbers from 1 to 10 numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # Use a list comprehension to create a new list with the even numbers squared and the odd numbers cubed result = [x**3 if x % 2 == 1 else x**2 for x in numbers] print(result) # Output: [1, 4, 27, 16, 125, 36, 343, 64, 729, 100]
Result:
Report an issue