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

แชร์
ฝัง
  • เผยแพร่เมื่อ 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 3 ปีที่แล้ว +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

  • @FunnyFunnyMoneyHoney
    @FunnyFunnyMoneyHoney 10 หลายเดือนก่อน

    Thanks. I always learn something new watching your videos.

  • @brianwake100
    @brianwake100 3 ปีที่แล้ว

    Keith your videos a very easy to understand, So helpful 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!!

  • @sarahjustme
    @sarahjustme 3 ปีที่แล้ว +1

    Very helpful, thanks a lot!

  • @TheFreeballer1
    @TheFreeballer1 2 ปีที่แล้ว

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

  • @NewfieNL
    @NewfieNL 4 ปีที่แล้ว

    Man! you are simply awesome

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

  • @adamrolander3518
    @adamrolander3518 5 ปีที่แล้ว

    u r da man, bruv

  • @lfggaming4248
    @lfggaming4248 5 ปีที่แล้ว +1

    thanks to you i know python

  • @meliyomollameles
    @meliyomollameles 3 หลายเดือนก่อน

    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.

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

  • @nehabalani7290
    @nehabalani7290 3 ปีที่แล้ว

    THank you :)

  • @CGRunLough
    @CGRunLough 4 ปีที่แล้ว

    Nice

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

  • @cmg1131
    @cmg1131 2 ปีที่แล้ว

    takk fyrir !!

  •  2 ปีที่แล้ว

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

    • @veggiebroth5542
      @veggiebroth5542 ปีที่แล้ว

      Right, but that wouldn't be a loop

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

  • @julianjoseph5679
    @julianjoseph5679 6 หลายเดือนก่อน

    Hey man thx preparing for exam 😅

    • @KeithGalli
      @KeithGalli  6 หลายเดือนก่อน

      Hope the exam went well!

  • @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 4 ปีที่แล้ว

      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.

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

  • @TheSuccessfulLeader
    @TheSuccessfulLeader 4 ปีที่แล้ว

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

  • @edixonbalzan1142
    @edixonbalzan1142 ปีที่แล้ว

    Hi man where i can practice more exercises?

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

  • @reedshay6173
    @reedshay6173 2 ปีที่แล้ว

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

  • @mikelim91
    @mikelim91 3 ปีที่แล้ว

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

  • @abhinavkumar8396
    @abhinavkumar8396 5 ปีที่แล้ว

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

  • @skepticaltep
    @skepticaltep 4 ปีที่แล้ว

    5:41

  • @alexanderpadalka5708
    @alexanderpadalka5708 3 ปีที่แล้ว

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

  • @arcangel1384
    @arcangel1384 ปีที่แล้ว

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

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

  • @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! th-cam.com/video/lx7oqZ7Nl3k/w-d-xo.html

  • @jeffkamau3432
    @jeffkamau3432 5 ปีที่แล้ว +1

    how can one improve python skills after watching your videos

  • @cheng-juyeh525
    @cheng-juyeh525 ปีที่แล้ว

    god bless u xdd

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

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

  • @ianwilliam9531
    @ianwilliam9531 4 ปีที่แล้ว

    Red Sox

    • @KeithGalli
      @KeithGalli  4 ปีที่แล้ว

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

  • @georgepoletes4459
    @georgepoletes4459 2 ปีที่แล้ว

    weights = Red Sox lineup?

    • @Quaght
      @Quaght ปีที่แล้ว

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

  • @lisao2920
    @lisao2920 ปีที่แล้ว

    bruins!

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