Locked lesson.
About this lesson
Learn to loop using a For loop.
Exercise files
Download this lesson’s related exercise files.
For Loops.docx59.1 KB For Loops - Solution.docx
59.1 KB
Quick reference
For Loops
The For loop lets us easily loop through things.
When to use
Whenever you have a list, tuple, dictionary, or range of any sort, use the for loop to loop through and grab each item.
Instructions
To create a for loop you need something to loop through like a string, list, tuple, dictionary, etc.
names = ["John", "Tim", "Mary"]
for x in names:
print(x)
To loop through a dictionary:
pizza = {
"John": "Pepperoni",
"Tim": "Mushroom",
"Mary": "Cheese"
}
for key, value in pizza.items():
print(key)
Hints & tips
- For loops let us loop through items in a list, tuple, etc.
- 00:04 In the last video, we looked at the while loop.
- 00:06 In this video, I want to look at the for loop.
- 00:08 And the for loop is quite a bit different than the while loop.
- 00:11 While lets us loop over a counter.
- 00:13 For, lets us loop through things, it lets us loop through variables.
- 00:18 It lets us loop through list, tuples, even hashes.
- 00:21 So if you have a range of things and
- 00:23 you want to loop through them, use your for loop.
- 00:25 To do that, it's pretty simple.
- 00:26 Let's start out by looping through a simple variable.
- 00:30 Let's call it name and let's set it equal to John.
- 00:34 Now, let's loop through there and
- 00:35 print out every letter in this variable on its own line.
- 00:40 So we would go, for x.
- 00:42 And here, we just need a variable, we just need some placeholder variable.
- 00:46 Anything will do, you could do y.
- 00:48 You could do num, you could do letter, whatever you want.
- 00:51 So I'm just going to do x, for x in name because we're going to,
- 00:56 want to loop through our variable name.
- 00:59 And here, we're just going to print out x.
- 01:01 If you call this letter here, you would type in letter, right?
- 01:06 I'll just do an x, because it's simple.
- 01:08 So if we save this and run it, we see J-o-h-n.
- 01:12 So what it does is it takes the first thing, J, prints it, loops around,
- 01:16 gets the next one, O, prints it, loops around, and so you're at the end.
- 01:21 You can do this with variables, you can do this with lists and tuples.
- 01:25 So let's create a quick list and let's go, John,
- 01:28 our old standby, John, Tim and Mary list.
- 01:34 Same thing, nothing here changes and every line,
- 01:37 it'll just print out each item in our list.
- 01:40 Super cool.
- 01:40 So we can do the same thing with a tuple.
- 01:43 And we don't have to change anything at all.
- 01:46 Again, we get John, Tim and Mary.
- 01:47 So that's super cool.
- 01:48 Now, we can also turn this into a hash, the dictionary,
- 01:51 I call them hashes because everybody else calls them hashes.
- 01:54 But in Python, we call them dictionary.
- 01:56 So let's create a quick dictionary.
- 01:58 And let's go John.
- 02:01 And John likes mushroom pizza.
- 02:04 Remember, no, John likes pepperoni.
- 02:06 Gotta keep these straight pepperoni, and we have Tim.
- 02:11 Tim likes mushroom, And we have Mary and Mary likes cheese.
- 02:19 To loop through here,
- 02:20 we needed to make a little bit of a change because we have a key and a value pair.
- 02:23 So we need to designate each one.
- 02:25 Instead of x, I'm going to call the our variable key just so
- 02:27 we can keep track of it.
- 02:28 So key, and then value, again this is just a variable name.
- 02:33 We could've named this x and this y.
- 02:36 I'm just putting key and value so we keep it straight in our own head.
- 02:39 In name, and then we gotta have items because we're looking at the items in
- 02:43 our dictionary and then call the function, because it's an items function.
- 02:47 And then let's print out the key.
- 02:50 If we save this, and run it, we get Tim, John and Mary.
- 02:52 Tim, John and Mary, if we want to print out the value instead,
- 02:57 just go value, mushroom, pepperoni and cheese.
- 03:01 And so like I said, this could be x or y if we want to print out the y,
- 03:05 the name of the variable is arbitrary.
- 03:08 It's just whatever you want.
- 03:09 These are just like placeholder variables.
- 03:12 We get the same mushroom, pepperoni, and cheese, we change it to x, the same Tim,
- 03:16 John, and Mary.
- 03:17 So that's the for loop.
- 03:18 Just remember anytime you want to iterate through items in a list or
- 03:23 variables or whatever, the for loop is what you want.
- 03:26 You could probably do this with a while loop as well.
- 03:29 You have to think it true and sort of suss out how you would do that.
- 03:32 But this just so easy, this for loop.
- 03:35 Those are the two main loops, for and while.
- 03:37 You're going to use those for just about everything.
- 03:39 In the next video, we're going to look at loop control statements.
Lesson notes are only available for subscribers.