Unit: Unit 6: Functions • Interactive Practice
Interactive Python programming tutorial and practice in your browser.
# Sort a list of student records (name, score) by score using a lambda function
students = [("Alice", 88), ("Bob", 95), ("Charlie", 72), ("Diana", 91)]
# Use sorted with key=lambda student: score
by_score = sorted(students, key=lambda s: s[1])
print("Sorted by score ascending:")
for name, score in by_score:
print(f" {name}: {score}")
# Inline calculation: square a number using lambda
square = lambda x: x ** 2
print("Square of 9:", square(9))