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

КОМЕНТАРІ • 211

  • @gtrspctr
    @gtrspctr 7 років тому +68

    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.

  • @vimukthikulasekara2109
    @vimukthikulasekara2109 4 роки тому +1

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

  • @enhua88
    @enhua88 9 років тому +15

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

  • @andrewfoster883
    @andrewfoster883 6 років тому +5

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

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

  • @Ari-jk3jl
    @Ari-jk3jl 4 роки тому

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

  • @waisyousofi9139
    @waisyousofi9139 2 роки тому

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

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

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

  • @hankhyten
    @hankhyten 5 років тому +13

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

    • @barronstone
      @barronstone  5 років тому +1

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

  • @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 2 роки тому +1

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

  • @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!

  • @CJTheeGuy
    @CJTheeGuy 7 років тому +1

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

  • @charlesrodriguez491
    @charlesrodriguez491 7 років тому +2

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

  • @TBroGaming
    @TBroGaming 4 роки тому

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

  • @OnlineMathsAcademy
    @OnlineMathsAcademy 2 роки тому +1

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

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

  • @5timulat10n
    @5timulat10n 2 роки тому +1

    great demonstration

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

  • @laurenknott2204
    @laurenknott2204 2 роки тому +1

    thanks buddy, this was super helpful!!

  • @reaganscofield9654
    @reaganscofield9654 6 років тому

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

  • @RogerThatRob
    @RogerThatRob 4 роки тому

    THE best explanation!

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

  • @benzdoct
    @benzdoct 10 років тому

    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.

  • @manjunathansiva1706
    @manjunathansiva1706 4 роки тому

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

  • @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 👍

  • @srtech8218
    @srtech8218 6 років тому

    Best explanation ever

  • @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!

  • @juliehodsdon131
    @juliehodsdon131 8 років тому

    Fantastic explanation really clear and easy to understand

  • @billgilmore8063
    @billgilmore8063 9 років тому

    Good real life demo for using loops. I like it

  • @shazzysexy1
    @shazzysexy1 8 років тому +2

    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"

    • @barronstone
      @barronstone  8 років тому +1

      +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

    • @shazzysexy1
      @shazzysexy1 8 років тому

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

  • @sasimaarika14
    @sasimaarika14 6 років тому

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

  • @Competent00
    @Competent00 6 років тому

    thanks man! it really showed me how things practically work

  • @ashwathjadhav2859
    @ashwathjadhav2859 5 років тому +2

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

  • @timmyhuynh3859
    @timmyhuynh3859 2 роки тому

    Amazing explanation

  • @shreehari2589
    @shreehari2589 3 роки тому +1

    I am watching this in 2021, Barron please upload more tutorials on python.

  • @jatinmali9310
    @jatinmali9310 3 роки тому +1

    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.

  • @dhruvyadav1492
    @dhruvyadav1492 4 роки тому

    Amazing Explanation

  • @colaphatgirl
    @colaphatgirl 10 років тому

    Thanks for helping me to understand loops a lot better

  • @vidunmanmitha8013
    @vidunmanmitha8013 3 роки тому

    this is good interest. Thanks brother

  • @mattwandcow
    @mattwandcow 4 роки тому

    Amazing explanation!

  • @PlasmoX
    @PlasmoX 4 роки тому

    thanks, this was very helpful explaination

  • @javidsarwari9991
    @javidsarwari9991 2 роки тому +1

    I know I'm late, but thank you so much!

  • @Tech_Wala_Friend
    @Tech_Wala_Friend 3 роки тому +1

    Hahahaha Best Trick ever to teach simple and easy to understand.

  • @AK_00704
    @AK_00704 3 роки тому

    Thank you for this, it really helps

  • @MRTECH-ym7lm
    @MRTECH-ym7lm 3 роки тому

    This is perfect dude

  • @brandonvolesky9867
    @brandonvolesky9867 8 років тому +1

    Great video! Really helped me in my python class.

  • @Domic462
    @Domic462 3 роки тому

    you are doing the Lord's work.. Thought I let you know.

  • @Tran0370
    @Tran0370 10 років тому

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

  • @MariamSalif
    @MariamSalif 3 роки тому

    Great analogy!

  • @김규리-v5w
    @김규리-v5w 5 років тому

    very good explanation!!!

  • @Zwerggoldhamster
    @Zwerggoldhamster 8 років тому

    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

  • @freeaman
    @freeaman 7 років тому

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

  • @BarnyWaterg8
    @BarnyWaterg8 2 роки тому +1

    I wish I would’ve found this video two days ago when I was stuck on a loop

  • @malvikachandhok904
    @malvikachandhok904 4 роки тому

    That was a really good explanation. Thanks!

  • @creeryk
    @creeryk 10 років тому +1

    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!

  • @adityachoudhary4306
    @adityachoudhary4306 2 роки тому +1

    ohhh man you are awesome....that 4:19 Sarge lol.....

  • @Ledesma184th
    @Ledesma184th 4 роки тому

    This is amazing!

  • @antoniobojorges3605
    @antoniobojorges3605 10 років тому

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

  • @amanlearning1474
    @amanlearning1474 8 років тому +3

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

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

  • @jamesclarity1077
    @jamesclarity1077 6 років тому

    Great job!!

  • @DanielOMahony
    @DanielOMahony 10 років тому

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

  • @KaranSinghD-yj9ep
    @KaranSinghD-yj9ep 2 роки тому +1

    Nice 😁💖

  • @nicholaschryssafis5797
    @nicholaschryssafis5797 5 років тому

    You are a God among men

  • @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!

  • @jacekicksass
    @jacekicksass 4 роки тому

    Very useful thanks

  • @elaineellerton19
    @elaineellerton19 9 років тому

    Thanks! That was most enlightening!

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

  • @kimochineina1
    @kimochineina1 4 роки тому

    Useful video

  • @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!

  • @HelionDark
    @HelionDark 7 років тому

    Awesome lession!

  • @joshuajaynes88
    @joshuajaynes88 8 років тому

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

  • @turkuekin3574
    @turkuekin3574 4 роки тому

    Thanks so much!

  • @scottdoucet5095
    @scottdoucet5095 3 роки тому

    great vid man

  • @julianlankstead
    @julianlankstead 8 років тому

    Awesome vid dude

  • @speconcloud9
    @speconcloud9 2 роки тому +1

    Thanks :)

  • @joelmachango
    @joelmachango 7 років тому

    very Clear, Thanks, man!

  • @AK-jg8gb
    @AK-jg8gb 5 років тому

    Really helpful video and great analogy. : )

  • @ying1296
    @ying1296 5 років тому

    Thank you so much! This was really helpful

  • @RinkumoniKhanikar
    @RinkumoniKhanikar 2 роки тому +1

    I will show this to my gym instructor
    He always makes me 50 pushups

  • @comkraft
    @comkraft 6 років тому

    Thanks explained very well

  • @Adks007
    @Adks007 6 років тому +1

    Nice explanation. The range is from 1 to 100, But at 6:40, how can it iterate till 150?

    • @barronstone
      @barronstone  6 років тому +1

      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.

    • @mulebayle1784
      @mulebayle1784 2 роки тому

      @@barronstone what's. The use of range here then

  • @amr1973
    @amr1973 7 років тому

    great and funny tutorial keep it up man!

  • @iashrai
    @iashrai 8 років тому

    helpful explanation and beautiful painting in the background :)

    • @barronstone
      @barronstone  8 років тому +3

      +iashrai Thanks! My wife painted them.

  • @MMABeijing
    @MMABeijing 5 років тому

    genial teacher!

  • @AustraliaDrone
    @AustraliaDrone 9 років тому

    Thanks!

  • @chrisreynolds6391
    @chrisreynolds6391 6 років тому

    Are you related to Ben Stone and Chris Stone? They were in our homeschool group in the 90s.

  • @abdullahahmed5695
    @abdullahahmed5695 2 роки тому

    Still using on 01/03/2022!

    • @barronstone
      @barronstone  2 роки тому

      The concepts are still the same :-)

  • @ebezgin
    @ebezgin 5 років тому

    please explain nested for loop like this ??

  • @sasimaarika14
    @sasimaarika14 6 років тому

    this s awsome way

  • @technology7706
    @technology7706 7 років тому

    Thanks bro

  • @abhikkumarde1053
    @abhikkumarde1053 3 роки тому

    More handsome than Ryan Gosling
    You could even change or start channel with the name
    Deadpool codes

  • @mohamedatef91994
    @mohamedatef91994 6 років тому

    I want to know the difference between them in memory ?

  • @waqasjan998
    @waqasjan998 6 років тому

    Thumbs up

  • @abhikkumarde1053
    @abhikkumarde1053 3 роки тому

    More handsome than Ryan Gosling
    You could even change or start channel with the name
    Code With Deadpool

  • @77Sherms
    @77Sherms 4 роки тому

    Good video but I don't understand why you range goes up to 21?

  • @clementinerosette1038
    @clementinerosette1038 7 років тому

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

    • @barronstone
      @barronstone  7 років тому

      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.

    • @clementinerosette1038
      @clementinerosette1038 7 років тому

      oh it works now. thanks

  • @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)