score = 76
if score < 60:
    grade = 'F'
elif score < 70:
    grade = 'D'
elif score < 80:
    grade = 'C'
elif score < 90:
    grade = 'B'
else:
    grade = 'A'
print(grade)

score = 76
grades = [
    (60, 'F'),
    (70, 'D'),
    (80, 'C'),
    (90, 'B'),
]
print(next((g for x, g in grades if score < x), 'A'))

Replace swith statement with a mapping
Replace swith statement with a mapping

Official Python documentation explains [1], that switch statement can be
replaced by `if .. elif .. else` or by a mapping.

In first example with varying condtitions `if .. elif .. else` was used, and in
this example a mapping is much better fitted way of doing a switch statement
logic.

Since this is an official and documented way of doing switch statement logic in
Python, am adding it here.

[1] https://docs.python.org/3/faq/design.html#why-isn-t-there-a-switch-or-case-statement-in-python

  score = 76
+ grades = [
+     (60, 'F'),
+     (70, 'D'),
+     (80, 'C'),
+     (90, 'B'),
+ ]
+ print(next((g for x, g in grades if score < x), 'A'))
- if score < 60:
-     grade = 'F'
- elif score < 70:
-     grade = 'D'
- elif score < 80:
-     grade = 'C'
- elif score < 90:
-     grade = 'B'
- else:
-     grade = 'A'
- print(grade)
  
Replace swith statement with a mapping
## Code Before:
score = 76
if score < 60:
    grade = 'F'
elif score < 70:
    grade = 'D'
elif score < 80:
    grade = 'C'
elif score < 90:
    grade = 'B'
else:
    grade = 'A'
print(grade)

## Instruction:
Replace swith statement with a mapping
## Code After:
score = 76
grades = [
    (60, 'F'),
    (70, 'D'),
    (80, 'C'),
    (90, 'B'),
]
print(next((g for x, g in grades if score < x), 'A'))
