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
forwhen the count is known and you will visit every item. - Use
whilewhen the end is “until the queue is empty” or “until the letter matches,” not the length of a list. while Trueneeds abreakinside. Nobreakmeans 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.
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.
More Python Guides & Articles
Python for Loop: Lists, range, and Letter Grades
A Python for loop walks every item in a list. range, indexes, and turning scores into letter grades. The Letter Grades problem uses the same scale.
Python if, elif, else: Condition Order and Letter Grades
Python if, elif, and else run the first true branch. Test 80 before 90 and a score of 95 becomes BA. The Letter Grades problem checks this order.
Python Data Structures Guide: Stacks, Trees, and Graphs with Interactive Interview Problems
Master stacks (monotonic stack, heaps), binary trees (BST, DFS, BFS), and graphs (topological sort, Dijkstra) in Python with clean code templates and 30+ interactive interview challenges.
10 Core Algorithms Every Python Developer Should Know to Become an AI Engineer (2026)
From cosine similarity to BPE tokenization, BM25 to beam search: learn the 10 core algorithms that power LLM and RAG systems and show up in AI engineering interviews, implemented in pure Python.
Best Python Courses for Intermediate Learners: Udemy, Coursera & Book Recommendations (2026)
Curated 10-minute guide to the best Python courses for intermediate learners in 2026. Compare top Udemy deep-dives, Coursera specializations, must-read Python books, and an 8-week study roadmap.
Python Intermediate Level Project Ideas: 8 Portfolio-Ready Engineering Projects (2026)
Stop building basic calculators and to-do apps. Explore 8 intermediate level Python project ideas with working code architectures, async pipelines, custom data structures, and interview-ready system designs.
Most Asked Python Interview Questions and Answers (2026)
The most frequently asked technical Python interview questions, tricky output puzzles, and working code examples. Read this complete guide from junior to senior level before your interview.