So in previous videos, we saw a few examples of how to use for loops in Python.
For example, if you have "a" being equal to a list with three elements.
"Apple", "banana" and "republic"
You can print each element with "for element" in "a" colon
4 spaces, print element.
And when you run this cell, you should see apple banana and republic, and you do.
So another way of doing the same thing is by using the indexes, or the indices of these 3 elements.
You can do that with for i for index, in range of 0 comma 3 colon.
Because this way, range will go from 0 to 3 but not including 3.
So that would be 0, 1 and 2.
Actually, a shorthand for this would be range of just 3 and it does exactly the same thing.
So lets see how that works with print "i"
And you can see that 0, 1 and 2 as expected.
So using this as the index, you can just write print "a" square brackets "i"
And then it's gonna print "apple", "banana" and "republic".
And instead of using 3, we could also use "len" parentheses "a"
which will return the length of "a" which is 3
So this is going to do the same thing as what we had before
So why would we want to use the second method instead of the first method?
The way I think about it is by default, you should use the first method
which is like for element in "a" because that's a much simpler method.
But in some cases the index matters, not just the element.
For example if you wanted to print "apple" once, "banana" twice and "republic" three times
here's what you can do
For "i" in range of len of "a" which is the same as what we had before
and then within this "for loop", create a new "for loop"
with for "j" in range of "i" plus 1
and this way when "i" is equal to 0, "j" will be just 0.
And when "i" is equal to 1, "j" will be 0 and then 1
And when "i" is equal to 2, "j" will be 0, 1 and then 2.
So actually the value of "j" doesn't really matter because we're not gonna use it
but the important thing here is that we're going to go through this inner "for loop"
which includes "j" once when "i" is equal to 0
and we're gonna go through this loop twice when "i" is equal to 1
and we're gonna go through that loop 3 times when "i" is equal to 2.
So within this "for loop", within the outer "for loop".
We can print "a" square brackets "i"
and that should print "apple" once, "banana" twice and "republic" 3 times.
Let's run this cell, and that's exactly what we see.
Ok, let's now quickly go through the 2 problems I gave you in the last 2 videos.
In tutorial 5, I gave you this problem.
Can you compute the sum of all multiples of 3 and 5 that are less than 100?
So that would be the sum of 3, 5, 6 and so on, and then 15 which is a multiple of both 3 and 5
18, 20 and so on.
And here's my solution
First initialize a variable, let's say... "total" to 0
and then run a "for loop" for "i" in range of 1 comma 100.
So that would be all the positive integers that are less than 100
and in this "for loop", just write if "i" mod 3 is equal to 0
So if "i" is a multiple of 3 then add this number to "total", with "total" plus equals "i".
And else if "elif", if that number is a multiple of 5 then add this number to "total"
After this "for loop" just print the results which is "total" and we get 2318, so that's the answer.
But instead of using "elif" we could also do, if "i" % 3 is equal to 0
or if this number is a multiple of 5 which is the same thing as "i" mod 5 is equal to 0
Then add "i" to the total number and then print "total".
So this "or" statement says we want to do the following which is total plus equals "i"
if the first condition or the second condition is true.
And if both of those conditions are true, then we still want to do the following
and that's good because when "i" is equal to 15, we still go through and we still add "i" which is 15 to total.
And then once we run this cell, we get the same answer: 2318
And here's the problem I gave you in tutorial 6.
We were given this list and the assumption there was that we don't necessarily know the content
or the length of this list but we know that it's sorted in a descending order
so that the largest number comes first and then when you go right
the numbers always stay the same or go down.
And we also assume that there's at least 1 positive number in this list
and we wanted to find the sum of only the negative numbers here.
To do this, here is my solution.
Let's first initialize "total2" to be 0
and then I'll initialize an index, lets call it "j", to the length of given_list minus 1.
So that would be the index that corresponds to the last element of this list
and then what I'm going to do is I'm going to change "j" so that we can iterate over only the negative numbers
starting from the right because just in case there are a lot of positive numbers in this list
but only a few negative numbers, we dont really wanna go through the whole list if we can avoid it.
So we're going to do that with while given_list square brackets "j" is less than 0
so while the current element that we're examining is less than 0, add that item to total2.
So total2 plus equals given_list square brackets "j" and then go to the next item by decreasing "j"
by 1 so you could write "j" equals "j" minus 1
but it's the same thing as "j" minus equals 1.
And after that, just print total2.
So the answer is -17 let's check that, -2 plus -3 plus -5 thats -10
plus -7 thats -17.
So it looks like this solution is correct.
Ok, if you're following these tutorials in real-time you might have noticed that
the pace of me posting these tutorials has been kind of slow recently
and it's just because I've been working on other videos.
So if you don't wanna wait for the next tutorial, I'd recommend the following 2 courses I have on
Lynda.com and Pluralsight.
And you'll be able to take them for free
so the one on Lynda.com is called
Get Ready for Your Coding Interview.
It is about coding interviews but I cover a lot of basics in Python so it should be a good course for you
to take after this course.
Actually my voice doesn't sound great in this course, but
it's just because I didn't know what I was doing back then but the content should be good.
And the second course I'd recommend is called
Introduction to Data Visualization with Python on Pluralsight
and this one's good if you're interested in getting into data analysis with Python.
And like I said, you can take these courses for free with their 30-day free trial and 10-day free trial.
I'm gonna put the links in the description below for these courses but don't worry because
I'm gonna come up with more Youtube tutorials too.
Ok, again I am so sorry if you've been disappointed with the pace of me posting these tutorials
but hopefully I will see you guys soon in the next video