Python - Conditional Statements
You want to create a dictionary where keys are numbers 1 to 5 and values are "Even" or "Odd" using a ternary expression inside a dictionary comprehension. Which code is correct?
{key: value for variable in iterable}. The value can use a ternary expression.else keyword, causing SyntaxError; d = {x if x % 2 == 0 else "Odd": x for x in range(1,6)} misplaces ternary in key. d = {x: "Even" else "Odd" if x % 2 == 0 for x in range(1,6)} has invalid syntax.15+ quiz questions · All difficulty levels · Free
Free Signup - Practice All Questions