hey guys in this video I'm gonna give
you a quick introduction to four loops
in Python and to download the sample
file for this video just go to CS loader
dot io / Python 5 so suppose you have
this lists a equals square brackets
banana comma Apple and Microsoft then
what if you wanted to iterate through
each element of this list meaning what
if you wanted to do something with each
item of this lists so one way to do that
for example if you wanted to print each
element would be to say print a square
brackets 1 which selects of course the
first item banana and print a square
brackets 1 and print a square brackets 2
so this method works but it's kind of
cumbersome because we need to repeat the
same statement over and over again and
that would be a lot of work for example
if this list had 100 elements so instead
we can use something called a for loop
so you can write for element in a the
lists : 4 spaces print element and this
whole block says for each element in a
do the following which is printing
printing each element let's see if it
works and it does it prints out banana
Apple and Microsoft and you can have
multiple statements in this for block as
well by writing print element again and
note that we have 4 spaces in front of
this line as well
just like we saw in an if block and when
we run this so we see that each element
is printed twice ok let's take a look at
another example here we have B square
brackets 20 comma 10 comma 5 2 print
each element of this list we can write
for element in B just like before print
element and actually this word element
is something we can choose so we can use
pretty much anything we want
so let's write for e in B print E and
once we run the cell each almond is
printed and what if you wanted to find
the sum of this lists
one way to do that would be to
initialize a new variable called let's
say total and let's initialize it to
zero and in this for loop we want to add
each element e to total we can do that
for example with total equals total plus
e
let's get rid of this print statement
and so what this four-block does is
it'll go through each element in B so e
will be initially 20 and then 10 and
then 5 and then for each e
we're going to add it to total so total
will be originally 0 and when e is equal
to 20 we'll have total the new value of
total is equal to the old value of total
which is their plus 20 so total will be
20 and when e is equal to 10 the old
value of total will be 20 and E will be
10 so the new value will be 30 so at the
end we should have total being equal to
35 which is some of these lists let's
check that by printing total after the
for loop and let's run this cell and we
see 35 which is expected so what if
instead you wanted to find the sum of 1
2 3 & 4 one way to do this would be to
create a new lists with these four
numbers but there's actually a better
way of doing that and that is to use the
range function in Python and to use it
you can just write range parentheses 1
comma 5
this means create a range of numbers
starting at 1 through 5 but not
including 5 and this method is better
for example if we wanted to find the sum
of 1 to 100 you probably don't want to
write 1 through 100 explicitly because
that would be a lot of work so like I
said range of 1 through 5 creates a
range of numbers starting from 1 till 5
but not including 5 and it's sort of
like a list and to see what's inside you
can actually convert it to a lists with
the lists function so I just wrote lists
parentheses and inside the parentheses
of 1 comma 5 so we're creating a new
range and then putting it into the list
function to convert it to a list let's
put it to a new variable C and then
print what's inside C with print C as
you can see range of 1 5 is sort of like
the lists 1 2 3 and 4 and you can use
this range in a for loop so you can just
write for I in range 1 comma 5 : let's
say print I and again this word I is
something we chose so this should print
each element in this range 1 2 3 and 4
and it does so what if you wanted to
find the sum of these four numbers you
can do that just like before with total
to equals 0 and that initializes this
new variable called total 2 to 0 and
then we can add each I these items 1 2 3
and 4
by writing total 2 equals total 2 plus I
and actually there's a shortcut for it
and that's total 2 plus equals I that
says the new value of total 2 should be
the sum of the old value of total 2 plus
I and that way we'll be able to add 1 2
3 & 4 2 total 2 which is at the
beginning 0 so we should get 10 because
1 plus 2 Plus 3 plus 4 is 10 so let's
check that by printing total 2 and we
have 10 okay let's take a look at
another example here we're going to use
range of 1 comma 8 so that's the range
of numbers starting from 1 to 8 but not
including 8 so if we convert it to a
list with the lists function and then
once we print it
we'll see 1 to 7 now out of these
numbers what if we wanted to find the
sum of only the multiples of 3 so here
the only multiples of 3 we have are 3 &
6 so we want to add them up and find the
number 9
it's kind of silly in this small example
but it could be useful when the range is
much larger so to do that we're going to
define a new variable let's call it
total 3 to 0 just like before and then
we're gonna run a for loop with for I in
range of 1 comma 8 and here we want to
be able to say if I is a multiple of 3
so we want to check for each number I if
that number is a multiple of 3 and if
that's true then add I to total three
ways total 3 plus equals I and to check
if something is a multiple of 3
we need to learn something called a
modulo operator here's an example of a
modulo operator let's say print 4 % 3
this says divide four-way 3 and this
modulo operator % gives us the remainder
of that division so 4 divided by 3 is of
course 1 with the remainder 1 so we
should be able to print the remainder
which is 1 and we did what if you had
print 5 mod 3 or 5 percent 3 this should
give us 2 because the remainder is 2 and
it did what if you had 1 mod 3 this will
give us 1 because the remainder would be
1 and what if you had 6 mod 3 the
remainder in this case is 0 so this will
give us 0 ok so to check if I is a
multiple of 3 we just need to do i % 3
or I mod 3 if this is equal to 0 then I
is a multiple of 3
so then if that's the case add I to
total 3 with total 3 plus equals I and
then we're done let's print total 3 and
just to remind ourselves range of 1
comma 8 is the numbers 1 through 7 and
the mall
4:03 are three and six so as the some of
those which you get nine here and we
just did okay here's a little task for
you to practice where you just learn can
you compute the sum of all multiples of
three and five there are less than 100
so to get all the positive integers that
are less than 100
you can just do range of 1 comma 100 and
you can check what it looks like with
the list function and you can print this
list with the print function and you get
the positive integers 1 through 99 so
what you want to do here is add up 3
which is a multiple of 3 5 6 and so on
and you'll notice that there are some
numbers that are both multiples of 3 and
5 for example 15 which is a multiple of
3 and 5 so if you're able to solve this
problem just let me know in the comment
below and if you're new here
you should join dozer gang by
subscribing to this channel and I didn't
come up with this name those were gang
one of my viewers did so thanks the
anonymous for that and of course to
download the sample file for this file
you can just go to the CS dojo dot io /
price of 5 and as usual I'm YK from CS
dojo and I'll see you guys in the next
video