For Loops

laptop-with-program-code

Crafting scripts properly in Python has always been a challenge for me. However, I think I’m getting a better understanding of how they work and how to make them work properly.

One of the most common loops one may find in Python, especially in data analysis, is the For Loop.

Using the For Loop, one can iterate over a sequence of objects in either a list, dictionary, or array.

In the example below, we first create a dictionary containing the 2020 records of the American League West baseball teams.

Then, we create a For Loop that prints the records in the dictionary.

If we only want to print the Teams in this dictionary (in other words, the keys rather than the values) we could use the keys() function.

Finally, we can use the For Loop to create simple sentences using the dictionary alwesttwenty.

Leave a comment

Your email address will not be published. Required fields are marked *