For Loops and While Loops | Python for Beginners Lesson 6 | Code with Kylie

00:17:30
https://www.youtube.com/watch?v=zXaQ-ift74A

Sintesi

TLDRIn this lesson, we explore loops in programming, focusing on for loops and while loops. For loops allow iteration over sequences, such as strings and lists, executing a block of code for each item. An example demonstrates printing each character of a string and values from a list. While loops, on the other hand, execute a block of code as long as a specified condition is true, with an example of counting down from a number. The lesson emphasizes the importance of avoiding infinite loops by ensuring termination conditions are met. The video concludes with an invitation to join a Discord server for further interaction and support.

Punti di forza

  • 🔄 Loops allow repeated execution of code blocks.
  • 📜 For loops iterate over sequences like strings and lists.
  • 🔍 While loops run until a condition is false.
  • ⚠️ Infinite loops can occur without termination conditions.
  • ✅ Always ensure a loop can eventually end.
  • 📝 Practical applications of loops will be discussed next.
  • 💬 Join the Discord server for student interaction.
  • 📚 Check out linked notes for additional resources.
  • 👨‍💻 Examples help clarify loop usage in programming.

Linea temporale

  • 00:00:00 - 00:05:00

    The video introduces loops, specifically focusing on for loops and while loops. It explains that loops allow for the repeated execution of a block of statements, using the example of iterating through a string. The presenter demonstrates a for loop that iterates through the characters of the string 'cat', printing each character and a final 'done' statement after the loop completes. The importance of indentation in Python to define the scope of the loop is emphasized.

  • 00:05:00 - 00:10:00

    The presenter provides another example using a list of numbers (0, 1, 2) to demonstrate the versatility of for loops. The loop prints each value in the list and its double, followed by a 'done' statement. The output is confirmed by running the code, showing that the loop correctly iterates through the list and executes the intended print statements.

  • 00:10:00 - 00:17:30

    The video transitions to while loops, explaining that they continue executing as long as a specified condition is true. An example is given where a variable 'n' is decremented until it reaches zero, at which point a 'go' statement is printed. The presenter warns about the potential for infinite loops if the condition never becomes false, illustrating this with a 'while true' example. The lesson concludes with a reminder to ensure termination conditions are present in while loops to avoid infinite execution.

Mappa mentale

Video Domande e Risposte

  • What are the two types of loops discussed?

    The two types of loops discussed are for loops and while loops.

  • What does a for loop do?

    A for loop allows you to iterate over a sequence, executing a block of code for each item.

  • What is a while loop?

    A while loop continues to execute a block of code as long as a specified condition is true.

  • What is an infinite loop?

    An infinite loop occurs when a loop continues to execute without a termination condition, causing the program to run indefinitely.

  • How can you avoid infinite loops?

    To avoid infinite loops, ensure that there is a termination condition that will eventually be met.

  • Can you iterate through lists with for loops?

    Yes, you can iterate through lists, strings, tuples, and other sequences using for loops.

  • What happens when the condition in a while loop becomes false?

    When the condition in a while loop becomes false, the loop terminates and the program continues with the next statement after the loop.

  • What is the output of the example with n equal to 3 in a while loop?

    The output is '3 2 1 go'.

  • What should you check before using a while loop?

    You should check that there is a termination condition to prevent infinite loops.

  • What practical applications of loops will be discussed next?

    The next class will discuss examples of each loop type and practical applications.

Visualizza altre sintesi video

