Print Through A List
This is the syntax for a super simple Python program that takes a list and prints out each out item in the list using a for loop.
For 'pizzazz' I used f-string syntax to print more than just the item in the list, but rather a phrase "printing number __!". The curly brackets hold the place of the variable to be used in that place of the printed text.
# list of numbers
numbers = ['1','2','3','4','5']
# for loop that goes through each item in list
for number in numbers:
print(f"printing number {number}!")
Output
printing number 1!
printing number 2!
printing number 3!
printing number 4!
printing number 5!
I love how For Loops in Python lets you create a new variable in the command. It helps make the code that much easier to understand when I can say to myself "For each number in the list of numbers..." and have that be able to be used in my code.
Subscribe to my newsletter
Read articles from Grayson Akerly directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Grayson Akerly
Grayson Akerly
I am a Python hobbyist, husband, girl-dad, and budget-home-chef. I first found out about python in college at Texas Tech University where I ended up with a major in mathematics. We were required to take a programing for mathematics class that used Python. It was strange to use at first, but over the years I have kept coming back to Python for it's relative ease of use, being able to quickly pick up where I left off. Most recently I have been finding ways to 'automate the boring stuff' (very much a reference to the book) at my work and life. Using the mindset of 'there has to be a better way', I am challenging myself to find ways to let my computer help me make my life better.