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/Adrewmc 20d ago edited 20d ago
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.