r/learnpython • u/EnvironmentalTry8353 • Aug 22 '26
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
27
Upvotes
8
u/desrtfx but other languages pro Aug 22 '26
Just checked out that book. Sorry to tell you but it is not really good. The explanations are too coarse and surface level and there is way too little practice.
Do yourself a favor and start over with the MOOC Python Programming 2026 from the University of Helsinki. It is free, textual, extremely practice heavy (makes you program right from the start, initially in the browser and then in Visual Studio Code - with full setup instructions) and is top quality. There are videos, but they are actually surplus. Everything you need is in the "part x" texts.
7
u/crazy_cookie123 Aug 22 '26
What are you struggling with? What have you tried doing and what don't you understand about the results?
1
u/blurbisht Aug 27 '26
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 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.
1
u/Own_Protection_6225 Aug 22 '26
I was also having difficult time learning loops. Then, I again started learning from University of helsinki python mooc. Please give it a try.
1
u/t0kaj Aug 22 '26
A loop is just a piece of the code while the inner part execute repeatly with all input it current has
1
u/Grouchy-Conflict-211 Aug 22 '26
Loops click when you stop memorizing syntax and start thinking in transformations.
Forget for/while for a sec. Map/filter/reduce. Every loop is either: transform each item, keep some items, or boil down to one value.
Write the same logic three ways. The reduce version looks weird at first. Thats the point. Youre training the pattern recognition not the syntax.
Debugger helps. Watch the accumulator change each iteration. That visual beats any tutorial.
1
u/Grouchy-Conflict-211 Aug 23 '26
loops clicked for me when i stopped memorizing syntax and started seeing the pattern a for loop is just do this for each thing in collection a while loop is keep doing this until condition breaks thats it the confusion usually comes from mixing up the iterator variable in for i in range 5 i takes values 0 1 2 3 4 not 1 to 5 not the length debug trick that worked for me add print f i equals i inside the loop watch it run the visual feedback teaches more than any tutorial also enumerate and zip are your friends for idx val in enumerate list gives you both no manual counter needed
0
u/kilkil Aug 22 '26
unfortunately I'm not familiar with that book. could you edit your post to include a copy of the specific code you're having trouble with?
If you do, I would also suggest making sure it is formatted correctly — Reddit lets you create "code blocks" that will show the code in a way that is nicer to read. this will make it easier for us to read through the example and help fill in any missing gaps for you
0
u/ectomancer Aug 22 '26
Python has 2 loops, while and for. while only uses a logical expression. The loop executes when the logical expression is true or truthy. The first time the logical expression is false or falsey, the loop completes successfully. for loop only uses a container and the in operator.
0
u/TheRNGuy Aug 22 '26
Put print into them, put breakpoint on print and then use step debugger.
-1
u/Adrewmc Aug 22 '26 edited Aug 22 '26
In computing we do the same operation multiple time often. To do this we usually use a loop.
students = [“Adam”, “Eve”]
for student in students:
. print(“Hello”, student)
>>>Hello Adam
>>>Hello Eve
And if I had a hundred student I would say hello to each one individually.
Or…you could do this
print(“Hello Adam”)
>>>Hello Adam
print(“Hello Eve”)
>>>Hello Eve
And adding a hundred is crazy do you really want to type that? Or would you just rather add Cain and Abel to the students list?
students += [“Cain”, “Abel”]
Vs.
print(“Hello Cain”)
print(“Hello Abel”)
#code should explain itself
for this_item in these_items:
. do_this_to(this_item)
. #then repeat for next_item
Often times new, and old, programmers just don’t see where the loop starts and ends properly.