0
0
Pythonprogramming~10 mins

Ternary conditional expression in Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to assign 'Yes' if x is greater than 10, otherwise 'No'.

Python
result = 'Yes' if x [1] 10 else 'No'
Drag options to blanks, or click blank then click option'
A<
B==
C>
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' changes the condition meaning.
Using '==' checks equality, not greater than.
2fill in blank
medium

Complete the code to assign 'Even' if num is divisible by 2, else 'Odd'.

Python
parity = 'Even' if num [1] 2 == 0 else 'Odd'
Drag options to blanks, or click blank then click option'
A//
B%
C**
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using '//' which is floor division, not remainder.
Using '+' or '**' which are not related to divisibility.
3fill in blank
hard

Fix the error in the ternary expression to assign 'Adult' if age is 18 or more, else 'Minor'.

Python
status = 'Adult' if age [1] 18 else 'Minor'
Drag options to blanks, or click blank then click option'
A==
B<=
C!=
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' reverses the condition meaning.
Using '==' only checks if age equals 18, not more.
4fill in blank
hard

Fill both blanks to create a ternary expression that assigns 'Pass' if score is 50 or more, else 'Fail'.

Python
result = 'Pass' if score [1] 50 else [2]
Drag options to blanks, or click blank then click option'
A>=
B'Fail'
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' in the condition reverses the logic.
Not using quotes around 'Fail' causes syntax errors.
5fill in blank
hard

Fill all three blanks to create a ternary expression that assigns the uppercase of name if is_upper is True, else the lowercase.

Python
formatted = name.[1]() if is_upper [2] True else name.[3]()
Drag options to blanks, or click blank then click option'
Aupper
B==
Clower
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '==' changes the condition meaning.
Mixing up 'upper' and 'lower' methods.