This code example demonstrates an advanced use of the ternary operator to determine grades based on scores. By leveraging nested ternary operators within a function, we can efficiently map numerical scores to letter grades.
This approach not only showcases the power of the ternary operator for handling multiple conditions but also highlights the elegance of Python’s list comprehensions for applying functions across collections. By the end of this example, you’ll see how these features work together to streamline the process of grade determination and output.
# Function to determine the grade based on score
def determine_grade(score):
return 'A' if score >= 90 else ('B' if score >= 80 else ('C' if score >= 70 else ('D' if score >= 60 else 'F')))
# List of scores
scores = [95, 82, 76, 59, 89, 99, 65, 73, 84, 91]
# Using list comprehension with ternary operator to assign grades
grades = [determine_grade(score) for score in scores]
# Printing the scores and their corresponding grades
for score, grade in zip(scores, grades):
print(f"Score: {score}, Grade: {grade}")
Score: 95, Grade: A
Score: 82, Grade: B
Score: 76, Grade: C
Score: 59, Grade: F
Score: 89, Grade: B
Score: 99, Grade: A
Score: 65, Grade: D
Score: 73, Grade: C
Score: 84, Grade: B
Score: 91, Grade: A
def determine_grade(score):
return 'A' if score >= 90 else ('B' if score >= 80 else ('C' if score >= 70 else ('D' if score >= 60 else 'F')))
Explanation:
def determine_grade(score): defines a function named determine_grade that takes one parameter, score.x if condition else y.score using a nested ternary operator for multiple conditions.score is greater than or equal to 90.'A' if score >= 90 else ...score is less than 90, it moves to the next condition:'B' if score >= 80 else ...'C' if score >= 70 else ...'D' if score >= 60 else 'F'score.scores = [95, 82, 76, 59, 89, 99, 65, 73, 84, 91]
Explanation:
scores is created, containing a series of integers representing different scores.grades = [determine_grade(score) for score in scores]
Explanation:
[expression for item in iterable] is used here.determine_grade(score) is called for each score in the scores list.determine_grade function is applied to each element in scores, resulting in a new list of grades.grades, where each score from scores has been converted to its corresponding grade using the determine_grade function.for score, grade in zip(scores, grades):
print(f"Score: {score}, Grade: {grade}")
Explanation:
zip(scores, grades) function pairs each element in scores with the corresponding element in grades, creating an iterator of tuples.for score, grade in zip(scores, grades) loop iterates over these tuples.print(f"Score: {score}, Grade: {grade}") prints each score and its corresponding grade in a formatted string.f before the string indicates an f-string, which allows for embedding expressions inside string literals using curly braces {}.This code efficiently determines and prints grades for a list of scores using a function with nested ternary operators. The function determine_grade uses these nested ternary operators to evaluate the score and assign the appropriate grade. List comprehension applies this function to each score in the scores list, and the zip function pairs each score with its grade for easy printing.