Ottenete l'accesso immediato ai riassunti gratuiti dei video di YouTube grazie all'intelligenza artificiale!
Sottotitoli
en
Scorrimento automatico:
  • 00:00:00
    today we're talking about loops loops
  • 00:00:03
    looped previously we saw that we could
  • 00:00:04
    iterate through sequences using Loops
  • 00:00:07
    well iteration allows us to actually run
  • 00:00:11
    a block of statements repeatedly kind of
  • 00:00:14
    just like Doctor Strange when he come to
  • 00:00:17
    bargain
  • 00:00:29
    now there are two kinds of Loops there's
  • 00:00:30
    the for Loop and there's the while loop
  • 00:00:33
    in this lesson we'll be discussing both
  • 00:00:35
    of them the first type of loop that
  • 00:00:37
    we're going to discuss is known as a for
  • 00:00:38
    Loop so a for Loop allows to iterate
  • 00:00:41
    over a sequence so this might be looping
  • 00:00:43
    through each character of a string for
  • 00:00:46
    example I might have some code that says
  • 00:00:48
    for Char in cat
  • 00:00:54
    print
  • 00:00:56
    char
  • 00:00:58
    and then at the very end I'm going to
  • 00:01:00
    print you know something that signifies
  • 00:01:02
    I'm done with this Loop so I might say
  • 00:01:05
    Okay print
  • 00:01:08
    done
  • 00:01:11
    all right so now what's this going to
  • 00:01:13
    look like in our virtual computer world
  • 00:01:15
    over here so this is our virtual
  • 00:01:17
    computer World okay so we have some
  • 00:01:20
    string here cat okay so I'm going to
  • 00:01:24
    represent that string as I did before
  • 00:01:26
    with this little sequence of boxes and
  • 00:01:29
    so that stands for letter C then a then
  • 00:01:33
    t
  • 00:01:34
    okay so now what this is doing so this
  • 00:01:38
    is what I'm iterating over what this
  • 00:01:41
    Loop is doing it's saying so I'm going
  • 00:01:43
    to come up with this variable char
  • 00:01:45
    and let me use green to denote that so
  • 00:01:48
    in my virtual computer world I now have
  • 00:01:50
    a variable named char
  • 00:01:52
    and I'm going to go through each of the
  • 00:01:55
    boxes in cat
  • 00:01:57
    what's the first box it's the C so at
  • 00:02:00
    first Char is equal to the C and let me
  • 00:02:02
    just erase this red to make it very
  • 00:02:04
    clear and once I've you know set Char to
  • 00:02:08
    be that first box in cat I go ahead and
  • 00:02:11
    execute whatever is underneath that
  • 00:02:14
    indented statement so that's anything
  • 00:02:17
    down here and right now we only have one
  • 00:02:20
    statement which is print Char so
  • 00:02:24
    what we're doing here is right now what
  • 00:02:27
    is Char well Char is C right so let me
  • 00:02:32
    draw out you know what my output might
  • 00:02:35
    be from here
  • 00:02:40
    and currently we want to print Char so
  • 00:02:44
    okay we come here Char is equal to C and
  • 00:02:48
    so we print C
  • 00:02:50
    now what happens
  • 00:02:52
    okay well
  • 00:02:54
    we've done everything under this Loop
  • 00:02:57
    right here we've done every we've done
  • 00:02:59
    this whole block of statements that's
  • 00:03:01
    indented
  • 00:03:02
    so what we actually are going to do is
  • 00:03:05
    go over to the next box
  • 00:03:08
    so I'm going to take this Char variable
  • 00:03:10
    and just slide it over by one box
  • 00:03:13
    and I'm going to do the same thing again
  • 00:03:15
    so now I'm going to come down here and
  • 00:03:16
    execute everything
  • 00:03:18
    that's indented in this for Loop
  • 00:03:21
    which is print that whatever Char is and
  • 00:03:25
    what is Char while we look over here at
  • 00:03:28
    our virtual computer world and Char is
  • 00:03:30
    pointing to a
  • 00:03:32
    so I come down here into my output and I
  • 00:03:35
    now write a because Char is a
  • 00:03:39
    okay and then we slide this over one
  • 00:03:42
    more time because we can and so now Char
  • 00:03:45
    is T and we're going to go ahead and
  • 00:03:48
    execute everything here so that we're
  • 00:03:50
    printing Char which is T so we print a t
  • 00:03:55
    and then we try and we want to slide
  • 00:03:57
    this chart over one more
  • 00:04:00
    and then we realize okay there's nothing
  • 00:04:01
    there at this point our computer says
  • 00:04:04
    okay we we can't go over one more
  • 00:04:07
    hmm well that means that now we're done
  • 00:04:11
    with this for Loop because we've
  • 00:04:13
    iterated through everything that we can
  • 00:04:14
    and there's nothing left to go through
  • 00:04:16
    anymore which means that we're left with
  • 00:04:19
    this final statement that's outside of
  • 00:04:21
    the loop so
  • 00:04:23
    one thing to note is that how do we know
  • 00:04:26
    what's in the loop what's outside the
  • 00:04:27
    loop we just look at this white space
  • 00:04:30
    here
  • 00:04:31
    so anything that's indented that's after
  • 00:04:33
    these dots that's inside of that for
  • 00:04:36
    Loop so whenever we go through one by
  • 00:04:38
    one we execute everything there if it's
  • 00:04:41
    outside if it's like the same uh
  • 00:04:44
    indentation as that original four then
  • 00:04:47
    that's outside the for Loop and we
  • 00:04:48
    execute that after we're done with the
  • 00:04:51
    loop so here because this print done is
  • 00:04:55
    the same indentation as four
  • 00:04:57
    then after we're done with this loop at
  • 00:04:59
    the very end we print done
  • 00:05:02
    okay so our output from this code should
  • 00:05:05
    be c a t done now let's go over to the
  • 00:05:08
    computer and try that out okay so here
  • 00:05:11
    I've typed for Char in cat print Char
  • 00:05:14
    and then at the very end we're going to
  • 00:05:16
    print done so let's run this
  • 00:05:19
    and we get cat done just like how I just
  • 00:05:24
    explained it so let's go through a
  • 00:05:26
    different example we've looked at
  • 00:05:28
    strings but actually we can iterate
  • 00:05:30
    through any sequence using a for Loop so
  • 00:05:32
    we can iterate through lists tuples and
  • 00:05:35
    even dictionary keys or you know we
  • 00:05:37
    could also do the values and items but
  • 00:05:39
    that requires an extra step we can also
  • 00:05:41
    do iteration through sets so let's look
  • 00:05:45
    at a list
  • 00:05:46
    l is equal to some list 0 1 2.
  • 00:05:52
    okay and then I'm going to say for vowel
  • 00:05:57
    in l
  • 00:05:59
    let me put a little tail on that L to
  • 00:06:02
    make it very clear that it's an L
  • 00:06:05
    um let's print that value
  • 00:06:09
    and then let's also print
  • 00:06:11
    that value times 2.
  • 00:06:16
    okay and then at the very end we're
  • 00:06:18
    going to print done again
  • 00:06:22
    okay so I'm going to put my virtual
  • 00:06:24
    computer world
  • 00:06:26
    up here
  • 00:06:27
    and then I'm going to write my output
  • 00:06:29
    down here
  • 00:06:33
    okay sorry these are not great boxes but
  • 00:06:37
    that's okay
  • 00:06:39
    so first I have some list L and I'm
  • 00:06:42
    going to set that to this like
  • 00:06:46
    representation of the list that I came
  • 00:06:47
    up with in the last class
  • 00:06:50
    um so here I have value 0 1 and 2 in my
  • 00:06:54
    list right
  • 00:06:56
    so I'm coming up with a variable vowel
  • 00:06:59
    and I'm pointing it towards the first
  • 00:07:01
    item when I go into this
  • 00:07:04
    for Loop so Val okay that's equal to
  • 00:07:08
    zero right now and I'm going to dive
  • 00:07:11
    into the indented part of that for Loop
  • 00:07:13
    so I'm going to print Val which is zero
  • 00:07:16
    okay then I'm going to print Val times
  • 00:07:18
    two okay well what is Val times two
  • 00:07:20
    vowel zero so Val times two zero times
  • 00:07:24
    two is zero so I'm going to Output 0
  • 00:07:26
    again
  • 00:07:28
    now I'm done with this block
  • 00:07:30
    so
  • 00:07:31
    I go and I scoot Val over to the next
  • 00:07:35
    item in my sequence
  • 00:07:38
    and now vowel is equal to one we go into
  • 00:07:42
    this indented part of the code again and
  • 00:07:44
    we're going to print Val which is one
  • 00:07:46
    and then we're going to print vowel
  • 00:07:48
    times two so one times two
  • 00:07:50
    that's two
  • 00:07:53
    okay so then we're going to scoot this
  • 00:07:56
    over one more time
  • 00:07:58
    till we get to 2.
  • 00:08:00
    okay and now we're going to print Val
  • 00:08:01
    again so that's two
  • 00:08:03
    and then we're going to print Val times
  • 00:08:06
    two so two times two that's four
  • 00:08:10
    and
  • 00:08:11
    now we're going to try to scoot this
  • 00:08:13
    over one more time but there's nothing
  • 00:08:14
    over here so
  • 00:08:16
    we're you know basically this is telling
  • 00:08:19
    us all right we are done with these
  • 00:08:22
    lines we're done with this for Loop and
  • 00:08:25
    then we can move on to the very last
  • 00:08:26
    statement which is print done
  • 00:08:30
    so then we print done at the very end
  • 00:08:33
    so let's go and try that out so let's
  • 00:08:35
    set L equal to 0 1 and 2. and then four
  • 00:08:40
    each vowel in L I'm going to print
  • 00:08:45
    Bell and I'm going to print Val times
  • 00:08:49
    two now at the very end I'm going to
  • 00:08:52
    print done
  • 00:08:54
    okay so if I run this
  • 00:08:57
    then I get zero zero one two two four
  • 00:09:01
    done which is exactly what we just wrote
  • 00:09:05
    down
  • 00:09:06
    so now let's go over while loops
  • 00:09:09
    so sometimes what we want to do instead
  • 00:09:12
    of iterating through a sequence is we
  • 00:09:14
    want to iterate through a block of code
  • 00:09:15
    until a certain conditional statement is
  • 00:09:19
    met so until something becomes false and
  • 00:09:23
    so in the doctor strange case it's until
  • 00:09:26
    dermamu could not tolerate with Doctor
  • 00:09:29
    Strange anymore and decides to concede
  • 00:09:32
    then they finally break that Loop never
  • 00:09:37
    come back
  • 00:09:38
    do it
  • 00:09:39
    and I'll break the loop
  • 00:09:42
    [Music]
  • 00:09:44
    so this is where something known as a
  • 00:09:46
    while loop come
  • 00:09:48
    so in a while loop we continue looping
  • 00:09:51
    while some condition is true and then
  • 00:09:53
    once that condition becomes false then
  • 00:09:56
    we break from that Loop all right so
  • 00:09:58
    let's do an example with some real code
  • 00:10:00
    so let's say that n is equal to three
  • 00:10:03
    okay so in my Loop I'm going to say
  • 00:10:05
    while n is greater than zero I'm going
  • 00:10:09
    to print
  • 00:10:11
    n and then I'm going to do a subtraction
  • 00:10:14
    so let's do n equals n minus 1. okay and
  • 00:10:19
    then at the very end
  • 00:10:21
    I'm going to print
  • 00:10:23
    go
  • 00:10:25
    all right so what happens here let's
  • 00:10:27
    draw out our
  • 00:10:29
    virtual computer world and then let's
  • 00:10:31
    draw out our output
  • 00:10:34
    basically okay let's start here n is
  • 00:10:38
    equal to 3. okay so we have some
  • 00:10:40
    variable n and we set that equal to 3.
  • 00:10:44
    and now our condition is while n is
  • 00:10:48
    greater than zero so right now what this
  • 00:10:50
    is doing is it's checking okay what is n
  • 00:10:53
    n is 3 is 3 greater than zero okay well
  • 00:10:58
    this is true
  • 00:10:59
    so we're going to go through our
  • 00:11:02
    statements here so we're going to print
  • 00:11:03
    n which is three
  • 00:11:05
    and then what we're going to do is
  • 00:11:07
    subtract 1 from n and set that back to n
  • 00:11:11
    so
  • 00:11:12
    over here n equals three okay so what's
  • 00:11:15
    three minus one it's two and we're going
  • 00:11:17
    to set that back to n so instead of
  • 00:11:20
    three now n is 2.
  • 00:11:22
    so
  • 00:11:23
    okay we come back up here and we check
  • 00:11:26
    our condition again so now n is equal to
  • 00:11:30
    2. so we're checking 2 greater than zero
  • 00:11:32
    well that's true again so we go into
  • 00:11:35
    here and we print n which is 2 and then
  • 00:11:39
    again we do okay n equals n minus one so
  • 00:11:43
    n minus 1 is 1 because 2 minus 1 is 1.
  • 00:11:47
    and we're going to set that as n so let
  • 00:11:50
    me just clean that up
  • 00:11:52
    so now n is one right
  • 00:11:54
    okay now we're done with you know that
  • 00:11:57
    block of statements again we go back to
  • 00:11:59
    the while and we say okay is one greater
  • 00:12:02
    than zero
  • 00:12:04
    okay well that's true again so we go
  • 00:12:06
    into our statements again so we're
  • 00:12:08
    printing one and then again well n
  • 00:12:12
    equals n minus one so we're setting n to
  • 00:12:15
    one minus one which is zero
  • 00:12:19
    so when we come back up to this while
  • 00:12:21
    loop the statement we're checking okay
  • 00:12:24
    is zero greater than zero
  • 00:12:28
    that's not true this time so
  • 00:12:31
    we don't do any of this stuff down here
  • 00:12:33
    and in fact we are done with this Loop
  • 00:12:35
    once that becomes false we're done with
  • 00:12:37
    the loop so the last thing that we have
  • 00:12:40
    left is to print go
  • 00:12:42
    so down here I print go so our output
  • 00:12:45
    becomes three two one go
  • 00:12:49
    now let's see that in action
  • 00:12:51
    okay so if I have n equals three and I
  • 00:12:56
    say while n is greater than zero
  • 00:13:00
    I'm going to print n
  • 00:13:03
    and then I'm going to set n equal to n
  • 00:13:06
    minus 1. so I'm basically just
  • 00:13:08
    subtracting 1 each time and then at the
  • 00:13:10
    very end I'm going to print go
  • 00:13:14
    okay if I run this
  • 00:13:16
    then I get three two one go
  • 00:13:20
    so you can actually kind of read this
  • 00:13:22
    while statement as if it were English
  • 00:13:25
    so in this specific context you can say
  • 00:13:27
    okay while n is greater than zero
  • 00:13:32
    then display this value of N and then
  • 00:13:35
    subtract 1 from n and when you get to
  • 00:13:38
    zero then we're going to print go
  • 00:13:42
    so we do have to be careful when we use
  • 00:13:44
    while loops
  • 00:13:45
    take a second here and think about why
  • 00:13:48
    do we have to be careful
  • 00:13:49
    again
  • 00:13:51
    and again
  • 00:13:54
    and again forever
  • 00:13:57
    well what would happen if I did
  • 00:13:59
    something like this
  • 00:14:01
    this while true
  • 00:14:04
    print
  • 00:14:06
    hello world
  • 00:14:12
    hmm
  • 00:14:14
    what would happen here
  • 00:14:16
    well
  • 00:14:19
    true is always going to be true
  • 00:14:23
    basically forever well not basically it
  • 00:14:26
    is going to be true forever right
  • 00:14:29
    so if that's true forever then when do
  • 00:14:32
    we ever stop printing hello world
  • 00:14:36
    we don't right and again forever in our
  • 00:14:40
    computer we'll go crazy like dormammu so
  • 00:14:43
    in this case it's actually pretty easy
  • 00:14:45
    to tell that this will become what's
  • 00:14:48
    known as an infinite Loop
  • 00:14:53
    so sometimes it's actually fairly
  • 00:14:55
    straightforward to tell if something's
  • 00:14:56
    going to be an infinite Loop but other
  • 00:14:59
    times it's not so for example if I have
  • 00:15:02
    n equals 27 and I say while you know n
  • 00:15:06
    does not equal one
  • 00:15:08
    I'm going to print n and within you know
  • 00:15:13
    the block of code underneath a loop you
  • 00:15:17
    can have other valid codes so let me say
  • 00:15:18
    like
  • 00:15:19
    let me put a conditional in here so if n
  • 00:15:23
    uh is even so this if the remainder
  • 00:15:27
    divided by 2 is 0
  • 00:15:29
    then I'm going to just divide n in half
  • 00:15:32
    and set the new n equal to that value
  • 00:15:35
    and otherwise so basically n is odd here
  • 00:15:40
    and here n is even
  • 00:15:45
    so otherwise we're going to do n equals
  • 00:15:47
    n times 3 plus 1.
  • 00:15:51
    okay
  • 00:15:52
    so is it obvious to you that in this
  • 00:15:54
    specific case we
  • 00:15:58
    you know this this never ends
  • 00:16:01
    not so obvious right
  • 00:16:04
    because n is sometimes increasing and
  • 00:16:06
    sometimes decreasing and
  • 00:16:09
    there are actually certain values of n
  • 00:16:11
    where the program will terminate so it
  • 00:16:15
    will sometimes become one but we can't
  • 00:16:18
    prove that this actually ends for all
  • 00:16:21
    positive values of n and there's
  • 00:16:23
    actually some famous example that I took
  • 00:16:25
    from one of mit's notes but basically
  • 00:16:28
    the warning here is that infinite Loops
  • 00:16:30
    are not always as simple as you know
  • 00:16:34
    while true so you have to be careful
  • 00:16:37
    when you use while Loops you need to
  • 00:16:38
    make sure that there's a termination
  • 00:16:40
    condition you need to make sure that
  • 00:16:43
    there will be an end if you start this
  • 00:16:44
    Loop otherwise you will drive your
  • 00:16:46
    computer crazy
  • 00:16:47
    in this lesson we learned about for
  • 00:16:49
    loops and while loops and how to use
  • 00:16:52
    each one now in the next class what
  • 00:16:54
    we're going to be talking about is
  • 00:16:56
    examples of each and why you might want
  • 00:17:00
    to use a loop or what is a practical
  • 00:17:02
    application of a loop so stay tuned
  • 00:17:06
    by the way make sure you guys check out
  • 00:17:07
    the Discord server where you can
  • 00:17:09
    interact with other students and ask
  • 00:17:11
    questions and also check out the notes
  • 00:17:13
    that are linked below
  • 00:17:15
    good luck
Tag
  • loops
  • for loop
  • while loop
  • iteration
  • infinite loop
  • programming
  • sequences
  • termination condition
  • coding examples
  • Discord server