**range increment 3.5** This is a topic that many people are looking for. **bluevelvetrestaurant.com** is a channel providing useful information about learning, life, digital marketing and online courses …. it will help you have an overview and solid multi-faceted knowledge . Today, ** bluevelvetrestaurant.com ** would like to introduce to you **For Loops in Python**. Following along are instructions in the video below:

“That we know a nlittle bit about lists. I thought i would use this nvideo nvideo to introduce you to loops and in nparticular for loops and if we have we might ndo other types of loops. But before we even ndo that i want to introduce you to a built in npython function called ranger. When i say built in it nmeans.

It comes with python and it s really na way to generate lists that have nintegers that increment so for example. If i njust say range of 6. It generates a list nfrom. 0.

Through 5. So it s 6 elements up nto but not including 6 if i wanted to include 6. I nwould have to do range of 7 and then i go all the way nup to but not including 7. But i get 6 in there if i want to have n1 through 6.

If i don t want to have that 0. There ni could do range starting at 1 and including 1 up to nbut not including 7. So the first item nhere will be included the last item will nnot be included and there we go we get 1 2. 3.

4. 5. 6. And what s cool about these nintegrated development environments.

These ide s this npie scripter that i m running is that when i type in a nfunction that it recognizes it actually will ntell me a little bit something about that function nin case. You forget it so i don t know if you ncan read this here. But it says range nstart stop step so right now. The default is nto increment by 1 each time.

But it says hey ni could tell you to increment by a ndifferent number. So let s say you have a range say you start at 0. And nyou want to go up to 8. And i want to go up nby 2.

Instead of 1. So then i went to 0 2. 4. N6.

I should have said it more precisely up to nbut not including. 8. The upper part of the nrange. You don t include it so it goes up to that nand it increments it by 2.

So you could do it like that so maybe you want to start at n3 and you want to go up to but not including ni don t know let me do up to but nnot including 31 and you want to go nup by steps of 3. So there you go it went up steps by 3 up nto. But not including 31. So it can generate a nwhole bunch of lists and the reason nwhy.

This is useful is because we can use nthese inside of loops and so the first type of loop ni m going to introduce you to is a for loop..

So this was the ninterpreter down here here. I m going to nactually write. A program. So this program is really just nto show you a for loop.

Does so i could say for i and i nis this variable. That s often used in for loops nin. Iterative. Loops and iterative means you keep ndoing something over and over again for i in nand.

Let me just do a simple one in nrange and i m just going to say. 5. Range of 5. So this will generate na list 0 1.

2 3 4. So for i in range 5 and this colon says nnow. We re going to have a sub clause of what to ndo on each of those iterations let s just print out what i is and so before we even see nwhat. This is going to do let s just think about it it s saying for each time nwe go through this loop for each of the things in the nrange make.

I refer to them and then each of those loops is ngoing to refer a different one and you want to print it out so let s just try this out so let me save this program and now let me run it so there you go what it did is it ncreated a loop. So each time it ngoes to the loop. It assigns. I to a different nelement in this list.

As you remember this nlist. Was 0 1. 2. 3.

And 4. Did i say 0 1. N2. 3 4.

5. Before no it s 0 1. 2. 3.

4. Up nto. But not including 5. And so each time you ngo through the loop.

It goes through neach of those items and this program. Really njust says print out that i so the first time we ngo to the loop. I is 0. So it prints it out then it prints out 1 then it prints a 2 then it prints a 3.

Then it prints a 4..

So it just keeps going nthrough. It that many times. If we said this nwas for range 10. Now let s run this nprogram you ll see it ll print 1 through 9.

I should say 0. Through n9 because that s the first element in this list. But you could do more ninteresting things. Let s say that you wanted to ntake the sum of 0.

Through. 9. So what you could do is you ncould define another variable here sum. So before i do anything the nsum is just going to be 0.

And then what you could nsay is each time you go through the loop. Why ndon t you define sum to be the previous sum. Plus. What ni is in this part of the loop and just so that we ncan keep track of it i then want to print.

The sum now let s see what nthis thing does so let s try it out so let me save it and nthen let me run it so there you go so. The first time. We went nto the loop sum is zero sum is 0. And i is 0.

