7554425D-0054-440D-B95E-D2ABC13D62CD Created with sketchtool.
Your Saved Info Packages

View your saved Course or Program Packages containing pricing and detailed curriculum.

Speak with a Learning Advisor.

Have any questions? We'll call you.

Fill out the form below and a Learning Advisor will reach out at a time convenient for you.

Please pick a valid date and time between 9 AM and 8 PM eastern (Monday to Friday)

By clicking "Book a call," you accept our Terms and will also receive exclusive offers and updates about new courses, workshops and events.

Python Tutorial

Python For Loops

Ready to start your career in Data? Find out more about BrainStation's Python Course

Loops are very useful programming principles for executing certain logic or a block of code multiple times for a collection of items like those found in string, list, tuple and dictionary data types.

One such looping technique is using a for loop. A for loop is the most widely used looping technique for iterating over a list, dictionary, string or a tuple. It repeats a certain logic over and over again for different items unless there are no more items left or an exit-condition is met. An exit-condition is usually something that will stop the loop.

Let’s look at a small flow chart that explains how for loop works:

For loop in Python has following syntax:

for value in list:
for loop logic for value

Let’s look at how to use for loop in code snippet below:

Python Range Function

Sometimes while using a loop, you are not necessarily looping through a predefined list or string. Instead, you may wish to loop through a predefined range of values or numbers. range() function comes really handy when you want to loop for a certain range of numbers. Based on the parameters passed to the range() function, it will automatically generate those values, which can be then used inside of a for loop.

range() function is defined as follows in two different variations:

range(start, stop[, step])

where, start defines the starting number from which range should start. stop defines the number at which the range should stop. The range will stop at the value defined by stop minus 1. step defines how the numbers should step up or increment in the range.

Let’s take a look at how range() function might come handy inside a for loop syntax:

Python Enumerate Function

When using loops, it is often handy for developers to remember the index or count of the item that has already been looped through. enumerate() function in Python allows you to loop through an iterable while also providing the index or count of each item being looped through.

enumerate() function is of the following form:

enumerate(iterable, start=0)

Let’s take a look at the code snippet below to understand how to use enumerate() function with a for loop:

Set Password

You already have an account with BrainStation, but you still need to set up a password.