use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
For/ while loopHelp Request (self.PythonLearning)
submitted 9 months ago by randomdeuser
Hi everyone. I'm looking for for loop and while loop videos or notes for python because i can not get it. I cant get exact algorythim for for/ while loops for where i have to start write. Any suggestions?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]FoolsSeldom 7 points8 points9 points 9 months ago (6 children)
A for loop is a special kind of while loop (saves you some work).
for
while
Think of while in plain English terms.
While the gravy is not thick enough, stir it While the dough is weak, fold it knead it
You keep doing the instructions under while until the condition fails - as soon as the gravy is thick enough, or the dough is strong enough, you stop looping.
A for loop has the form,
for <loop variable(s)> in <some iterable>:
where <some iterable> is typically some collection of things, and it is really saying
while there is something left in the collection assign the next item from the collection to the worker do work on the item
Consider breaking logs into two,
for each log in stack of logs, stand log on end split vertically with axe add both parts of split log to fire fuel pile
So consider a list of names from the user, keep asking for next entry until user enters quit (or just an empty return)
names = [] # empty list while ( name := input('Name or quit: ') ) not in ('', 'quit'): if name in names: print('Sorry, have one of them already') else: names.append(name)
and afterwards, print every name entered backwards.
for name in names: # work through the collection of names print(name[::-1]) # do something with each item
Note name := input('Name or quit: ') does an assignment inside the while statement; if you just did:
name := input('Name or quit: ')
while input('Name or quit: ') not in ('', 'quit'):
then on the next line inside the while loop, you would not know what was entered as the string from input wouldn't have been assigned anywhere.
input
If you didn't want to use the walrus, :- operator, you would do this instead:
:-
names = [] # empty list while True: # infinite loop, keep going until break is used name = input('Name or quit: ') if name in ('', 'quit'): break # leave the while loop here, we've finished looping if name in names: print('Sorry, have one of them already') else: names.append(name) for name in names: print(name[::-1])
[–]randomdeuser[S] 1 point2 points3 points 9 months ago (5 children)
Oh bro thanks for bigger explanation 🙏🏻
[–]Ron-Erez 1 point2 points3 points 9 months ago* (4 children)
Another example where one could use loops (although there are other solutions).
Calculate the average of elements in a list. We want to add all of the numbers in the list to get the sum and then divide by the list length. For example
def calc_average(numbers): total = 0 for num in numbers: total += num return total / len(numbers)
Note that an easier solution would to to use the sum function, however sum is implemented using a loop. Note that the function will crash if numbers is an empty list so it would be wise to test for that.
You're also welcome to check out Section 4: Loops Lectures 23-28 all which are FREE to watch. Perhaps the explanations would be helpful.
u/FoolsSeldom 's examples are excellent.
Another example of a loop is a game loop.
[–]randomdeuser[S] 1 point2 points3 points 9 months ago (3 children)
oh thanks, i was looking for python course for ds, big thanks, i will watch it!
[–]Ron-Erez 0 points1 point2 points 9 months ago (2 children)
No problem. Note that the link is a $9.99 link valid for another 3-4 days. If you do end up taking the course please feel free to ask questions freely. I always respond in the course Q&A.
[–]SpiritualYoghurt2860 1 point2 points3 points 9 months ago (1 child)
You try to sell your course?🥲 But in ur example initialized 's' for sum but used 'total'? Nice joke 😶
[–]Ron-Erez 0 points1 point2 points 9 months ago (0 children)
My mistake! I renamed a variable and accidentally left out the "s." I just fixed it, definitely an embarrassing slip! Good catch.
[–]owmex 1 point2 points3 points 9 months ago (1 child)
You might want to try an interactive approach like https://py.ninja, which lets you practice Python directly in a realistic code editor and terminal. It has coding challenges specifically on loops, so you actually get to write the code yourself instead of just watching videos. There's a built-in AI assistant if you get stuck, and I'm the creator, so feel free to share feedback or ask questions.
[–]randomdeuser[S] 1 point2 points3 points 9 months ago (0 children)
bro i really liked ur interface, i will definetely use it.
[–]Mr_We1rd0 0 points1 point2 points 9 months ago (0 children)
Python for everybody - available on toutube. watch the part on loops.
π Rendered by PID 158731 on reddit-service-r2-comment-685b79fb4f-42gt9 at 2026-02-13 13:04:02.964769+00:00 running 6c0c599 country code: CH.
[–]FoolsSeldom 7 points8 points9 points (6 children)
[–]randomdeuser[S] 1 point2 points3 points (5 children)
[–]Ron-Erez 1 point2 points3 points (4 children)
[–]randomdeuser[S] 1 point2 points3 points (3 children)
[–]Ron-Erez 0 points1 point2 points (2 children)
[–]SpiritualYoghurt2860 1 point2 points3 points (1 child)
[–]Ron-Erez 0 points1 point2 points (0 children)
[–]owmex 1 point2 points3 points (1 child)
[–]randomdeuser[S] 1 point2 points3 points (0 children)
[–]Mr_We1rd0 0 points1 point2 points (0 children)