EME 210
Data Analytics for Energy Systems

Introduction to For Loops

PrintPrint

  Read It: For Loops

For loops can be used to perform calculations and store variables iteratively. In Python, there are several keywords that all for loops must have:

  1. for --- This command starts the for loop

  2. in  --- This command sets the number of iterations

  3. :   --- This command says that everything that follows is meant to be executed in the loop (until the indentation changes)

Below, we demonstrate the basics of for loops.

  Watch It: Video - Introduction to For Loops (07:15 minutes)

Click here for a transcript.

Hello and welcome back to the videos in this lesson. We're now going to move in to bootstrapping and how we can use bootstrapping to develop more samples that we can then use to develop confidence intervals. Because in the previous lectures, we had four samples and while we can use that to develop confidence intervals, we really want a lot more data in order to really ensure that our results are the most statistically sound that they can be. And that's where bootstrapping comes in. but before we get into bootstrapping I wanted to briefly introduce for loops, because that's how we actually conduct the bootstrapping is through leveraging for loops. So this video will just cover how we Implement for Loops in Python.

So we're back in Google Collab. We've got our libraries here that we're going to use throughout the bootstrapping lectures. But in this case, we're simply going to talk about for loops. And so, there are a few key words that we need to be aware of when working with for loops, the word for starts the loop the word in tells python how many iterations to complete, and the colon tells python that everything that comes after this is included in the loop. So to give a very simple example, we can say for I in range 0 to 10 colon. And so, everything after this will be executed in the loop. The I here is a placeholder that is just counting up the iterations for us. So it's saying I is what is counting up from 0 to 9. And again, remember that python does that weird thing where it excludes the last number. So we're really doing 0 to 10 minus 1. The key thing is to make sure that whatever you list in this opening statement of the loop is also considered in the body of the loop.

So here we want to print I. So we if we were to print J, it wouldn't work, you can see that we've got this red line underneath. It's telling us that there is no variable J and that's because we messed up our counter. We need to Define I, which is defined up here. We can also do operations. So we can print I equal to 2, or I, print I plus 2. And then we can keep going and as long as we maintain the same indent level, will execute everything. When we're done with the loop, we can backspace, I like to write a comment and let me know that I'm done with the loop. And then we can continue on with whatever we were doing. And so, if we run this we can see that it's printing I zero. Then it's printing I plus two, but then it goes back, and now it prints I. Now I is equal to one. I plus two. Now I is equal to two print and so forth until it ends and says this is the end of the loop. And so within these loops, once we get into bootstrapping we'll do a lot more intensive loops, but the basic structure will always remain the same. The next style of loop that I wanted to demonstrate was how we can nest loops. So this is where we add for loop inside of a for loop. It starts off with the same style. We'll say 0 to 4 this time. Which is actually three. And we'll say that we want to print I is and then print I. So here we're mixing text and value and then for each iteration of I, we want to also complete a series of values for J. We'll say 0 to 3.

And so now you can see that when I enter down I'm another indent here. So this is our first loop and now this is our second loop. So we can print J equals. So we are printing where J is equal to J. And now, in order to make sure that our loops are organized, we can go back one space and add a comment. And now if we want to do anything outside of the inner loop, but still in the outer loop we can add stuff there, so we can say this is the end of the inner loop. Then if we backspace again, add in a comment, and we can also print. So if we run this, we can see that it is it started where I equals zero, and then it gives J one through two. It iterates through all of J during the first iteration of I, and then it does our printed. And then it increases I by one and reiterates through all of J. And so forth until we reach the end. And so the purpose of these nested loops is when you have multiple things that you're iterating over, and you need to do them sort of in sequence. And so we'll use primarily this single version of the loop throughout the bootstrapping lectures, but just know that you can also do a lot of what we're doing with nested loops as well.

Credit: Eugene Morgan & Renee Obringer © Penn State is licensed under CC BY-NC-SA 4.0

  Try It: DataCamp - Apply Your Coding Skills

Try to implement a simple for loop below. You want to iterate the loop 5 times, each time printing the index number.


  Assess It: Check Your Knowledge

Knowledge Check 

 FAQ