For-loop Basics

Gabriel Warner
1 min readJun 1, 2021

--

The for loop can be used to repeat a set of statements in a list, dictionary, tuple, set, or string.

to make a for loop:

  1. Identify your variable
  2. Identify where the variable is located
  3. What do you want to do to the variable
  4. for loop formula:

for _ in _: -> for variable in variable location

statement

In the example below a list of names is created. We want to print each name out of the list, we could print each individually but this can be tedious with large sets of data.

“Name” is the variable. This can be named anything you want as long as the same name is used throughout the loop.

“Names” is the name of the list the data is from. This tells the computer where to pull the variable from.

If you are throwing errors and can not figure out why these are some common errors:

Incorrect indentations

Forgot the ‘:’ in “for _ in _:”

Mixing strings and integers

--

--

No responses yet