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...

Комментарии • 211

  • @gtrspctr
    @gtrspctr 7 лет назад +69

    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.

  • @Alex-ns6hj
    @Alex-ns6hj 4 года назад +9

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

  • @hankhyten
    @hankhyten 4 года назад +13

    Watching this video in 09/2019. Great explanation of for and while loops. Thanks, Barron!

    • @barronstone
      @barronstone  4 года назад +1

      You're welcome! I'm glad folks are still finding this video helpful :-)

  • @thegoodgorilla287
    @thegoodgorilla287 6 лет назад +2

    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!

  • @wcsamms
    @wcsamms 8 лет назад +2

    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!

  • @andrewfoster883
    @andrewfoster883 6 лет назад +5

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

  • @abhaypatil4214
    @abhaypatil4214 3 года назад +6

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

  • @enhua88
    @enhua88 8 лет назад +15

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

  • @Ari-jk3jl
    @Ari-jk3jl 3 года назад

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

  • @Kilopillz
    @Kilopillz 6 лет назад +12

    :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

  • @lttlgreene
    @lttlgreene 9 лет назад

    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! :)

  • @mahendersam2159
    @mahendersam2159 7 лет назад +4

    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 😊

  • @beelu8295
    @beelu8295 Год назад +1

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

  • @charlesrodriguez491
    @charlesrodriguez491 6 лет назад +2

    This video really helped me understand the difference between for and while loops. Thanks!

  • @tod4y
    @tod4y 7 лет назад

    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.

  • @CJTheeGuy
    @CJTheeGuy 7 лет назад +1

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

  • @DanielPena-nv2nj
    @DanielPena-nv2nj 7 лет назад +7

    Thanks for that great explanation and for your Service!

  • @blkpny
    @blkpny 9 лет назад +2

    AWESOME explanation and love the analogy used. Keep the videos coming!!! Great instructor. Oh, and Thank You for your service!!!

  • @benzdoct
    @benzdoct 9 лет назад

    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.

  • @HxCPokemonMaster
    @HxCPokemonMaster 5 лет назад +1

    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!

    • @barronstone
      @barronstone  5 лет назад +1

      That's a great assignment. Hope it goes well!

    • @gunner2225
      @gunner2225 5 лет назад +1

      I have the same thing. Thanks for the help.

    • @HxCPokemonMaster
      @HxCPokemonMaster 5 лет назад

      @@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?

  • @pel_wang55
    @pel_wang55 3 года назад

    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

  • @waisyousofi9139
    @waisyousofi9139 2 года назад

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

  • @haleyshelnutt4657
    @haleyshelnutt4657 2 года назад +1

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

  • @AnthonyRBarberini
    @AnthonyRBarberini 8 лет назад

    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..

  • @vimukthikulasekara2109
    @vimukthikulasekara2109 4 года назад

    this is the best explanation of the difference of these loops.......... TYSM

  • @reaganscofield9654
    @reaganscofield9654 6 лет назад

    You're something else buddy you just made my day with your fun demonstration

  • @luisduran7668
    @luisduran7668 3 года назад

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

  • @jaybartgis5148
    @jaybartgis5148 9 лет назад

    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.

  • @JR_Davis
    @JR_Davis 7 лет назад

    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!

  • @RecycleGeeks
    @RecycleGeeks 6 лет назад +1

    Thanks a lot for this mate, I finally understood it! Cheers from Sydney!

  • @freeaman
    @freeaman 6 лет назад

    Hi Barron, Thanks for this awesome video. Nice way of explaining loops. I am a beginner programmer and this has helped me a lot!

  • @mohangurusamy1
    @mohangurusamy1 7 лет назад +1

    Very nicely explained, Barron. Thanks!

  • @khantzawhtet6896
    @khantzawhtet6896 3 года назад +1

    Gawd this is still useful in 2020 thank u sir

  • @brandonvolesky9867
    @brandonvolesky9867 7 лет назад +1

    Great video! Really helped me in my python class.

  • @Competent00
    @Competent00 6 лет назад

    thanks man! it really showed me how things practically work

  • @laurenknott2204
    @laurenknott2204 2 года назад +1

    thanks buddy, this was super helpful!!

  • @RogerThatRob
    @RogerThatRob 3 года назад

    THE best explanation!

  • @anirudhsingh5042
    @anirudhsingh5042 Год назад +1

    great demonstration

  • @jirayafire721
    @jirayafire721 Год назад

    What a great teaching method!!!

  • @Tran0370
    @Tran0370 10 лет назад

    thank you for making it entertaining. You are a great teacher.

  • @barronstone
    @barronstone  9 лет назад +1

    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.

    • @jonathansum9084
      @jonathansum9084 8 лет назад

      does the army need people who use python?

    • @barronstone
      @barronstone  7 лет назад +1

      Absolutely. Python is a valuable skill for "cyber warriors" in any branch of the military.

    • @mazenmohammad7452
      @mazenmohammad7452 3 года назад

      You could also use the randrange() method in for loop, but without seting the sarge_is_awake variable ,right?

  • @DanielOMahony
    @DanielOMahony 10 лет назад

    Your videos are really good man! Help me a lot. Thank you.

  • @manjunathansiva1706
    @manjunathansiva1706 4 года назад

    Super explanation................. great bro........

  • @westfield90
    @westfield90 5 лет назад

    Excellent, such a fantastic explanation.

  • @ahmedalwitry
    @ahmedalwitry 8 лет назад

    Really liked the way you explained this!

  • @JPAll
    @JPAll 6 лет назад

    Thanks Barron , this is pretty cool explanation. Thanks for that .

  • @juliehodsdon131
    @juliehodsdon131 8 лет назад

    Fantastic explanation really clear and easy to understand

  • @malvikachandhok904
    @malvikachandhok904 4 года назад

    That was a really good explanation. Thanks!

  • @mattwandcow
    @mattwandcow 4 года назад

    Amazing explanation!

  • @Ledesma184th
    @Ledesma184th 4 года назад

    This is amazing!

  • @billgilmore8063
    @billgilmore8063 9 лет назад

    Good real life demo for using loops. I like it

  • @colaphatgirl
    @colaphatgirl 10 лет назад

    Thanks for helping me to understand loops a lot better

  • @m3coupe4me
    @m3coupe4me 9 лет назад

    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!

  • @AK_00704
    @AK_00704 3 года назад

    Thank you for this, it really helps

  • @jamesclarity1077
    @jamesclarity1077 5 лет назад

    Great job!!

  • @elaineellerton19
    @elaineellerton19 9 лет назад

    Thanks! That was most enlightening!

  • @TBroGaming
    @TBroGaming 3 года назад

    Holy shit this is the most innovative method of explanation, as well as the easiest.. thanks a lot!

  • @subhashismohanty5420
    @subhashismohanty5420 3 года назад +1

    Good Job Man

  • @MariamSalif
    @MariamSalif 3 года назад

    Great analogy!

  • @dhruvyadav1492
    @dhruvyadav1492 4 года назад

    Amazing Explanation

  • @unstoppablepriya
    @unstoppablepriya 9 лет назад

    good lively comparisons..easy to understand nd get the subject..

  • @OnlineMathsAcademy
    @OnlineMathsAcademy 2 года назад +1

    Hahahaha I shall try this demonstration tomorrow 😂😂😂 thanks dude!

  • @antoniobojorges3605
    @antoniobojorges3605 10 лет назад

    Great video. I'll use it to teach some programming. Thanks!

  • @srtech8218
    @srtech8218 5 лет назад

    Best explanation ever

  • @timmyhuynh3859
    @timmyhuynh3859 2 года назад

    Amazing explanation

  • @user-zc5hr3dp2p
    @user-zc5hr3dp2p 5 лет назад

    very good explanation!!!

  • @vidunmanmitha8013
    @vidunmanmitha8013 3 года назад

    this is good interest. Thanks brother

  • @doubleu7658
    @doubleu7658 3 года назад +1

    omg i love this video!

  • @MRTECH-ym7lm
    @MRTECH-ym7lm 3 года назад

    This is perfect dude

  • @ashwathjadhav2859
    @ashwathjadhav2859 5 лет назад +2

    This is a REALLY good explanation Thanks, dude!
    Also, I liked Sarge's yawn.

  • @AK-jg8gb
    @AK-jg8gb 5 лет назад

    Really helpful video and great analogy. : )

  • @julianlankstead
    @julianlankstead 7 лет назад

    Awesome vid dude

  • @HelionDark
    @HelionDark 7 лет назад

    Awesome lession!

  • @PlasmoX
    @PlasmoX 4 года назад

    thanks, this was very helpful explaination

  • @sasimaarika14
    @sasimaarika14 6 лет назад

    you are an amazing teacher.. wish i had u in my school

  • @ying1296
    @ying1296 5 лет назад

    Thank you so much! This was really helpful

  • @amr1973
    @amr1973 6 лет назад

    great and funny tutorial keep it up man!

  • @joelmachango
    @joelmachango 6 лет назад

    very Clear, Thanks, man!

  • @rafaelgalvan4087
    @rafaelgalvan4087 5 лет назад

    so clear thank you

  • @epicwhat001
    @epicwhat001 7 лет назад +1

    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.

  • @rogeremilemanzi406
    @rogeremilemanzi406 4 года назад

    Thank you very much sir. 🙌🏾🙌🏾🙌🏾

  • @joshuajaynes88
    @joshuajaynes88 8 лет назад

    Thanks man - this helped a lot (even with Java)

  • @amanlearning1474
    @amanlearning1474 8 лет назад +3

    xD its fun to laugh when learning. good show bro.

  • @Protoolsle8
    @Protoolsle8 3 года назад

    Great. Thank you.

  • @turkuekin3574
    @turkuekin3574 4 года назад

    Thanks so much!

  • @iashrai
    @iashrai 8 лет назад

    helpful explanation and beautiful painting in the background :)

    • @barronstone
      @barronstone  8 лет назад +3

      +iashrai Thanks! My wife painted them.

  • @scottdoucet5095
    @scottdoucet5095 3 года назад

    great vid man

  • @TinaMaddox0
    @TinaMaddox0 9 лет назад

    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 :)

  • @bsummer
    @bsummer 2 года назад

    and this was 2013, its 2022 and im now grasping the concept so well

  • @kylesmart5521
    @kylesmart5521 9 лет назад

    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)

  • @kimochineina1
    @kimochineina1 4 года назад

    Useful video

  • @jacekicksass
    @jacekicksass 4 года назад

    Very useful thanks

  • @comkraft
    @comkraft 6 лет назад

    Thanks explained very well

  • @mdrafiqul2898
    @mdrafiqul2898 3 года назад +1

    I followed each and every step you mentioned. Now I lost weight along with learning programming. lol
    anyway, great explanation.... an awesome video....

    • @barronstone
      @barronstone  3 года назад +1

      Fitness and programming go well together.

  • @ev4d241
    @ev4d241 4 года назад

    HHAHHAAH GREAT VID I LOST IT WHEN HE LOOK UP AT THE SARGE AND HE SHOOK HIS HEAD.GREAT EXPLANATION! 1 LIKE FOR U!

  • @thegreatestpotato6460
    @thegreatestpotato6460 3 года назад

    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.

    • @barronstone
      @barronstone  3 года назад +3

      It's never too late to learn something new 👍

  • @vanman118
    @vanman118 5 лет назад +1

    What would be the difference of doing "while count < 20:" instead of "if count < 20:"?

  • @ebezgin
    @ebezgin 5 лет назад

    please explain nested for loop like this ??

  • @MMABeijing
    @MMABeijing 4 года назад

    genial teacher!

  • @AustraliaDrone
    @AustraliaDrone 9 лет назад

    Thanks!