Skip to Main Content

Intermediate Concepts in Python: Conditions and Iteration

This guide was created for those interested in increasing their Python programming knowledge. This guide is best for those who have some familiarity with basic concepts in Python.

What are FOR statements?

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.

notable_tulane_alumni = ["Victoria Reggie Kennedy"
                         "Paul Michael Glaser"
                         "Jerry Springer"]             
 Jupyter Notebook View

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.

notable_tulane_alumni = ["Victoria Reggie Kennedy"
                         "Paul Michael Glaser"
                         "Jerry Springer"]         Jupyter Notebook View

name_count = []

for name in notable_tulane_alumni:
    name_count.append(len(name))
    

print (name_count)

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.