today's topic is for statement in Python for statement is also known as for loop so to begin I have py charm open so let me create a new file and I'm going to call it four okay now in order to understand this we will work on a sample problem let's say you have a list of your monthly expenses so for January February each of these months you have your total expense amount and you want to calculate the total of all these monthly expenses so how do you solve this problem in Python you want to use
Python to track down your monthly expenses and python program should tell you the total result so for that first you need to create a list and we are going to call it exp equal to I will create a list this is how you create a list and you're going to store all the monthly expenses so let's say in January you spend twenty three forty dollars your February expense was twenty five hundred your March expense was twenty one hundred your April expense was thirty one hundred because it's your marriage anniversary you have a big expense and
your May expenses twenty nine eighty hundred dollars if you are going with a traditional way without using for loop then you will create a variable called total and you will add each of these individual items so you'll do this and you will print total at the end so when you run this program it will print the total so I'm going to run for I have two files if and four that's why it's showing me two options so I'm interested in executing four so it is telling me thirteen thousand twenty is the total expense from 1
January all the way to tell me now the problem with this code is that you are writing a long expression here what if your list had like 100 items and if you start writing all of these you'll become granddaddy by the time you done writing it it's like freely tedious so a programming language should have something better and that thing is a full loop so let me now do the same thing using a for loop so I'm going to command this line so if you use control slash shortcut in py jam it will comment it
or you can just put this character here it means this line won't have an impact so now you can say total you can create a total variable and you can say my total expense is zero and now you are going to run a for loop so the syntax of for loop is this so you can say for item in exp column what this means is for the special keyword and then using item in exp meaning for each of the items in exp list do this what do we don't want to do you're going to say
total equal to total plus item what happens because of this is it will run in a loop and every time it will keep on adding this individual items so let me run it excellent so I'm seeing the same result and see my code looks much better so compare this core versus this code here you were doing lot of typing versus here is plain simple so when you write a for loop internally computer will expand your cool into something which is shown in the diagram here so as you see in the diagram at each iteration it
is taking each of these individual items and adding them into your total so you don't have to write all the code that is shown in the diagram but instead the full loop will do that for you and it makes your code look beautiful and readable now let me show you the same thing using py charm debugger so if you have seen my pH I'm intro on debugging etc you you would have some idea on how to set breakpoints and do debugging if you are not sin then I would recommend going back to that video and
take a look so let me set a breakpoint here so when you do left mouse click or use control f8 it will set a breakpoint and you can say run debug okay now I am here okay so as you see let me expand this guy my exp list is this two three four zero and so on you can use the step or function to go on line by one line so my total as you see here is zero and I just enter the for loop when I move into next tap what happens is my item
is 2340 which you can see here so the for loop when it started it first picked up the first item which is 2340 my total is it is still zero and I am saying total equal to total plus item so my total is zero and my item will be twenty three forty so after this statement gets executed you can imagine what will be the value of total so next nice so you see like Toto has a value twenty three forty and then I am one to my next item so when I say next item my
next item now is twenty five hundred as you see here my total is twenty three forty so now total equal to total plus I had a meaning twenty three forty plus item which is twenty five hundred so now my total is forty eight forty so this keeps on going until the very end so in the end your item is twenty nine eighty in your total is already this much and when you say next now see your total become thirteen thousand $20 and you are in the very last step so now since you have traversed all
the values in your list this will come out of that followed so let me hit next so now it came out of for loop and your final value for the total is this and when you run it it will bring that so let me look at the console and you got this value so just to summarize for loop should be used whenever you want to do same operation on a range of things okay next thing I want to cover is a range function what if you want to print number one to ten so let's do
that using for loop so we'll say for I in range so range is a special function in Python and the way it works is you specify two indexes start and stop so my start index is one and your stop as it is indicated by the tooltip this tooth is pretty useful so stop index should be wherever you want to stop plus one so I'm going to pin one to ten hence I will put eleven because the second index is always your end index plus one column hit enter and then just say print I what I
means here is I is a variable that you are creating in your for loop and that variable has a value coming out of this function so on each iteration range will give you one new value so let's see what happens so you can just run it nice so as you notice here it printed one to ten okay so range just gives you a range of numbers that you specify using start and end index you can use the same thing to print a square of all these numbers so you can just say I cross I and
this will print square of all the numbers from one to ten so again and you see 1 4 9 16 and the square of 10 is 100 so whenever you want to do this kind of operation on a range of numbers you should use range okay now let's go back to our monthly expense examples so let me create that same variable for the list again and now what I want to do here is I want to print monthly expense along with the month number so what I'll do is I will use LAN function so I
would say for I in rain Len exp so what this land will do is it will return the length of this list so what is the length of the list it is 5 so when you say for I in range 5 what you're doing is you are omitting the start index in your and an index will be the length of the list so you will say print your month is I plus 1 why am i doing I plus 1 because in the range function I am specifying only the end index and if I don't specify
my start index it will be 0 by default so if it is 0 I don't want to start from 0 I want to start from 1 that's why I'm doing I plus 1 and I will say my expense is exp off I so again expense is a list so you can use an index in a square bracket to access each individual elements so I in this for loop for the first time it will have a value 0 so when you say exp 0 you will be referring to this value when you say exp 1 you
are referring to 2500 value and so on also as we are calculating a total we'll do the same thing as before create a variable called total and on each iteration you're saying total is total plus exp of I let's run this awesome so it is saying month 1 expenses this month to expenses stress but hey we forgot to plain total so let me do that so you will say print my total expense is my total and again run it good so it printed induced expenses here and at the end I have a total expense now
let me just quickly show you this land thing in idle so that you know what went behind the scene so when you have this list like this and when you sell em of exp you see it is saying return the number of items in a container so exp is a list which is a container and when you sell and exp 5 so this for loop was equivalent as writing length range file okay a range usually have two parameters we could have said this as well this is starting index this is ending index but if you
don't specify it by default it will assume it to be zero okay so you can have like this or you can have just this but we knew it is 5 but let's say if this list was long you don't want to go and calculate it that's why we had we had it like this now full loop will normally go through your entire list or entire range that your specified here but often there is a need to break in between you want to do some operation and one certain goal is achieved you want to stop the
for loop and terminate in between so let me give you the example let's say you lost your keys your car keys in your home and you now begin searching it so you will go to different places you will go to your living room your closer you will look around chairs to see if there is a key and once you find the key what will you do you will stop your search so let demonstrate the same thing using for loop and a brief statement so let's say my key location is chair so my keys are actually
placed on chair and I don't know where they are but I know all the locations in my home so all the locations in my home are garage then I have living room and then I have closed it and I have chair so let me put chair here before close it so I'm going to sort through my home for my keys in all this location in this particular order so again you can use follow because for loop is used to go through each of these items one by one so you say for I in locations meaning
such each of these locations one by one and if I is equal to this is a comparison operator or double equal to sign if I circle the key location then you will say print keys found in this particular place right and once the key is found now you don't want to go complete your for loop you want to break so when you write this break statement it will just come out of the for loop it will just terminate in between and if that is not the case if Y is not equal to key location then
you want to sleep print key is not found and I okay now let me run it and see what happens nice so it first went through garage and says key is not found garage then it went through living room and his saying key is not found in living room then it encountered chair and chair was a key location so it it printed this one and then it broke in between it didn't sort through close it so use break whenever your goal is achieved and you want to just terminate so again let me show you by
debugging this so just debug this use a fade to go through each of these tabs so your eye is garage so you're looking into garage and assess key is not found in garage then its living room and you're saying key is not found in living room but third comes chair and since key is on the chair it was a keys found in chair and it will break so now see what happens so you see you came out of the program it didn't go through this thing this is very useful when you are doing real-time programming
and you have a list which has let's say 10,000 items you don't want to just go or each of these items because it will waste some of the CPU cycles it will be overburden on computer for the work that's gonna give nothing right your goal is achieved why do you want to continue with the full loop and go over the remaining elements another statement that is used frequently in for loop is continue so when you are running a full loop and doing same operation on list of items sometimes for some items you want to skip
that operation so in that case you will use continue so again let's work on a simple problem where between one to five you want to print the square of all numbers except the even numbers so you want to skip even numbers so you will say for I in range so if you want to go from 1 to 5 you have to say 1 to 6 because it will exclude the second index so it should be always your n number plus 1 now you want to skip even numbers and for remaining numbers you want to print
I cross I so let me just first run this guy so when you run this what's gonna happen is let me see what happen okay so let's run this first so when you run it it is printing square of all the numbers between 1 to 5 but you want to skip even numbers which is this and this so for that this is your main operation that you're doing follow up just before that just say do the check for even number how do you do a check for even number you say I personally to equal to
zero meaning if you divided by two and if the remainder is zero then you will say continue so this continued statement what it will do is once you are an if condition if it is satisfied you will just continue from here and you will jump back here in the for loop for the next iteration hence you will skip any coal which is written below this line below this line inside the for loop so if you have something outside the for loop used to do it so now let's run this awesome so you see the square
of one is one square of 3 is 9 and square of 5 is 25 but we skipped two and four okay now the last thing I want to cover is vial statement so one statement is similar to for loop with a little bit difference in the syntax so let's say you want to print number one to five how do you do that using while loop so in while you say while and then the condition print I and I equal to I plus one so while loop takes only one condition and until that condition is satisfied
to be true it will just keep on iterating so if you run it you will be saying one two three four five okay now be careful whenever you're doing while loop you have to increment this counter if you don't increment this then it will just go into infinite loop so what happens is the way this works is let me debug it so here again I'm using f8 to go to next line so your I was one as you see here I was one and you printed I first and then your eye is too then your
eye is three so when it is here it is saying I is the I less than equal to five meaning 3 is less than 5 hence you continue inside the loop now I is 5 so eyes no I I is 6 this time so now 6 is not less than 5 so it will come out of it okay now if you don't have this here then it will be pretty dangerous so what will happen is it will just keep on iterating and it will cause an infinite loop so that concludes our episode thanks for watching