Ternary Operator
Part of the Logic & Flow section of Coddy's Python journey — lesson 24 of 78.
The ternary operator is a concise way to write simple if-else statements in a single line. It's a shorthand method that can make your code more compact and readable when used appropriately.
The syntax of the ternary operator is:
value_if_true if condition else value_if_falseHere's how it works:
- If the
conditionis true, the expression evaluates tovalue_if_true. - If the
conditionis false, the expression evaluates tovalue_if_false.
For example:
age = 20
status = "Eligible" if age >= 18 else "Not Eligible"
print(status)In this example, if age is greater than or equal to 18, status will be assigned the value "Eligible". Otherwise, it will be assigned "Not Eligible".
The ternary operator is particularly useful for simple conditional assignments where you want to choose between two values based on a condition.
You can also use the ternary operator directly inside a print() call, without assigning the result to a variable first:
age = 20
print("Eligible" if age >= 18 else "Not Eligible")This produces the same output but in a single line, making it even more compact.
Challenge
EasyCreate a program that takes an integer score from the user. The program should use the ternary operator to determine the status based on the score:
- If the score is greater than or equal to 50, the status should be
"Pass". - If the score is less than 50, the status should be
"Fail".
The program should output the determined status.
Cheat sheet
The ternary operator is a concise way to write simple if-else statements in a single line:
value_if_true if condition else value_if_falseExample:
age = 20
status = "Eligible" if age >= 18 else "Not Eligible"
print(status)If the condition is true, it returns value_if_true; otherwise, it returns value_if_false.
Try it yourself
# Write code hereThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Variables Exploration
ConstantsMultiple Variable AssignmentsSwapping VariablesPlaceholder VariablesRound NumbersList Casting4Contact Book Application
Display MenuAdd Contact7Sets Part 2
Mathematical Operations Part 1Mathematical Operations Part 2Recap - Treasure HuntSubsets and SupersetsIterating Over SetsRecap - Tournament Tracker2Dictionaries Part 1
What is a Dictionary?Creating a DictionaryAccessing ValuesModifying DictionariesRecap - Recipe Manager5Advanced Decision Making
Ternary OperatorMembership ChecksIdentity ChecksIndentation ErrorsRecap - Vacation Filter8Student Records Manager
Project OverviewAdd Student11Advanced Functions
Returning Multiple ValuesLambda Functions Part 1Lambda Functions Part 2Recap Challenge - Lambda SortRecursive Functions Part 1Recursive Functions Part 2Recap - Sum Nested List14Higher-Order Functions
The Map FunctionThe Filter FunctionRecap - Email ValidatorRecap - Number Processor3Dictionaries Part 2
Dictionary MethodsNested DictionariesChecking for KeysLooping Through DictionariesRecap - Frequency Counter9Advanced Data Aggregation
Using SumFinding Minimum and MaximumSorting Data EfficientlyRecap - Dictionary Sorter