CS50 Video Player
    • 🧁

    • 🍩

    • 🍌

    • 🍿
    • 0:00:00[MUSIC PLAYING]
    • 0:00:05SPEAKER: Well, hello, one and all, and welcome to our short on for loops.
    • 0:00:09If you're familiar with the Mario video game series,
    • 0:00:12you might be familiar with Princess Peach and her castle, Peach's Castle.
    • 0:00:16And we'll imagine together that Princess Peach is inviting some guests to join
    • 0:00:20her at a ball at Peach's Castle at 7:00 PM this evening.
    • 0:00:24Now, thankfully, Peach has automated the process
    • 0:00:26of writing letters to her guests.
    • 0:00:28In fact, we have here a function called write_letter,
    • 0:00:31which takes in two arguments, one called receiver and one called sender.
    • 0:00:36And notice how the purpose write_letter is
    • 0:00:39to return to us a fully written and formatted letter
    • 0:00:42to a receiver from the sender.
    • 0:00:45In particular, we're using Python f-strings here
    • 0:00:48to interpolate the values of receiver and sender.
    • 0:00:52For instance, if our receiver were to be Mario, as it is on line 2 here,
    • 0:00:57and our sender were to be Princess Peach, well, we should see,
    • 0:01:01at the end of this, a letter that says "Dear Mario,"
    • 0:01:05and then some text down below, "Sincerely, Princess Peach."
    • 0:01:09And then we'll take that letter and print it out to our terminal
    • 0:01:12to make sure everything is all correct.
    • 0:01:15So let's see what happens if we run this program.
    • 0:01:17I'll go ahead and open up my terminal here.
    • 0:01:18And I'll run python of letters.py.
    • 0:01:22I'll give us a bit more space so we can see all the letters fly by here.
    • 0:01:25If I hit Enter, we'll see some letters being printed out to each of our guests
    • 0:01:30here.
    • 0:01:30We see we invited Mario first.
    • 0:01:35That seems good.
    • 0:01:36Invited Luigi next, Daisy as well, and finally Yoshi.
    • 0:01:41So this program seems to work.
    • 0:01:43But there's a question of how well it's designed.
    • 0:01:46I were to show you again what this program looks like,
    • 0:01:50let's think of some scenarios where this might get a little tedious.
    • 0:01:54Let's say we're only inviting four guests now,
    • 0:01:57but we also want to invite other folks, too, in which case, our guest list
    • 0:02:01gets pretty long.
    • 0:02:03And to do that, I need to copy and paste, copy
    • 0:02:06and paste to add new letters to new characters as well.
    • 0:02:10Now, if you find yourself in this kind of situation
    • 0:02:13where you're repeating something for each maybe guest you have,
    • 0:02:16you might think to use something like a for loop.
    • 0:02:19A for loop, in fact, is great when you know
    • 0:02:21how many times you want to iterate or you want to do something, like write
    • 0:02:25a letter, for each person or each thing you have in some list, like a guest
    • 0:02:30list, in this case.
    • 0:02:31So let's actually make ourselves a guest list and write a for loop
    • 0:02:36to write a letter for each person on that guest list.
    • 0:02:41Well, in main here, why don't I go ahead and update this program
    • 0:02:45to instead keep track of a list that we're going to call names.
    • 0:02:51And this names list will be our guest list for the ball.
    • 0:02:55I'll go ahead and maybe add a few names to the guest list.
    • 0:02:58I'll go ahead and add Mario here and Luigi as well, Daisy too--
    • 0:03:04Daisy-- and and Yoshi, just like this.
    • 0:03:07And now I have a guest list of names I'm hoping to invite.
    • 0:03:12Well, now what should we do?
    • 0:03:14We know we want to write a letter for each person that
    • 0:03:17is going to be in this list.
    • 0:03:20So I think I can actually get rid of this code down below here
    • 0:03:23and instead think about how I could use a for loop.
    • 0:03:26Well, one way to use a for loop, as we saw in lecture,
    • 0:03:30is to use numbers and iterate over every number in some range of numbers.
    • 0:03:35So I could write some code like this-- for i,
    • 0:03:38which is usually the name we give to some variable that is iterating,
    • 0:03:41changing on each iteration-- for i in--
    • 0:03:44and then the Python range function.
    • 0:03:47And range will give us a list of numbers from 0 up to but not including the value
    • 0:03:54we pass in as input to range.
    • 0:03:57So in effect, maybe I want i here to iterate, let's say, from 0, maybe
    • 0:04:04the zeroth index of this list, to 1, the first index of this list,
    • 0:04:09to 2, the second index of this list, and then to 3, the third index of this list,
    • 0:04:16again, indexing from 0 with Mario here.
    • 0:04:19So to do that, I can actually pass in to range here the length of our list.
    • 0:04:25Here, I'm saying, len of names, which in this case is 4.
    • 0:04:29But range will give us 0, 1, 2, and 3.
    • 0:04:33Let's try this out though.
    • 0:04:34I'll go ahead and print i down below.
    • 0:04:37And ideally, I'll see 0, 1, 2, and 3, each of the indices in this list here.
    • 0:04:44I'll go ahead and run python of letters.py.
    • 0:04:48And we'll in fact see 0, 1, 2, and 3.
    • 0:04:52That seems to work just as well.
    • 0:04:54But of course, we don't want to invite these numbers to our party.
    • 0:04:57We want to invite Mario, Luigi, Daisy, and Yoshi.
    • 0:05:00So one way to approach this is to use i to index into our list.
    • 0:05:06I could do something like this.
    • 0:05:07I could say names bracket i.
    • 0:05:10And on each iteration, again, i will change.
    • 0:05:13First, it will be 0, then 1, then 2, then 3,
    • 0:05:17giving us different names in our list here.
    • 0:05:20I wanted to go ahead and run letters.py again.
    • 0:05:23And we'll see, now, the names we're hoping to invite.
    • 0:05:26And this is promising because I think I can actually use these as input
    • 0:05:30to the write_letter function.
    • 0:05:33Let's try this.
    • 0:05:34If I were to go ahead and print not just names bracket i but the result
    • 0:05:40write_letter given names bracket i as the receiver and "Princess Peach"
    • 0:05:46as the sender, I think we'd start to get the same letters we saw last time.
    • 0:05:51Notice a few things first here, though.
    • 0:05:53Again, i will update on each iteration of our loop--
    • 0:05:57that is, from one traversal or time, going from top to bottom
    • 0:06:02in this indented code here.
    • 0:06:05What will actually stay the same is "Princess Peach" here,
    • 0:06:08the name that will be the sender.
    • 0:06:10That will not change on each iteration, because it doesn't
    • 0:06:13depend on anything in our loop itself.
    • 0:06:16So let's go ahead and run this.
    • 0:06:18I'll run Python of letters.py.
    • 0:06:20And we'll see the same letters down below here as well.
    • 0:06:24So same results, but what have we given ourselves here?
    • 0:06:28I'm not able to make this program much more flexible, much more adaptable.
    • 0:06:31How would you invite additional guests?
    • 0:06:34We could very quickly, let's say, add a guest, like Bowser.
    • 0:06:38Everyone is invited here.
    • 0:06:41And we could print out a letter for Bowser, just
    • 0:06:44like that, no copying and pasting, just adding Bowser's name to our guest list.
    • 0:06:49Now let's uninvite Bowser, actually, and think through some ways
    • 0:06:53to improve the design of even this little segment of code here.
    • 0:06:57Now, this works.
    • 0:06:59It's actually pretty well designed.
    • 0:07:01But I think we could make it a little more readable, maybe stylistically
    • 0:07:04better too.
    • 0:07:04And we could do that using a feature of Python, which is we
    • 0:07:07can actually iterate over any kind of list directly.
    • 0:07:11This is a list here.
    • 0:07:13Names is a list of, in this case, names.
    • 0:07:16So we can actually use it as the second part of our for loop.
    • 0:07:20We could say, for i in names.
    • 0:07:23And in this case, i will actually temporarily
    • 0:07:26refer to each of the items in our list, or the names in our names list.
    • 0:07:32i will first be Mario, then Luigi, then Daisy, then Yoshi,
    • 0:07:36updating on each iteration.
    • 0:07:39But I don't need to use i.
    • 0:07:40In fact, I can give it any name I like as
    • 0:07:42long as I am consistent with that name.
    • 0:07:45I could say something more English-like, like, for name in names,
    • 0:07:49because we have a list of names, plural.
    • 0:07:51But each item is a single name, singular.
    • 0:07:55So now on the inside of my for loop, well,
    • 0:07:58there's no longer an i variable to use to index into names.
    • 0:08:02But I've conveniently given myself now this variable called
    • 0:08:05name that I can just use.
    • 0:08:08Again, first name will be Mario, then Luigi, then Daisy, then Yoshi,
    • 0:08:13updating on each iteration as we loop through our for loop.
    • 0:08:17And once we get to the end, once there are no more names,
    • 0:08:20we'll simply stop looping and exit our program.
    • 0:08:24Let's try it here.
    • 0:08:25I'll go ahead and run python of letters.py.
    • 0:08:28And we'll see the same results but now with a bit better English syntax
    • 0:08:33and probably better stylistically as writing programs like these.
    • 0:08:37So for loops, again, are powerful when you know how many times you want to loop
    • 0:08:42or when you want to do something for each item you have
    • 0:08:45in some list or, more generally, some iterable,
    • 0:08:47something can be iterated over.
    • 0:08:49This was our short on for loops.
    • 0:08:51We'll see you next time.
  • CS50.ai
Shortcuts
Before using a shortcut, click at least once on the video itself (to give it "focus") after closing this window.
Play/Pause spacebar or k
Rewind 10 seconds left arrow or j
Fast forward 10 seconds right arrow or l
Previous frame (while paused) ,
Next frame (while paused) .
Decrease playback rate <
Increase playback rate >
Toggle captions on/off c
Toggle mute m
Toggle full screen f or double-click video