Differences Between For & While Loops (in Python)
Вставка
- Опубліковано 27 вер 2024
- 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.codeskulpto....
Unrolled For Loop: www.codeskulpto....
Basic While Loop: www.codeskulpto....
While Loop Used Like a For Loop: www.codeskulpto....
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 is the best explanation of the difference of these loops.......... TYSM
this is the most interesting for and while loops thing I've seen. Thanks.
Really good "concretization" of the two concepts. I'm a month into studying Python, my first computer language. Thanks!
: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
7 years later and still the best source I could find. Thanks a billion! (better break you my for loop for that one)
The best ever explanation ! loved the way u cleared my doubt.....
Thanks a lot supporting you from Afghanistan.......
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.
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?
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 :-)
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!
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!
Appreciate the explanation and also thank you for all you do for this country!
This video really helped me understand the difference between for and while loops. Thanks!
Holy shit this is the most innovative method of explanation, as well as the easiest.. thanks a lot!
Hahahaha I shall try this demonstration tomorrow 😂😂😂 thanks dude!
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! :)
great demonstration
this video is very underrated! Even added some humor this vid is amazing :D btw who's watching in 2020??
Watching in 2022
thanks buddy, this was super helpful!!
You're something else buddy you just made my day with your fun demonstration
THE best explanation!
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.
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.
Super explanation................. great bro........
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 👍
Best explanation ever
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
Fantastic explanation really clear and easy to understand
Good real life demo for using loops. I like it
This code loops past 100; you could be doing a lot of press ups!!
Here is the correct code:
import random
count = 0
desired_count = random.randrange (0, 100)
print "desired count=" + str(desired_count)
while count < desired_count:
print "Down!"
print "Up!"
count = count + 1
print str(count) + " done"
+Mark Lawson Howdy Mark! You are absolutely correct that the while loop could loop past 100. If I'm really unlucky Sarge will never get tired and I'll be doing pushups until my arms fall off. Not fun!
I used a while loop in that example because I DON'T KNOW how many pushups I'll need to do BEFORE I start the task. In your example code, it establishes the desired_count value PRIOR to entering the pushup loop. Since you know how many pushups you'll be doing BEFORE the loop, that is the perfect time to use a FOR loop instead of a WHILE loop.
Here's a modified version of your program that uses a for loop instead so you don't have to keep track of your own count variable:
import random
desired_count = random.randrange (0, 100)
print "desired count=" + str(desired_count)
for pushup in range(1,desired_count+1):
print "Down!"
print "Up!"
print str(pushup) + " done!"
I like your program because it limits me from ever doing more than 100 pushups. THANK YOU!!! :D
Hey you are welcome. I really enjoyed your video. I am fairly new to coding, hence watching UA-cam vids etc. I got frustrated at getting ranges over 100 and wanted to fix it, just to only satisfy my own sanity. Keep the great work!!
you are an amazing teacher.. wish i had u in my school
Sasi Maarika thanks!
thanks man! it really showed me how things practically work
This is a REALLY good explanation Thanks, dude!
Also, I liked Sarge's yawn.
Amazing explanation
I am watching this in 2021, Barron please upload more tutorials on python.
It would be awesome if you start a new updated series of python with practical examples and practical approach it will definately help you gain new subscribers and you can even put ads on it and will help you to generate some revenue from it.
Amazing Explanation
Thanks for helping me to understand loops a lot better
this is good interest. Thanks brother
Amazing explanation!
thanks, this was very helpful explaination
I know I'm late, but thank you so much!
Hahahaha Best Trick ever to teach simple and easy to understand.
Thank you for this, it really helps
This is perfect dude
Great video! Really helped me in my python class.
you are doing the Lord's work.. Thought I let you know.
thank you for making it entertaining. You are a great teacher.
Great analogy!
very good explanation!!!
I actually stumbled across this video because I was mildly confused about the difference between if and while. I think I got it now, even though that wasn't even addressed in this video xD
Hi Barron, Thanks for this awesome video. Nice way of explaining loops. I am a beginner programmer and this has helped me a lot!
I wish I would’ve found this video two days ago when I was stuck on a loop
That was a really good explanation. Thanks!
While (no pun intended) at this point in my life I am quite familiar with both types of loops, I found your explanation to be very entertaining and informative. As I was in the Navy, I'll say to you "Bravo Zulu" meaning "Well Done". Thanks!
ohhh man you are awesome....that 4:19 Sarge lol.....
This is amazing!
Great video. I'll use it to teach some programming. Thanks!
xD its fun to laugh when learning. good show bro.
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.
Great job!!
Your videos are really good man! Help me a lot. Thank you.
Nice 😁💖
You are a God among men
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!
Very useful thanks
Thanks! That was most enlightening!
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 :)
Useful video
HHAHHAAH GREAT VID I LOST IT WHEN HE LOOK UP AT THE SARGE AND HE SHOOK HIS HEAD.GREAT EXPLANATION! 1 LIKE FOR U!
Awesome lession!
Thanks man - this helped a lot (even with Java)
Thanks so much!
great vid man
Awesome vid dude
Thanks :)
very Clear, Thanks, man!
Really helpful video and great analogy. : )
Thank you so much! This was really helpful
I will show this to my gym instructor
He always makes me 50 pushups
Thanks explained very well
Nice explanation. The range is from 1 to 100, But at 6:40, how can it iterate till 150?
Every iteration of the loop picks a random number between 0 and 99 to determine whether or not sarge falls asleep (he goes to sleep when it randomly turns up as 0). Each iteration of the loop is independent from the previous iteration. The process could potentially go on forever if a 0 never comes up.
Think of it like you're rolling a six-sided die (numbers 1 through 6) repeatedly until you roll a 1. Each roll is *random* and *independent* of the other rolls... you might roll 1 on your first try... or you might have to roll 1,000,000,000 times to finally get a 1 if you have really bad luck. You are not guaranteed to roll a 1 within your first 6 rolls.
@@barronstone what's. The use of range here then
great and funny tutorial keep it up man!
helpful explanation and beautiful painting in the background :)
+iashrai Thanks! My wife painted them.
genial teacher!
Thanks!
Are you related to Ben Stone and Chris Stone? They were in our homeschool group in the 90s.
Still using on 01/03/2022!
The concepts are still the same :-)
please explain nested for loop like this ??
this s awsome way
Thanks bro
More handsome than Ryan Gosling
You could even change or start channel with the name
Deadpool codes
I want to know the difference between them in memory ?
Thumbs up
More handsome than Ryan Gosling
You could even change or start channel with the name
Code With Deadpool
Good video but I don't understand why you range goes up to 21?
i found someone's idea and ran the code. It shows that it starts from 99 and down. Why does it start from 99 and not 100?
count = 100
while count > 0:
count = count - 1
print(count)
if count == 0:
print("blast off")
Foxxel Fire you are decrementing that value of the count variable before you print it, so on the first time through the loop "count" goes from 100 to 99 before you print out the value.
Swap the order of two lines so:
print(count)
count = count - 1
That should make the program count down from 100 like you expect.
oh it works now. thanks
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)