# For loops | BUS 104 – Python Exam

Still feeling nervous for your exam? Check out the Python Cram Kit for practice questions that emulate the difficulty that you'll face on your exam. Each question includes a thorough step-by-step explanation where I walk you through the entire problem solving process!

## For loops explained

Let's say you teach at ABC Elementary School and it's the first day of classes. You've got a small class of 4 students, and you need to call attendance on the first day.

As you look on your attendance sheet, you see the following 4 names:

You read through each name, and gradually you're able to identify each student.

When you read through the list of names on the attendance sheet and called on each student, you were essentially accomplishing the primary purpose of for statements:

For loops are primarily used to loop through a list, or array, of items and enact an action on each item.

## Coding an array

Before we learn how to code for loops, we need to learn how to write a list of names in Python.

Make sure to open a Trinket file to follow-along! (How to create a Trinket file?)

We will utilize arrays to accomplish this.

What's an array?

An array is a list of items enclosed in square brackets.

names = ["Peter", "Lila", "Alex", "Bella"]

These items are identified with indexes, starting with the first item having an index of 0 and adding 1 to it each time.

names = ["Peter" (0), "Lila" (1), "Alex" (2), "Bella" (3)]

These indexes are used to reference specific items in the array.

For example, what if we wanted to print the 2nd item in the names, "Lila"? We'd type the following...

print(names[1])

names = ["Peter", "Lila", "Alex", "Bella"]

Remember, the 1st item in an array corresponds to the index value of 0. That means that the 2nd item has an index value of 1, not 2.

To reference specific items in arrays, you'll type the array name and then the index value in square brackets next to it. Here's a template:

array[index]

## Coding a for loop

Alright, now that we've explored how to create the list of names on the attendance sheet in Python, let's figure out how to loop through each name and print it.

Like we did in when learning while loops, I'm going to show you the answer first, and then we're going to dissect how it works.

names = ["Peter", "Lila", "Alex", "Bella"]

for x in names:
print(x)

Now, let's dive into each part of the for loop.

names = ["Peter", "Lila", "Alex", "Bella"]

for x in names:
print(x)

Here, we're declaring a loop variable that'll be referenced within our loop. Notice how inside our loop, we reference the loop variable?

names = ["Peter", "Lila", "Alex", "Bella"]

for x in names:
print(x)

Here in lies the purpose of loop variables...

The loop variable references each item in our array we're looping through.

Just to make the concept of the loop variable hit home, the first time we run through the loop, the value of x will be "Peter".

names = ["Peter", "Lila", "Alex", "Bella"]

for x in names:
print(x)

Then, the next time we run through the loop it'll have the value of "Lila".

names = ["Peter", "Lila", "Alex", "Bella"]

for x in names:
print(x)

See how as we loop, the loop variable is assuming the value of each item in our array of names?

### Declare the array to loop through

names = ["Peter", "Lila", "Alex", "Bella"]

for x in names:
print(x)

We've declared our loop variable, now we must declare what items that loop variable is going to represent. These items are stored in an array.

This part is pretty self explanatory, it's just important to not forget to include the "in" keyword between your loop variable and the array.

names = ["Peter", "Lila", "Alex", "Bella"]

for x in names:
print(x)

### Code to be executed within the loop

names = ["Peter", "Lila", "Alex", "Bella"]

for x in names:
print(x)

With each iteration of the loop, we want to print the item the loop variable represents from the names array.

So, the first run of the loop, we'll print the first item in names.

names = ["Peter", "Lila", "Alex", "Bella"]

Then, we'll print the second item.

names = ["Peter", "Lila", "Alex", "Bella"]

Then, the third item.

names = ["Peter", "Lila", "Alex", "Bella"]

Lastly, the fourth item.

names = ["Peter", "Lila", "Alex", "Bella"]

After this, the loop stops running.

If you want to test out the above code on your own, type the below code into your Trinket file. It should look like this now:

When you run your program by clicking the "Run" button above your code, you should get the following output:

## Revisiting the for loop template with arrays

Now that we've successfully broken down each part of the for loop, here's the template of it for you to use in the future:

for loop variable in array:
code to be executed referencing loop variable

## Practice problem

In a new Trinket file, create a program that utilizes a for loop to print each of the following fruits.

apples, oranges, pears.

These fruits should be stored in an array.

The expected output from your program can be seen in the slideshow below:

HINT: Forget how to print content in the output? Check out the print() function template on the last page of your Python Cheat Sheet for some help!

fruits = ["apples", "oranges", "pears"]

for x in fruits:
print(x)

Note: if you named your fruits variable something else. It's alright, as long as the functionality of the program is the same, that's all that matters!

## Python Follow-along Guide

It's no secret you retain info better when you write it down. That's why I created the Python Follow-along Guide for you!

As you come upon key concepts highlighted in yellow, like this sentence here, you can fill-in-the-blanks on your Python Follow-along Guide so that you remember all the important stuff for later!

You can obtain your Python Follow-along Guide for FREE by entering your email below!

Now that we've got the different types of loops covered, let's circle back to all the different types of data you'll be dealing with in Python. Click below to continue!

### Concepts

Each exam concept broken down with relatable situations to your life!

### Practice Problems

Include step-by-step explanations through each part of the problem!

"...I was blown away with the videos and the ability to explain what is going on at a simple level. Highly recommend!"

Jack Whelan
March 8, 2021

Rating: 5 out of 5.