Computer science
-
Introduction to Programs Data Types and Variables
-
Binary Numbers
-
Python Lists
-
For Loops in Python
-
While Loops in Python
-
Fun with Strings
-
Writing a Simple Factorial Program. (Python 2)
-
Stepping Through the Factorial Program
-
Flowchart for the Factorial Program
-
Python 3 Not Backwards Compatible with Python 2
-
Defining a Factorial Function
-
Diagramming What Happens with a Function Call
-
Recursive Factorial Function
-
Comparing Iterative and Recursive Factorial Functions
-
Exercise - Write a Fibonacci Function
-
Iterative Fibonacci Function Example
-
Stepping Through Iterative Fibonacci Function
-
Recursive Fibonacci Example
-
Stepping Through Recursive Fibonacci Function
-
Exercise - Write a Sorting Function
-
Insertion Sort Algorithm
-
Insertion Sort in Python
-
Stepping Through Insertion Sort Function
-
Simpler Insertion Sort Function
For Loops in Python Basics of for loops in Python
⇐ Use this menu to view and help create subtitles for this video in many different languages.
You'll probably want to hide YouTube's captions if using these subtitles.
- Now that we know a little bit about lists,
- I thought I would use this video
- to introduce you to loops, and, in particular, for loops
- (and if we have time we might do other types of loops).
- But before we even do that,
- I want to introduce you to a built-in Python function
- called "range()" -- and when I say "built in,"
- it means it comes with Python.
- And it's really a way to generate lists
- -- lists that have integers that increment.
- So, for example, if I just say, range of six [range(6)]
- it generates a list from 0 through 5.
- So it's six elements, up to, but not including six.
- If I wanted to include six
- I would have to do range of seven [range(7)].
- And then I go all the way up to but not including 7
- but I get six in there.
- If I want to have 1 through 6 --
- I don't want to have that 0 there --
- I could do range starting at 1
- and including 1 up to but not including 7.
- So the first item here will be included,
- the last item here will not be included.
- And then we get -- there we go, we get 1 2 3 4 5 6.
- And what's cool about
- these Integrated Development Environments,
- these I.D.E.s, this PyScripter that I'm running
- is that when I type in a function that it recognizes,
- it actually will tell me a little bit,
- something about that function, in case you forget it.
- So I don't know if you can read this here, but it says,
- "range([start,] stop, [step])"
- so you can also, if you want, tell it, how much to int--
- So right now, the default is to increment by 1 each time.
- It says,
- "Hey, I can tell you to increment by a -different- number!"
- So, lets say you have a range,
- so you start at 0, you want to go up to 8.
- And I want to go up by 2 instead of 1.
- So then I went [0, 2, 4, 6] and I
- -- I should have said it more precisely.
- Up to but not INCLUDING 8
- -- the upper part of the range, you don't include it.
- So it goes up to that and it increments it by 2,
- so you could do it like that
- or maybe you want to start at 3
- and you want to go up to but not including 31
- and you want to go up by steps of 3, there you go.
- [?] up to but not including 31.
- So it can generate a whole bunch of lists
- and the reason why this is useful is that
- we can use these inside of loops.
- And so the first type of loop
- I'm going to introduce you to is a "for" loop
- So this is the interpreter down here,
- here I'm going to actually write a program.
- So, this program is really just to show you
- what a "for" loop does.
- So I could say "for i" and i is this variable
- that's often used in "for" loops and iterative loops
- and iterative means
- you keep doing something over and over again.
- "for i in" and let me do a simple one, "in range,"
- and I'm just gonna say, 5, "range(5)"
- So this will generate a list, [0, 1, 2, 3, 4],
- so "for i in range(5)" and this colon says,
- "Now we're going to have a sub-clause
- of what to do on each of those iterations."
- Let's just print out what i is.
- And so before we even see what this is going to do,
- let's just think about it.
- It's saying, "for each time we go through this loop,"
- "for each of the things in the range,"
- "make i refer to them,"
- and then each of those loops is going to
- refer to a different one,
- "and then you want to print it out."
- So let's just try this out.
- So let me save this program, and now let me run it.
- So there you go.
- What it did is, it created a loop,
- so each time it goes to the loop
- it assigns i to a different element in this list.
- As you remember, this list was [0, 1, 2, 3, 4]
- (Did I say 0,1,2,3,4,5 before?
- No, it's just 0,1,2,3,4 -- up to but not including 5.)
- So each time you go through the loop,
- i goes through each of those items
- and this program just says, "Print out that i."
- So the first time we go to the loop, i = 0 so it prints it [0] out.
- Then it prints out 1, then it prints a 2,
- then it prints a 3, then it prints a 4
- So it just keeps going through it that many times.
- If we said this was "for range(10):",
- now let's run this program.
- Let's see, it'll print...
- 1 through 9.
- It'll print 1 through 9.
- Or, I should say, 0 through 9!
- 'Cause that's the first element in this list.
- But you can do more interesting things.
- Let's say that you wanted to take the sum of 0 through 9.
- So what you could do is define another variable here.
- So before I do anything the sum is just going to be 0.
- And then what you could say is,
- "each time you go through the loop, why don't you..."
- "add to sum, define it to be the previous sum
- plus what i is in this part of the loop."
- And just so we can keep track of it,
- I then want to print the sum.
- And let's see what this thing does.
- So save it and then let me run it.
- So there you go.
- The first time you went through the loop,
- sum is 0 and i is 0, you add 'em together
- you print the sum, and you get 0.
- The next time you go to the loop,
- i is 1. We add that to the previous sum (0),
- print it, you get 1.
- The next time through the loop, i is now going to be 2.
- Add that to the previous sum, 2 + 1, you get 3.
- Next time through the loop,
- i is 3, add that to previous sum, you get 6.
- So the sum of all of the numbers, 0 through 9, is 45.
- So just that, that's kind of a useful program.
- And just to make the point a little bit clearer,
- let me actually walk you through this
- On my little drawing tablet.
- Paste my code, that I just wrote up here.
- And let's just think about what this is doing.
- So, we have a couple of variables here.
- We start off with this variable "sum" that is set to 0.
- There's two ways to visualize variables.
- A common way to visualize
- (this isn't necessarily wrong,
- it's sometimes an easier way to think about it)
- is to view them as buckets.
- But in Python, the really correct way
- (and you saw that with lists)
- is to view them as referring to something
- So, if you view them as buckets you can say,
- okay, from the get-go,
- the number in the "sum" bucket is 0.
- But the reality is that
- the variable "sum" is now referring to the numeric literal,
- the actual number 0, sitting in memory someplace.
- Now we enter into the "for" loop.
- And what the for loop says is
- let's iteratively assign i to each of the elements
- in the list generated by range(10)
- So let's be clear,
- if we call this, right over here, range of 10,
- this will generate a list that looks like this:
- ... up to but not including 10,
- this is ten elements right over here.
- And then we go into the "for" loop.
- So the first time we go through the for loop...
- I'll think about it two different ways.
- So I'll, 1.) I'll think about it the "bucket" way.
- So we have this variable i,
- it can actually only be used inside of this clause
- (we'll talk more about scope,
- or where a variable can be used,
- but this i variable is only good inside of this clause.
- The sum variable, the way I've defined it,
- I can use it anywhere --
- outside the clause or inside the clause,
- because it was defined outside of the clause.)
- So you have your "i"
- So this, if you think of it as a bucket
- the first iteration is assigned to this first item over here.
- So i is going to be 0.
- The reality, the better way to think about it,
- is i is actually referring to this first element
- our first time through the loop.
- And then we go through the loop, and it says "sum + i"
- well sum is 0 and i is 0, you add 'em together, you get 0.
- So sum is still going to be 0, or still referring to 0.
- And then it prints the sum out, this line over here.
- It says, "Okay, the sum right now is just 0,"
- and it printed it out.
- Then it goes back to the for loop and it says,
- "Look, are there any more elements that I can assign i to?"
- Well, sure there are!
- There's all of these that we have to go through.
- So then it makes i the next element in the sequence,
- so now i is going to be the next element
- in the sequence,
- or another way to think about it is in the "i" bucket,
- if you want to view them as buckets,
- i is now set equal to 1. And now we run this again.
- And we say, "Okay, sum is 0, plus i. i is 1.
- So sum is now going to refer to 1."
- And when you print sum, it will now print 1.
- Then, the for loop will say,
- "Are there any more elements
- in this list over here to assign i to?"
- Well, sure there are!
- There's all of these others, so let's keep going!
- So now, i can now be equal to (or can refer to) 2.
- So if you think of it as a bucket, i is now 2,
- or you can say i is now referring to 2
- (and you can ignore these right over here).
- And we go through the loop again; sum is now 1,
- 1 + i, i is now 2; 1 + 2 is 3.
- So sum is now going to be 3
- this time through the loop.
- So sum is now referring to 3, you print sum, you get 3.
- And you keep going all the way,
- i is going to keep referring to
- as we go through the loop,
- the loop will keep going
- as there's more things that i can refer to,
- that we can keep incrementing i
- to these other values over here.
- And eventually, i is going to be 9,
- we're going to evaluate this,
- and the for loop says,
- "Hey, is there anything left in this list that i can be?"
- And you're like, "No! There isn't anything left in this list!"
- And then the loop is going to be done,
- and we'll break out to it and the program will stop.
- And that's exactly what we saw happening when we ran the program over here.
Be specific, and indicate a time in the video:
At 5:31, how is the moon large enough to block the sun? Isn't the sun way larger?
|
Have something that's not a question about this content? |
This discussion area is not meant for answering homework questions.
Discuss the site
For general discussions about Khan Academy, visit our Reddit discussion page.
Flag inappropriate posts
Here are posts to avoid making. If you do encounter them, flag them for attention from our Guardians.
abuse
- disrespectful or offensive
- an advertisement
not helpful
- low quality
- not about the video topic
- soliciting votes or seeking badges
- a homework question
- a duplicate answer
- repeatedly making the same post
wrong category
- a tip or feedback in Questions
- a question in Tips & Feedback
- an answer that should be its own question
about the site
Share a tip
Suggest a fix
Have something that's not a tip or feedback about this content?
This discussion area is not meant for answering homework questions.