You add them together you print the sum you get 0. The next time we go nthrough the loop. I is 1 we add that to the previous nsum. Which was 0 print it you get one then the next time through the nloop.

I is now going to be 2 add that to the previous nsum 2 plus 1. You get 3 next time through nthe loop. I is 3 you add that to the nprevious sum you get 6. So the sum of all the nnumbers 0 through.

9 is 45 so just that that s actually na kind of a useful program. But just to make the point na little bit clearer. Let me actually walk you through nthis. On my.

Little drawing tablet. So. Let me close my let me npaste. My code that i just wrote up here.

And let s just think nabout. What this is doing so we have a couple nof variables here we start off with this nvariable sum that is set to 0. And there s two ways nto visualize variables a common way to nvisualize it and this isn t necessarily wrong this nis. Sometimes an easier way to think about it is nto view them as buckets.

But in python..

The really ncorrect way and you saw why that s nimportant with lists is to view them as nreferring to something so. If you view them as nbuckets you can say ok from the get go the number nin the sum bucket is 0. But the reality is is that nthe variable sum is now referring to the numeric nliteral the actual number 0 sitting in memory someplace now we enter into the for loop. And what the for loop nsays is let s iteratively assign i to each of the nelements in the list generated by a range of 10.

So just to be clear if we ncalled this right over here range of 10. This will generate na list that looks like this. So. It will generate a list 0 1.

N2. 3. 4. 5.

6. 7. 8. And 9 up to.

But not including 10. This is 10 elements nright over here and then we go ninto. The for loop. So the first time we go nthrough the for loop and let me write it nso.

I ll think about it two different ways. So one i ll think about nit in the bucket. Way so we have this variable ni that can actually only be used inside of this clause. We ll talk more about scope or nwhere a variable can be used.

But this i variable is only ngood inside of this clause. The sum variable the nway. I ve defined it i can use it anywhere outside nof. The clause or inside of it because it was defined noutside of the clause.

So you have your i so. This is if you think nof. It as a bucket and so through the nfirst iteration. I is assigned to this nfirst item over here so i is going to be 0.

The better way to nthink about it is i is actually referring to this nfirst element. Our first time through the loop and then we go through the nloop and it says sum plus. I well sum is 0. And i is 0.

You add them ntogether you get 0. So sum is still going to be n0 or still referring to 0. And then it prints the sum out so then this line right nover here prints sum. It says ok the sum nright.

Now is just let me do it in blue..

It s just 0. And it printed it out then it goes back nto the for loop and says look are nthere any more elements that i can assign i to well sure there are there s all of these that nwe have to go through so then it makes i the next nelement in the sequence. So now. I is going to be the nnext element in the sequence or another way to think nabout.

It is in the i bucket. If you want to view nthem as buckets. I is now set equal to 1 and now we run this again and we say ok sum is 0. Plus.

I i is 1. So sum is now going nto refer to 1 or another way to nthink about it is sum is now going to refer to 1. And when you print nsum it ll now print. 1.

Then the for loop. Nwill. Say. Alright are there any more elements nin.

This list right over here to assign i to well sure. There are there s all of these others nso let me keep going so i can now be equal. Nto or can refer to 2. So if you think of it nas a bucket.

I is now 2 or you could say i nis now referring to 2. And you could ignore nthese right over here. And we go through nthe loop. Again sum is now 1 1.

Plus. I i is now 2 1. Plus. 2 is 3.

So sum is now going to be 3 nthis time through the loop. So sum is now referring to 3. You print sum you get 3 and you keep going all the way as we go through nthe loop. The loop will still keep ngoing as there s more things that i can refer to we can keep incrementing.

I to nthese other values over here and eventually i nis going to be 9. We re going to evaluate this and the for loop nsays hey is there anything left in this nlist that i can be and you re like no. There isn t nanything left in this list. And then the loop nis going to be done.

And we ll break out to it and the program will stop and that s exactly nwhat we saw happening. ” ..

Thank you for watching all the articles on the topic **For Loops in Python**. All shares of bluevelvetrestaurant.com are very good. We hope you are satisfied with the article. For any questions, please leave a comment below. Hopefully you guys support our website even more.

description:

tags: