FOR statements are used to iterate over a series of items in container variables (lists, tuples, data frames) and strings.
In this section, you will learn how to:
Employ iteration using FOR statements (FOR Statements)
FOR Code Examples
Copy the following code on the left in your IDE to execute the process shown in the image on the right.
For Loop In this example, the code iterates through the notable_tulane_alumni variable and prints the length of each name. The "name" variable acts as a proxy for each individual name to be passed through the for loop.
for name in notable_tulane_alumni: print (len(name))
For Loop with a Function
This examples shows a similar process, but instead of printing out each name length, the code stores each value in a new list variable (name_count). This variable is printed at the end of the code. The .append method is used to update the new list variable.