A for loop visits every item in a sequence once. A score list, a word, or range(5) all use the same shape. You do not advance a counter. Python stops when the items run out.

This guide covers a for over a list, range when you need the index, then the same scores mapped to letters with if. Paste the code into the online Python compiler and read the output. Then write it as a function in Letter Grades.

for over a list

scores = [95, 82, 70, 55, 49]
for score in scores:
    print(score)

score is the next number on each turn: 95, then 82, and finally 49. When the list ends, the loop ends. score keeps the last value. It does not start another pass.

range when you need the index

scores = [95, 82, 70]
for i in range(len(scores)):
    print(i, scores[i])

range(3) yields 0, 1, 2. Indexes start at 0. scores[3] raises IndexError on this list.

If you need both the index and the value, enumerate is shorter:

for i, score in enumerate(scores):
    print(i, score)

for and if: a score becomes a letter

for brings the next score. if picks one letter.

scores = [95, 82, 70, 55, 49]
for score in scores:
    if score >= 90:
        letter = "AA"
    elif score >= 80:
        letter = "BA"
    elif score >= 70:
        letter = "BB"
    elif score >= 60:
        letter = "CB"
    elif score >= 50:
        letter = "CC"
    else:
        letter = "FF"
    print(score, letter)

Output:

95 AA
82 BA
70 BB
55 CC
49 FF

95 also satisfies >= 80. It stays AA because >= 90 is tested first. Reverse the branches and 95 becomes BA. Tighter bounds go above wider ones.

print inside the loop writes one line per score. A print after the loop shows only the last letter.

What usually breaks

  • Indentation. Lines that belong to the for move in together. No indent raises IndentationError.
  • Appending to the list you are walking. scores.append(...) inside for score in scores extends the trip. Collect results in a new list.
  • range(1, len(scores)) skips the first item. Start at 0 unless you mean to skip it.

To walk the same list with a counter, read the while loop guide. Condition order is in if, elif, else.

Write it as the problem

Letter Grades uses this scale. print is not enough. Return a list with the same length and the same order as the input.

def harf_notlari(notlar: list[int]) -> list[str]:
    result = []
    for score in notlar:
        if score >= 90:
            result.append("AA")
        elif score >= 80:
            result.append("BA")
        elif score >= 70:
            result.append("BB")
        elif score >= 60:
            result.append("CB")
        elif score >= 50:
            result.append("CC")
        else:
            result.append("FF")
    return result

An empty list never enters the loop, so the function returns []. That matches the empty-input rule. Try print(harf_notlari([95, 82, 49])) on the online Python page, then open the problem.

Interactive Practice Station

Ready to Test Theory in a Real Exam Environment?

10 specialization modules and 200 original Python interview problems await you on PyMülakat. Write and run your code in the browser and test yourself against hidden boundary cases.

Solve First Problem (K01) →Explore Modules