Instructional Guide - How to Make a 'for' Loop in Python

by AishaMuj in Workshop > Science

392 Views, 1 Favorites, 0 Comments

Instructional Guide - How to Make a 'for' Loop in Python

Screenshot (255).png

Image credit: Sturtz, John. Python "for" Loops (Define Iteration). 2019, Real Python.

This guide will walk you through the steps of a very basic 'for' loop in Python. "For" loops are used to help a coder iterate through something - for instance, if you have a list of numbers and would like to print a worded statement with each number in the list, a "for" loop would make the process extremely efficient. "For" loops go through each element in a group of data, whether it's a list, set, dictionary, or other approved data groups in a coding language. "For" loops are a basic building block of coding - while this guide is shown specifically in Python, the concept of it is useful for every other coding language. This procedure will be demonstrated on a Windows PC, and may look different depending on the device you're using.

Open Your IDLE.

Screenshot (254).png

An IDLE (Integrated Development Learning Environment) is an application that will help you code in Python. If you don't have it downloaded, go to Python.org/downloads/ and choose the right download for your computer (Windows, Mac, Linux, etc.). On an updated Windows Device, your IDLE application will look like this. Click on it to open the IDLE Shell, where you'll be making your 'for' loop.

Defining Your List

Screenshot (256).png

'For' loops iterate through something. In this example, we'll be making a 'for' loop that iterates through a list. As shown above, I named my list 'list1'. We'll iterate through each number in the list using the 'for' loop. Lists use square brackets. When you're making a variable name for your list, make sure to not literally use the word 'list', because it has a built-in meaning for the IDLE, and can cause problems when you're running your code. Note that although I put numbers in my list, you're also free to use other acceptable Python text types.

Creating the 'for' Loop

Screenshot (257).png

Python 'for' loops start with the word 'for', then any letter or word you want (in this example I used 'i'), followed by "in" and what you'll be iterating through. In this case I'll be iterating through my 'list1' list. Follow your 'for' loop declaration with a colon. Although the IDLE usually indents for you after you press enter, double-check that the next line after your 'for' loop declaration is tab indented.

Iterating

Screenshot (259).png

Iterating means you'll need to decide what you want to do with each object in the list. I want my 'for' loop to print out each number with "is in list1" after it. Because my list has numbers, and I want to add "strings", which are two different types in Python, I need to be sure that I convert each number to a string to make sure it prints properly. this can be done this using the str() function. You can use the '+' symbol to combine the string-converted integer with the phrase. Click the enter key when finished to see your output.

A Finished 'For' Loop!

Once you've clicked the "enter", your loop is complete. Make sure to observe your output for any mistakes, and modify your code accordingly. In only a few lines of code, a 'for' loop enables coders to have any amounts of output, making it a fundamental concept of coding.