Differences Between For & While Loops (in Python)

preview_player
Показать описание
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):
Рекомендации по теме
Комментарии
Автор

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.

gtrspctr
Автор

this video is very underrated! Even added some humor this vid is amazing :D btw who's watching in 2020??

Alex-nshj
Автор

I am gonna remember this for my entire life, thanks to u r methodology

abhaypatil
Автор

this is the best explanation of the difference of these TYSM

vimukthikulasekara
Автор

Really good "concretization" of the two concepts. I'm a month into studying Python, my first computer language. Thanks!

andrewfoster
Автор

7 years later and still the best source I could find. Thanks a billion! (better break you my for loop for that one)

Ari-jkjl
Автор

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!

wcsamms
Автор

this is the most interesting for and while loops thing I've seen. Thanks.

enhua
Автор

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.

tody
Автор

Hey Tina! Thanks for the support!  YouTube 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.

barronstone
Автор

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.

jaybartgis
Автор

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

pel_wang
Автор

The best ever explanation ! loved the way u cleared my
Thanks a lot supporting you from

waisyousofi
Автор

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!

HxCPokemonMaster
Автор

: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

Kilopillz
Автор

This is a great explanation. I was very confused before watching this video. GREAT EXPLANATION!!!

luisduran
Автор

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!

thegoodgorilla
Автор

Thank you so much. I just wasn't getting the difference after reading about it and this finally made it click for me

haleyshelnutt
Автор

Appreciate the explanation and also thank you for all you do for this country!

CJTheeGuy
Автор

Excellent. Truly the best video to explain this. Thank you a lot for all of your push ups!

beelu
welcome to shbcf.ru