Differences Between For & While Loops (in Python)
HTML-код
- Опубликовано: 18 ноя 2013
- A demonstration of For Loops and While Loops in Python with emphasis on how they differ and when to use each one.
This video was made for students of the Rice University Coursera course "An Introduction to Interactive Programming in Python" to help them know which loop to use!
Example Code (in order of appearance):
Basic For Loop: www.codeskulptor.org/#user25_L...
Unrolled For Loop: www.codeskulptor.org/#user25_9...
Basic While Loop: www.codeskulptor.org/#user25_i...
While Loop Used Like a For Loop: www.codeskulptor.org/#user25_3...
This is a REALLY good explanation of the two. I didn't understand what for loops were used for until now. Thanks dude!
Also, I liked Sarge's yawn.
this video is very underrated! Even added some humor this vid is amazing :D btw who's watching in 2020??
Watching in 2022
Watching this video in 09/2019. Great explanation of for and while loops. Thanks, Barron!
You're welcome! I'm glad folks are still finding this video helpful :-)
Hey Barron!
Thanks for your service and the awesome explanation sir!
I've always debated inside my head with these two concepts for a while and now I have a way to visualize it and never forget it again!!
All the best to you!
This was a great help! I've dabbled with programming in the context of scientific data software for many years, but have struggled with the ability to take a blank piece of paper and start from scratch. I've always been confused on these two constructs, but now I know why: most WHILE loops I've seen have actually been used to mimic FOR loops, as in your last example. Thank you!
Really good "concretization" of the two concepts. I'm a month into studying Python, my first computer language. Thanks!
I am gonna remember this for my entire life, thanks to u r methodology
this is the most interesting for and while loops thing I've seen. Thanks.
7 years later and still the best source I could find. Thanks a billion! (better break you my for loop for that one)
:D :D :D :D :D The Push up analogy is by far the best analogy I have ever encountered. Thanks a lot man :D !!!!!! This helped me alot
Thank you for your video! I am new to programming, and working in Python. The examples you came up with, helped me tremendously in being able to understand why my "while loop" had to run an additional "loop" before the condition was satisfied (I.e. It couldn't see that "Sarge" was sleeping before it checked the condition once more at the start of the loop, ha ha love it!)! Keep up the great work, and again thanks for this! :)
Thanks a lot bruh!!! I saw a lot of videos on this topic but i I didn't get it.... But after watching this video I completely understood ur example was apt... Thank you once again 😊
Excellent. Truly the best video to explain this. Thank you a lot for all of your push ups!
This video really helped me understand the difference between for and while loops. Thanks!
Thanks for this! Im an engineer forced to write some code and there is the voice of the university guy saying mockingly "there are even people not understanding the for/while difference in the final year!" ringing in my mind round and round... This has, at least *for* a *while* made it a bit clearer to me.
Appreciate the explanation and also thank you for all you do for this country!
Thanks for that great explanation and for your Service!
AWESOME explanation and love the analogy used. Keep the videos coming!!! Great instructor. Oh, and Thank You for your service!!!
Very nicely done, I like the very explicit names for each variable, operation, and conditions, it does make it easier to follow what you are trying to make happen. Keep up the good work.
I'm working on a project in one of my college courses where I have to demonstrate a potential disadvantage of using a for loop instead of a while loop. This video was an excellent explanation of where each loop shines and I think I have a solid idea for my project now! Many thanks!
That's a great assignment. Hope it goes well!
I have the same thing. Thanks for the help.
@@gunner2225 I guess it's unlikely that we are in the same course but maybe it's worth asking anyway, are you attending a school in Scandinavia?
you have explained well though you were in a military. Better than others, your presentation and explanation is more simpler and easy to under for student like us. thanks to you i can program better now. happy coding
The best ever explanation ! loved the way u cleared my doubt.....
Thanks a lot supporting you from Afghanistan.......
Thank you so much. I just wasn't getting the difference after reading about it and this finally made it click for me
Hey really cool video. I had gone over these two concepts before, but the other videos I came across weren't very clear on the differences. The Drill Sargent pushup example was a great way to explain it..
this is the best explanation of the difference of these loops.......... TYSM
You're something else buddy you just made my day with your fun demonstration
This is a great explanation. I was very confused before watching this video. GREAT EXPLANATION!!!
Excellent video . Summary of what I got out of this:
1. for loop increment updates for the Boolean test are included internally in the parameters.
2. while loop increment updates for the Boolean test are included externally from the parameters in the body: of the loop.
Sorry for my foreign terminology. I'm a Java programmer.
Thanks for the video. Was planning to make my own video for my class to demo this, but didn't have to because you did such a great job!
Hey hey Mr. Davis
Thanks a lot for this mate, I finally understood it! Cheers from Sydney!
Hi Barron, Thanks for this awesome video. Nice way of explaining loops. I am a beginner programmer and this has helped me a lot!
Very nicely explained, Barron. Thanks!
You're welcome!
Gawd this is still useful in 2020 thank u sir
Great video! Really helped me in my python class.
thanks man! it really showed me how things practically work
thanks buddy, this was super helpful!!
THE best explanation!
great demonstration
What a great teaching method!!!
thank you for making it entertaining. You are a great teacher.
Hey Tina! Thanks for the support! RUclips isn't letting me reply directly to your comment so I'll answer your question here.
The randrange() method will return a single, random number from 0 to 99 (the 100 is not included in the range) every time it is called. It gets called once per loop, then we check to see if the random number it returned happens to be zero, and if it is zero then we set the sarge_is_awake variable to False in order to exit the loop. Randrange() has a 1-in-100 probability of returning zero on any given loop iteration because it is randomly choosing from a pool of 100 numbers (0 to 99). On AVERAGE the loop will iterate for 100 cycles, but each individual time you run the program it will run for a random number of cycles until it sees the zero. Sometimes that's less than 100... sometimes it's more!
You can try it for yourself by running the "Basic While Loop" code that's linked in the description above. Just hit the "play" button in the top left corner. You should see a different amount of pushups each time you run it.
does the army need people who use python?
Absolutely. Python is a valuable skill for "cyber warriors" in any branch of the military.
You could also use the randrange() method in for loop, but without seting the sarge_is_awake variable ,right?
Your videos are really good man! Help me a lot. Thank you.
Super explanation................. great bro........
Excellent, such a fantastic explanation.
Really liked the way you explained this!
Thanks Barron , this is pretty cool explanation. Thanks for that .
Fantastic explanation really clear and easy to understand
That was a really good explanation. Thanks!
Amazing explanation!
This is amazing!
Good real life demo for using loops. I like it
Thanks for helping me to understand loops a lot better
Great tutorial. It really helped. My only recommendation would be to include a computer application example like giving a user a predetermined amount of tries on a pin # for an atm. Otherwise, keep it up!
Thank you for this, it really helps
Great job!!
Thanks! That was most enlightening!
Holy shit this is the most innovative method of explanation, as well as the easiest.. thanks a lot!
Good Job Man
Great analogy!
Amazing Explanation
good lively comparisons..easy to understand nd get the subject..
Hahahaha I shall try this demonstration tomorrow 😂😂😂 thanks dude!
Great video. I'll use it to teach some programming. Thanks!
Best explanation ever
Amazing explanation
very good explanation!!!
this is good interest. Thanks brother
omg i love this video!
This is perfect dude
This is a REALLY good explanation Thanks, dude!
Also, I liked Sarge's yawn.
Really helpful video and great analogy. : )
Awesome vid dude
Awesome lession!
thanks, this was very helpful explaination
you are an amazing teacher.. wish i had u in my school
Sasi Maarika thanks!
Thank you so much! This was really helpful
great and funny tutorial keep it up man!
very Clear, Thanks, man!
so clear thank you
while loop is logical which mean it will continue until a certain logic is true.a for loop is iterative increment desc or asc within a range on the list.
Thank you very much sir. 🙌🏾🙌🏾🙌🏾
Thanks man - this helped a lot (even with Java)
xD its fun to laugh when learning. good show bro.
Great. Thank you.
Thanks so much!
helpful explanation and beautiful painting in the background :)
+iashrai Thanks! My wife painted them.
great vid man
First I want to thank you for your service in our military!!! Secondly, I want to thank you for a great tutorial. This explains the operation better than the book I have in my Python class. I have a question. It may be simple but I notice on the random loop the print was higher than 100 which the range was (1, 100). Why did it do that? Thank you so much :)
and this was 2013, its 2022 and im now grasping the concept so well
Hello, I am wondering what type of python you are using 2 or 3? (sorry i am new to programming and can not figure it out myself)
Useful video
Very useful thanks
Thanks explained very well
I followed each and every step you mentioned. Now I lost weight along with learning programming. lol
anyway, great explanation.... an awesome video....
Fitness and programming go well together.
HHAHHAAH GREAT VID I LOST IT WHEN HE LOOK UP AT THE SARGE AND HE SHOOK HIS HEAD.GREAT EXPLANATION! 1 LIKE FOR U!
If you put this in the while loop after the print("Down!") it will only print down.
I wish i learnt how to code when i was younger i am actually enjoying this.
It's never too late to learn something new 👍
What would be the difference of doing "while count < 20:" instead of "if count < 20:"?
please explain nested for loop like this ??
genial teacher!
Thanks!