Hey everybody.
Welcome to the next video.
So in this video we're going to be going over loops now loops are a way for you to repeat certain sections
of code for a certain amount of time or a certain amount of iterations.
So how many times you want to loop through this code ten times.
Do you want to loop through every character in a string.
Do you want to look through every element or value in a list every element dictionary.
Now what we're going to be I'm going to leave the looping of through lists dictionaries tuples.
I'm going to leave all of that to their own video but I want to take you through how loops work in a
nutshell as law as well as while loops.
So let's create some base information right we'll do a temp string we'll name it red.
Right.
Now what if we want to loop through every every character in the string.
So the basic loop is going to use this of all loops are going to use a similar format.
So we're gonna do four and then some sort of amount.
So or I'm sorry not so far.
This is for four.
And then the value that you want to use inside the loop.
So for you know every character in temp string we want to do something.
So in this case we're going to print char so if we go ahead and look at this we should see printing
R E D.
Now we can print char in print.
See dash.
And then you know some sort of numbers so say the actual
format.
Char comma tapestry.
So you see we could print everything with it.
You can do a lot of infamy.
We can do a lot of interesting things when you start looping through data of some kind.
This is a quick example of how you loop through a string itself because what it does well Python does
is it treats the string.
It separates every character into a list and then it goes through loops to that list.
And then this is the variable that holds the current value of the iteration so you can think of this
as I.
If you've seen if you used other thing if you use other languages now what I want to go through here
is let's say we want to do certain let's say we don't want to loop through a string but we want to loop
through some code a certain amount of time so doing something a little different based on let's say
a certain number.
Right.
So if we do for X so the number in range which gives us we give it a number we give a range of numbers
so if we say 10 it will loop through it 10 times so what range is it range does is it creates a list
of 0 1 2 3 4 5 6 7 8 9.
Because remember counting starts at 0 then we're going it's going to run through that and for each element
in that list it's go for zero for example print.
If I print x it'll be zero and then the next iteration will be 1 2 3 etc. so we can see here we see
0 1 2 3 4.
Now it's important to understand range and that it gives it's based on a set of numbers.
So you can't give.
Don't give it a float.
Give it an integer of some kind.
Now this can be useful for a lot of things.
In most cases you're going to want to use for loops for looping through some sort of lists or something
along those lines.
However there's a few types of loops that you're going to want to use in other places.
So what we can do here is we'll give this a little bit of a break.
We'll give it just a a new line as an example.
So inside a press statement you can do a forward slash or backslash and that'll bring a new line that's
equivalent to you hitting enter on the keyboard.
T is a tab.
So it's like you hitting the tab key C adds in roughly four spaces.
So we're just gonna give it a new line just for some breakage purpose so we can have a little bit of
space down here.
Now let's say we have I we're gonna set to zero.
So this is our index counter it's not always required but in this particular case we'll we're going
to use it so then there's something called a while.
Well look similar to a for loop but it also looks similar to an if statement because it works somewhat
similar to an if statement.
So it's going to say you're going to want to do whatever's in here while this condition is met.
So if we do I while I is less than 10 we're gonna do this position or this this code.
So in this case we're going to print by now if we run this
we're gonna see that it's just indefinitely printing zero like you try to put some data in there it's
just going to definitely print zero.
The reason for this is because I never gets increased so inside the loop if we do I plus equals one.
So that means every iteration it's gonna be adding one to I so one will be in the first round.
When this first runs it will print.
I sort of print zero.
Then I will be set to 1 because it's adding 1.
And then this will now print one and then we'll be set to two.
And it just keeps going through until I is less than 10 then it will then print it will then break out
of the loop and we can even put a print statement down here that's not inside of the loop and say made
it so if we go into this again we can see we go 0 1 2 3 4 5 6 7 8 9 and then we stop
So another interesting piece here is that again we didn't start with one and it stopped at 9.
So technically this was 10.
However if we do less than or equal to 10 then at that point we will then get our 10 here.
Then we get our made perfect but with while loops maybe you want to break it sooner so you might be
printing this.
But let's say you'd want to do if I is equal to five right.
If I is equal to five.
You want to break.
Now break is a keyword in Python.
That allows you to break out of a loop of some kind.
So in this particular case you can do break.
Now for other for loops in general you can do something called continue.
So if you see that if I put in here right above this first print statement so if I say if I is equal
to zero we want to continue basically continue means it says restart the loop so if I did this right
now this would never run and then it would just indefinitely repeat
just sit there.
We're not printing anything things we haven't made it to the print and it's just saying Isaac of zero
continue.
However I just want you to understand how continue works but I also want you to see how brick works
as well.
So right now we print this should print 0 1 2 3 4 and 5 and then it should break and say made it perfect
so we can see it goes through 0 1 2 3 4 and 5 and on 5 because this got this was 4 plus 1 to 5 prints
5 and it says If I go to 5 we're going to break and it stops the loop so we never reach all the way
to 10.
So this is how you kind of use for loops and while loops now play around with this a little bit make
sure you understand the concept here.
When you when we start using lists and dictionaries and couples will go through how to loop through
each one of those specifically.