[on_True] if (condition) else [on_False]
elif
(Multiple Conditions)[on_True1] if (condition1) else [on_True2] if (condition2) else [on_False]
# Program to compare a and b
a, b = 7, 8
print("a is less than b") if (a < b) else print("a is greater than b")
a is less than b
# Program to check if a student cleared the test
score = 65
result = ("Fail", "Pass")[score > 50]
print(result)
Pass
# Program to check if a student cleared the test
score = 75
res = ["Fail", "Pass"][score > 75]
print(res)
Fail
True
and False
are keys, and their corresponding values hold the possible outcomes.# Program to check if a student cleared the test
score = 76
print({True: "Pass", False: "Fail"}[score > 75])
Pass
# Program to check if a number is even or odd using a lambda function
num = 5
result = (lambda x: "Even" if x % 2 == 0 else "Odd")(num)
print(result)
Odd
[on_True] if (condition) else [on_False]
[on_True1] if (condition1) else [on_True2] if (condition2) else [on_False]