fbpx

For loops | BUS 104 – Python Exam

To view the VIDEO for this concept, you must purchase Lifetime Access, Python Cram Kit or BUS 104 Cram Kit Bundle. Already purchased? Click here to log in.

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.

  • Calling Peter's name for attendance

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.

Here's how an array representing the attendance sheet would look:

PAID CONTENT

This is the end of the preview. To unlock the rest, get the Lifetime Access, Python Cram Kit or BUS 104 Cram Kit Bundle.

Already purchased? Click here to log in.

Leave a Comment