#21 Python Tutorial for Beginners | For Loop in Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome back aliens my name is Ivan vetti and let's continue this series on Python so in the last video we have talked about loops right and specifically it was a while loop in this video we'll talk about for loop so while loop and for loop they both are loops right but then the working is different in fact normally when we talk about while loop it is for certain 2 iterations and it is positon conditions so if ever the while loop we specify a condition but that's not the case with father because in for loop it normally works with sequence whatever sequence I'm talking about think about least think about tuple think about string so what I'm basically talking about is let's say if you have a laced here and in this list AHA certain values it can be any random value let's say my name Naveen 65 and if I say they'd say 2.5 so these are I have this values one is string one is integer and when his float doesn't matter what type of value you have there I want to print this values it's really easy actually you can simply say print and you can print the value of x this will work okay let me just run this code and you can see record the output but what if you want to get the output individually I want to print the values one by one so in this case you will be using a for loop now for loop works so we have to say for then we have to use a variable with this we will say this time I now I represents one element of this list at a time so example initially I will refer to Naveen then I will refer to 65 then I will refer to 2.5 one by one so you have to say for I in X so one by one it will fetch the value and again this is a suit so you have to use a code in there and you can write lines of statements again the point is you need to make sure that you use a proper indentation so I will be printing the value that's it I will printing the value of I that's it said those there's no iteration unlike a while here so we don't the enjoys the variable we don't take the condition and we don't increment or decrement by ourselves which will be done by for loop itself this which is smart right so now if I run this code you can see record the values one by one so yes if you want to print all the values from at least use for loop so for loop normally can be used with sequence example list which you have worked with now all a string let's say you have a string here let's say the X is not at least now it is a simple string which is Naveen and then I want to print the value of this one by one so one way is you can use an array right so we can use a while loop here as well and then you can use that square back and you can specify index index values otherwise if you run this code you can see this is how you're for the works so what it will do the value of I first will have N and that will have a let me let me just do that again the thing which we are doing from a long time which is debug so I'd like and say debug let's trace it so if i trace you can see the value of x is naveen and the value of i is and now i'll give you fire repeat if I said next iteration you can see the value of I it will become a and the next iteration value of I becomes V the value of I becomes I and then value of I becomes n so that's moving for you and you're printing all the values right so if I see the console you got all the run is one by one right of course I forgot to one do one more so you got live in that so that's the advantage of using for loop oh can I use for loop with something else yes we can example we can also use this with tuple or we can use with say but then we have one more example in you can do you can specify the list itself okay instead of having a different variable you can have your list here even that works here so you have two six Paul and if I done this code you can see we got one by one so you can have your list yet itself so syntax is you mention for then you say I in and then you mention the collection or the sequence it can be a list it can be a string everything works in fact what if I want to print the values from 1 to 10 because of course why we can do that we can do that in while loop can we do it here and this is yes for that we have to use range if you remember in sequence we have talked about range in range you can specify 10 what it means it means start from 0 and end at 9 so in total we have 10 values and if I have this code you can see record and 0 to 9 so we got 0 and then it ends at 9 so in total we have 10 values so we can use range as well and using this range you can perform certain operations in fact in rain you can actually start from different values it's I don't want to print from 0 to 10 I want to print from let's say 11 to 20 in that case you would say hey I want to start from 11 I want to end at 2070 so we want 20 included right so we'll say 21 and every iteration will have 1 so it will be 1 by 1 iteration so if I say run so you can see we got values from 11 to 20 so you have to mention three things starting point ending point and the iteration it will be difference would be 1 or 2 example if I say this is 2 in this case so look at the values now it will be different values 11 13 15 17 and 19 so if there's a difference of two if I say 5 you can see we got 11 and 16 that's it so this is how you specify the gap there what if you want to print in Reverse let's try can I start with 20 and if I end at 11 if I run this code it will give you it is giving you nothing because it always goes in ascending order we have to mention go in reverse order so you have to say minus 1 so it will start with 20 and then 1918 and it wasn't that way so you can see we got from 20 to 12 but there's a problem you have to make sure that it goes till 11 right so you have to because the value 11 is excluded so we have to use 10 so that it will include 11 that you want this again and it works so this is how you can reuse range and this how you can use for loop now in fact we can do one more thing so you can see if we have a range of 20 and then I'm printing the value of I in fact I will just go I will start with 1 so much from 1 to 20 so I will inclusive I will make sure it is 21 so that it will include 20 and the iteration would be 1 or even if you don't mention that that's fine if I say print value we can see it just 1 to 20 what I want is so every time the value which is a divisible by 5 it should not get printed okay so I want that so I want to print only when the value is not user by 5 so we can use a inside phone right so we can do that so we can have if condition inside for if you want to if you have if you want to have that condition so you can say if I mod 5 is equal to equal to 0 don't print otherwise you can print okay but how can I do that so what we can do is we can say not equal to so print only when it's not equal right so we can do that so print only when it is not equal to say fine on this code if I say oh we got an error because I missed something and I'm also using brackets because I have this habit of working on Java so it's the anyway it's optional in Python so let's run this and you can see we got we got one and regard for you can see we don't have five there because we are taking about the condition so yes we can write it inside of four so since we had if inside fault we can also have fall inside of four which is a straightforward loop but then we will not be doing that here in the next video we'll play with some patterns you know we'll try the dorsum patterns and we'll try to understand how do we do that so in the next video it will be fun so I hope you are enjoying the series it means the comment section and dukey like button if you're enjoying it I'm watching everyone bye you
Info
Channel: Telusko
Views: 854,066
Rating: 4.928257 out of 5
Keywords: telusko, navin, reddy, tutorial, java, python, loop, while, for, difference, syntax, example, code, debug, tuple, set, nested, range, collection
Id: 0ZvaDa8eT5s
Channel Id: undefined
Length: 7min 27sec (447 seconds)
Published: Thu Jul 12 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.