while checks a condition. If it is true, the block runs, then the condition is checked again. When it is false, the loop stops. for stops when the items run out. while does not stop unless you make the condition false.

A score list is usually a for. Use while when you hold the index yourself, or when the stop rule is “until this becomes true.” Run both on the online Python page.

A counter

i = 0
while i < 3:
    print(i)
    i += 1

The output is 0, 1, 2. Without i += 1 the condition stays 0 < 3. The loop never ends and the tab locks. This compiler is the same Python: forget the update and the page freezes.

Walk a list with while

scores = [95, 82, 70, 55, 49]
i = 0
while i < len(scores):
    print(i, scores[i])
    i += 1

i is the index. scores[i] is the score at that index. If len(scores) is 5, the condition is i < 5. The loop stops when i becomes 5. The last index is 4.

for score in scores does the same walk in one line. If you do not need the index, the for loop is shorter. Use while when you manage the index, skip the middle, or stop on something other than the length.

while and if: the same letter scale

scores = [95, 82, 70, 55, 49]
i = 0
while i < len(scores):
    score = scores[i]
    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)
    i += 1

i += 1 belongs at the end of the block. Incrementing before you read the score skips the first item. Condition order is covered in if, elif, else.

When while, when for

  • Use for when the count is known and you will visit every item.
  • Use while when the end is “until the queue is empty” or “until the letter matches,” not the length of a list.
  • while True needs a break inside. No break means the loop does not close.

break ends the loop. continue ends the current turn and checks the condition again. If the counter is not updated before continue, the loop is still infinite.

Write it as the problem

Letter Grades wants a returned list. while can build it:

def harf_notlari(notlar: list[int]) -> list[str]:
    result = []
    i = 0
    while i < len(notlar):
        score = notlar[i]
        if score >= 90:
            result.append("AA")
        elif score >= 50:
            result.append("CC")
        else:
            result.append("FF")
        i += 1
    return result

That scale is shortened so the shape is visible. The problem also has bounds at 80, 70, and 60. A missing elif returns the wrong letter. Move the full branch list from the for-loop guide into this while, 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