Introduction to Loop

Python Programming Loops in Python
7 minutes
Share the link to this page
Copied
  Completed

Transcript

Welcome, everybody. So today, I will be introducing you to all loops. So Python loops. So before starting with a loop, let me write a program. So suppose I want to print a statement, suppose Hi, how are you. So I want to print this statement.

And for that, I need to write this statement 10 times otherwise I cannot print this statement for 10 times. So I just copy this code and paste it 10 times so that I get the result 10 times. So this is the first second, third, fourth, fifth, sixth. So by mistake I've run this code, so you can just copy it again and paste it over there. So as many times I write this code, I will get the result the same number of times. So the number of times I've written is 1-234-567-8910 1112.

So I've written this particular code 12 times and I've got the result two f times, but this is really tedious. If I want to do it 100 times then I have to repeat it 100 times. So for this what we can do is we can use a loop we can use a for loop. So let me show you how to use a for loop. So for using the follow view to use a keyword for and then I, now here is a variable, which will take up a value of 10. So here I have to write for i in range 10 and then outside this bracket, give a car loan and press Enter.

Now whenever you will give a car loan and give up enter This thing will be indented, my cursor is now indented. Now here you have to write print and within this print write Hi. How are you. So now whenever you will print this, it will show you the result 10 times See, you got the result 10 times. So this is much more easier and much more compact without writing all the codes several times we have just written using a for loop and our work is done in much easier way. So, what's happening here is here, I is a variable.

So, by now you all know what a variable is. We have been talking about variable in Python for so long now, so I think you're familiar with variables now. So, here I become a variable in range 10. This means it creates a variable time range 10 means it start assigning the values 12345678 and nine. So i in range 10 means it's a statement that will take up a value up to that is 10 values it will take 10 values starting from zero. So I already told you that in machine in any computer language, the counting start from zero so from zero it will take 10 values so from zero to nine it will take so start counting from 00123456789.

So these are the 10 times I have written this particular text now let me show you something here. I didn't write for I In range 10 Now I will print all see what happened. See what you get, you get 0123456789 so now I think it is clear to you that whenever you will run a loop or anything or any counting in Python, it will always start from zero now for i in range 10 eva cola train here I'll write I into two. That is now I will get the result where I will not get the consecutive numbers I will get the value of i where it will just jump to Two times every iteration. So what happens? Well, the first value it will take zero.

So the next value it will take to the third value, it will take four, so it will just increase it by two, the value will be incremented by two, so run it, see, what do you get 0246 810 1214 1618. So can you guess what I got? This is basically I'm multiplying it by two. So what's happening is I first value of is zero to zero into two is zero, then the next value of i is one, so one into two is two. The next value is two, so two into two is four. So three into two is six, four into two is eight, five into two is 10.

So basically what I've got is I've got the multiplication value have to the table have to basically but here I got up to nine because I'm giving range 10. So from zero to 10, it will give you from zero to nine. Now if you want the up to 10, the multiplication of 10 also 10 cross two you want then just increase the range over here, I've increased the range to 11 and now run it and you will get 20 Okay. So starting from 00 1-234-567-8910 now, it will give you up to 10 because if you count it from one, this is a range of 11 so it has provided you the value up to 10. That is, if you multiply each value by two, you will get the result what I have got over here, so basically, I have just introduced you to the loop. Now we'll work more on loop in our next videos.

Till then keep practicing and change the values over here, do it one cross three, one cross four and three, that is our change the range over here and see the result unless and until you manipulate your code you manipulate your program are very difficult for you to understand. So I'll suggest you just keep on practicing change few range few values, and you will have a much clearer idea on this loop. So till then, goodbye. See you in the next video. Thank you

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.