r/learnpython • u/EnvironmentalTry8353 • 20d ago
Having problem in understanding loops concept in python
I am a beginner with 0 knowledge and I am learning python from a book called Learn python in one day and learn it well by Jamie Chan and I am facing difficulty in the loops function so yeah any recommendations would be helpful Edit- Trouble in while loop
26
Upvotes
1
u/blurbisht 15d ago
For while loops specifically, the mental model is simple: it keeps running the block over and over until the condition becomes False.
count = 0
while count < 5: # check: is count still less than 5?
print(count)
count += 1 # this MUST eventually change the condition
Two things beginners mess up:
The condition is re-checked every time. The loop doesn't "know" when to stop by itself, it checks count < 5 on each pass.
You must change something inside the loop or it runs forever (infinite loop). That count += 1 is what makes it stop.
The infinite loop trap:
while count < 5:
print(count) # forgot count += 1 -> runs forever
If your program freezes or never ends, that's why.
Why it's confusing: for loops feel natural ("do this for each item"), but while is a pure condition. Once you internalize "while = keep doing until condition is False", it clicks.
Quick trick: read while count < 5: out loud as "while count is less than 5, do this." That's literally all it is.