Locked lesson.
About this lesson
For loops are used to loop through things like lists or groups of data.
Exercise files
Download this lesson’s related exercise files.
10 - Python For Loops (1).docx57.2 KB 10 - Python For Loops SOLUTION.docx
55.6 KB
Quick reference
Python For Loops
For loops are used to loop through things like lists or groups of data.
When to use
Use them whenever you have any sort of list of items that you need loop through and grab individual items from.
Instructions
Given a List:
names = ["John", "Tina", "Joe"]
Create a list:
for name in names:
print(name)
Be sure to indent all lines in a for loop (except the first declaration line) using the tab key on your keyboard.
Hints & tips
- for name in names:
- indent all lines of a for loop except the first one
- use an f-string to add text: f'Hello there {name}...'
- 00:04 Okay, in this video I want to talk about for loops.
- 00:07 And a for loop is a type of Python loop.
- 00:09 And a loop is exactly what it sounds like, it loops through things.
- 00:13 So we've been talking about lists and tuples.
- 00:15 Those are our lists.
- 00:16 If we want to loop through the list and, for instance,
- 00:19 pull out each item in the list, we could use a for loop for that.
- 00:22 So a lot of times, we're going to be getting data from Excel.
- 00:25 We're going to get them in list form.
- 00:26 And we need to sort of loop through the columns and rows in Excel and
- 00:30 grab different information, different data out of those things.
- 00:34 And a lot of times, we'll use different types of loops.
- 00:37 The most popular is probably the for loop.
- 00:38 So let's go ahead and start out by creating a list called names.
- 00:44 Give us some practice making a list.
- 00:46 So let's go John, Tim, and Mary.
- 00:50 Now, what if we wanted to loop through this thing, and print out each item?
- 00:55 We've already learned that we can print out a single item by calling names, and
- 01:00 then the item number, right?
- 01:01 But how do we print out all the items one at a time,
- 01:04 or do something else with them if we wanted to?
- 01:07 Ultimately, we're going to get data from our Excel spreadsheet because we
- 01:10 want to do something with it.
- 01:12 So how do we do something with that data, with each item of that data?
- 01:15 So that's one of the things we use for loops for.
- 01:17 So to create a for loop, we start out by typing for.
- 01:21 So let's go, for name in names.
- 01:25 Now, you'll notice that this name, where are we getting that?
- 01:28 Well, I just invented it.
- 01:29 We could call this anything we want.
- 01:31 We could call it, for x in names.
- 01:33 We could call it, for pizza in names, anything you want.
- 01:37 But we're going to, these are all, what are these things?
- 01:40 These are names.
- 01:42 So what's the singular of names?
- 01:45 Name, so we're going to get each name from this list of names.
- 01:49 So I just called it name right there.
- 01:51 Now, we put a colon here, and then hit Enter.
- 01:55 Now, notice that the cursor is now right here.
- 01:58 When we hit Enter our Sublime Text editor automatically indents for us.
- 02:04 And indentation in Python is very important, Python is indentation specific.
- 02:09 So to do this, we're not hitting the space bar a bunch of times, don't ever do that.
- 02:13 What you want to do is hit the Tab key on your keyboard, and
- 02:16 you'll notice it just tabs over.
- 02:19 Or if you just come up here and hit Enter, like I said,
- 02:21 Sublime Text will tab it for you.
- 02:23 Now inside of here, we can do anything we want with these names.
- 02:27 So for instance, let's just print out each name.
- 02:30 So I'm referencing this name right here.
- 02:32 So what Python does is it will start at the beginning of this thing, and
- 02:35 it will loop through.
- 02:36 And every time it does, it grabs an item.
- 02:40 And it assigns it to this variable name.
- 02:43 And it loops through again, grabs the next item, assigns that to this variable name.
- 02:49 Then it loops through again, grabs the next item,
- 02:52 assigns it to that variable name.
- 02:54 And then here, and anywhere else below here as long as we're indented,
- 02:58 we can do anything we want.
- 03:00 So all we really want to do right now is just print each of these names.
- 03:03 So let's go ahead, get rid of that line here.
- 03:06 Let's save this and run it, and see what this looks like.
- 03:10 So let's go print.
- 03:11 And you can see, boom, it just prints out John, Tim, Mary, one at a time.
- 03:15 And you'll notice how that's different than if we were just to print out our
- 03:19 whole list as we've done in the past.
- 03:22 If we did that, and came down here and ran this again, you notice in the past,
- 03:26 we got this whole big list, that the items are in quotation marks.
- 03:30 There's this bracket, this is not very useful.
- 03:33 This, on the other hand, is useful.
- 03:36 We're printing out each individual thing, and
- 03:38 we can do anything we want with each of these individual things.
- 03:41 For instance, if we wanted to add a little something else,
- 03:44 we could create something called an f string.
- 03:46 And we really don't care too much what these are.
- 03:48 But inside of here, we can write some text.
- 03:51 So we can go, Hello there.
- 03:54 And then let's put curly brackets, and then we could type in name.
- 03:58 And then we can get rid of this thing.
- 03:59 So this will print out, Hello there name.
- 04:03 And then we can put other things too.
- 04:06 So if we were to save this, and let's get rid of this line.
- 04:09 And save this, head back over to our terminal.
- 04:12 Let's clear the screen and run this guy.
- 04:14 It says, Hello there John.
- 04:15 Hello there Tim, Hello there Mary, and we could do anything we want.
- 04:18 So obviously, this is a very simplistic sort of thing to do.
- 04:23 But in the future, we'll get more complicated with what we want to do with
- 04:27 the things that we're pulling out of our lists.
- 04:29 But this gives us a way to do it.
- 04:31 Now, we can loop through just about anything, right?
- 04:34 Here we're listing through a list.
- 04:36 We can also list through a tuple, the same way.
- 04:38 We could also just create a variable.
- 04:40 Let's call first_name, and set that equal to John.
- 04:44 So here we can go, for letter in first_name.
- 04:52 And then let's just print out letter.
- 04:55 So this will loop through here.
- 04:57 And each of these items, which will be text characters,
- 05:01 it will print out the letter.
- 05:03 So if we save this and run this, you can see one at a time, J-o-h-n.
- 05:09 So loops are very powerful, they're very easy to use.
- 05:13 And this format is sort of important with Python,
- 05:16 where you see you do a thing here, you put a colon.
- 05:19 And then the next line is indented, and
- 05:22 all the lines after this will continue to be indented.
- 05:26 And all of these things will relate to whatever you've done here.
- 05:31 And then as soon as you come outside of here, and
- 05:34 go back to normal indentation like that, then you're breaking out of this for
- 05:39 loop, and no longer doing loopy things.
- 05:42 So very cool and very easy to use.
- 05:44 So that's all for this video.
- 05:46 In the next video, we'll look at random numbers.
Lesson notes are only available for subscribers.