For Loops & While Loops in Python - Beginner Python Tutorial #5 (with Exercises)

Поделиться
HTML-код
  • Опубликовано: 13 мар 2018
  • Fifth video in my python tutorial series. In this video we cover the two different types of loops, for & while loops. Loops allow you to repeatedly execute blocks of code. For loops work great for a set number of iterations as well as iterating through lists. While loops work well with booleans (true or false).
    If you have any questions, leave a comment down below!
    Feel free to watch on 1.25x or 1.5x speed.
    SUBSCRIBE if you enjoyed this video!! More tutorials to be uploaded real soon.
    -------------------------
    PYTHON TUTORIAL SERIES:
    Setup & Installation: • Why Should you Learn P...
    1. Math & Variables: • Math & Variables in Py...
    2. Conditional Statements (if, elif, else): • Conditional Statements...
    3. Functions: • Functions in Python - ...
    4. Lists & Tuples: • Lists & Tuples in Pyth...
    5. For Loops & While Loops: • For Loops & While Loop...
    Learn to Program a game (mentioned at the start of this video): • How to Program a Game!...
    -------------------------
    Follow me on social media!
    Instagram | / keithgalli
    Twitter | / keithgalli
    -------------------------
    If you are curious to learn how I make my tutorials, check out this video: • How to Make a High Qua...
    ⭐ Kite is a free AI-powered coding assistant that will help you code faster and smarter. The Kite plugin integrates with all the top editors and IDEs to give you smart completions and documentation while you’re typing. I've been using Kite for 6 months and I love it! www.kite.com/get-kite/?...
    *I use affiliate links on the products that I recommend. I may earn a purchase commission or a referral bonus from the usage of these links.

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

  • @KeithGalli
    @KeithGalli  6 лет назад +21

    Video Outline!
    1:09 - Intro to For Loops
    2:09 - Getting Iteration Number
    4:10 - For Loops + Lists
    5:31 - Calculating the average value in a list
    10:21 - For Loops + Lists (index method)
    12:33 - Using the Enumerate function
    13:27 - Intro to While Loops
    15:02 - Counting to 10 with While loop
    18:18 - Break & Continue Statements
    21:17 - Coding Bat Example Problems
    24:53 - Coding Bat Example #2
    30:33 - Video Recap
    Feel free to watch on 1.25x or 1.5x speed to learn faster!

  • @cambridgebreaths3581
    @cambridgebreaths3581 4 года назад +12

    man, honestly you are the coolest programmer. I will start enjoy watching your python and NN videos systematically.

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

    where were you 4 months ago when I statrted teaching myself python.. you are the man bro.. thanks

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

    I used to program in C/C++ using the Windows APi (Petzold book) and it was a complicated process!! Python is so much easier!! Thank you for these tutorials!!

  • @sylvesterkanev2918
    @sylvesterkanev2918 Год назад +2

    Hey, Keith. Great video as always! Could you make another one regarding the nested for loops (for loops in for loops) and nested if statements (if statement in if statement)? Stay safe!

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

    Keith your videos a very easy to understand, So helpful Thanks..

  • @debitsandcreditsclub
    @debitsandcreditsclub 11 месяцев назад

    Thanks. I always learn something new watching your videos.

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

    Very helpful, thanks a lot!

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

    Man! you are simply awesome

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

    u r da man, bruv

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

    you just helped me after being stuck for 2 hrs on this while loop.

  • @christofweber8965
    @christofweber8965 5 лет назад +3

    Love your tutorials, easy to follow and good content for beginners. Short question on the Codebat "String_time": did you use the loop intentionally as one could have solved it by simply stating: return str*n
    Thanks again for your videos!

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

      Thanks for the kind comments! Yea so I used the loop intentionally just to demonstrate an example of what this video was about. Your solution to return str*n would be more optimal in general.

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

      Thanks for your quick response! Ok, understood .. makes sense. Btw: would you have some recommendations with regard to next steps once finished with your tutorials? Codingbat or any other resources for becoming more advanced? Thanks!

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

      Well I guess it really depends on what your programming goals are. What type of stuff do you think is cool? Some potential areas could be game development, robotics, machine learning, app development, etc. Let me know and I'll try to point you to some good resources

  • @meliyomollameles
    @meliyomollameles 4 месяца назад

    It's my first time here but i liked you the first minute the video started and the second you started talking. I don't think I am going anywhere.

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

    thanks to you i know python

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

    For the 24:31 Exercise, I want to ask that why my code doesn't work? What's the difference?
    def string_times(str, n):
    for i in range(n):
    return str*n

  • @mirsadkuljici2737
    @mirsadkuljici2737 2 года назад +2

    Can someone tell me why do we put a zero number when getting a total value for smth (7:19) .. maybe the question is absurd but I am a beginner. Thank you in advance!

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

      Because you can't add to a variable that doesn't exist yet. It also needs to be a number for adding to make any sense. You couldn't add 1 to a function object so Python needs to know it's a number first.

  • @BN-hy1nd
    @BN-hy1nd 4 года назад +1

    For an absolute, absolute beginner, how do we arrange numbers in ascending or descending order/ Thank you

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

      Watch his other Python tutorials it's explained in number 4 I think

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

    Hi man where i can practice more exercises?

  •  2 года назад

    You can also do this:
    def string_times(str, n):
    return (str * n)

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

    THank you :)

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

    24:41 For whatever reason this works too...
    def array123(nums):
    if 1 and 2 and 3 in nums:
    return True
    else:
    return False

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

      lol its so good but only for this situation

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

      But this code will always return True even if the sequence '1, 2, 3' is not in order. For example, print(array123([1, 1, 3, 2, 1])) will still return True as it contains 1, 2 and 3 but not in that order.

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

    i = 2
    n = 1
    while n

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

      from time import sleep
      i = 2
      while True:
      print(i)
      i *= i
      sleep(1)

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

    Nice

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

    What is "i". Can it be something else instead of "i" when you type "for 'i' in range (10):" ?

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

      Yes. You ‘I’can be almost anything.

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

      Yes i can be anything it may be a variable name. This i or any variable is usually acts like a counter of for loop

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

    takk fyrir !!

  • @julianjoseph5679
    @julianjoseph5679 7 месяцев назад

    Hey man thx preparing for exam 😅

    • @KeithGalli
      @KeithGalli  7 месяцев назад

      Hope the exam went well!

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

    def string_times(str,n):
    non = int(n)
    return str*non

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

    Hello. First i want to say that i really appriciate your work. Im a 13 yrs old kid and im learing python rn cuz i want to work like software engineer. I can write a simple codes but when i try to solve problems in this site i cannot. I really cannot understand the strings.

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

      Hmm I'm not sure I understand your problem. Can you try to be a little bit more specific?

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

      @@KeithGalli when i have an idea to do some sinple code i do it. But if i have it like an exercise like in this site i cannot figure it out. Also im watching your tutorial to do a game and i understand the code but i cannot write it by myself. Btw thank you so much for answering me ur the best❤️

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

      Check out this video for additional guidance on solving the coding bat problems. They are not easy at first, but with enough practice you should be able to start getting them! ruclips.net/video/lx7oqZ7Nl3k/видео.html

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

    5:41

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

    keith your videos are awesome ~ can you make on django app on web with python ! step by step .. how to!

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

    so for some reason, it says this is invalid
    game_over = False
    score = 0
    while not game over:
    print("playing game")

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

      can any of you guys tell me whats wrong

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

      @@rtmbiking67 Well you used the wrong variable in your while loop. It should be game_over rather than game over!!😊

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

      Ok thanks

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

    Index of 25 positions with weights varying from about 180 to about 250, and you have what looks like an MIT flag on your wall. I would guess the Bruins.

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

      You are correct! I'm a big bruins fan :)

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

      @@KeithGalli Well as a Hawks fan, I'm just happy it wasn't Detroit. Cus then I'd have a dilemma on my hands lmao. I spent 4 years living in Boston, so I can get down with Bruins fans. Thanks for all the videos and all the work that you do here man. It's awesome.

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

    hey just never say standard deeev. Other than that it was a great review thanks big fudge

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

    CodingBat.com: good path as a learning aid: thanks Keith;

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

    i have big problem guys that i cant understand the task any body can help me and tell me anything that make me better in that

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

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

    weights = Red Sox lineup?

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

      That was my thought! MIT in the background and 25 players.

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

    side effects of using visual studio code saving all the time it happens with me too bro lollz

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

    Red Sox

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

      You were very close, but actually took the numbers from the Bruins!

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

    how can one improve python skills after watching your videos

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

    bruins!

  • @cheng-juyeh525
    @cheng-juyeh525 Год назад

    god bless u xdd

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

    which team was it lol

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

    i write a code weights = [1, 2, 5, 7, 9, 8]
    total_weights = 0
    for weight in weights:
    total_weights += weight
    print(total_weights)
    and get:
    1
    3
    8
    15
    24
    32
    why not sum ?

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

      Move: print (total_weights) out of the loop. Should be vertically aligned to: for weight in weights: .