Now, if youve ever used any other programming language before, you might think that these concepts are pretty straightforward. But let me tell ya, my friend in Python, things get a little interesting.
First up, we have our trusty old for loop. In most languages, the for statement is used to iterate over an arithmetic progression of numbers (like in Pascal) or give you control over both the iteration step and halting condition (as C). But not in Python! Oh no, here were talking about sequences lists, strings, tuples anything that can be indexed.
So let’s say you have a list of words:
# Create a list of words
words = ['cat', 'window', 'defenestrate']
# Loop through each word in the list
for word in words:
# Print the word
print(word)
# Output:
# cat
# window
# defenestrate
# The purpose of this script is to iterate through a list of words and print each word.
# The variable 'words' is a list of strings, which can be indexed and iterated over.
# The for loop allows us to loop through each word in the list and perform a specific action, in this case, printing the word.
# The variable 'word' is used to store each word as we iterate through the list.
# The print() function is used to display the word on the screen.
And you want to measure the length of each word in your list. You might think, “I’ll just use a for loop and iterate over the indices!” But hold on there, partner that wont work!In Python, we don’t need no stinkin’ indexing! Instead, we can simply write:
# This script is used to measure the length of each word in a list.
# First, we define a list of words to work with.
words = ["apple", "banana", "orange", "strawberry"]
# Next, we use a for loop to iterate through each word in the list.
for word in words:
# Within the loop, we print the word and its corresponding length using the len() function.
print(word, len(word))
# Output:
# apple 5
# banana 6
# orange 6
# strawberry 10
# The for loop iterates through each word in the list and assigns it to the variable "word".
# The len() function is used to determine the length of each word and print it alongside the word itself.
We get the length of each word printed out to us. Its like magic! But wait what if you want to skip over certain elements? That’s where our second beloved feature comes in handy: the try statement (with a little help from its friend, the continue statement).
Let’s say we have another list of numbers and we only want to print out the odd ones. We could use an if statement inside our for loop like this:
# This script is used to print out the odd numbers in a list of numbers.
# First, we define a list of numbers.
numbers = [1, 2, 3, 4, 5]
# Next, we use a for loop to iterate through each number in the list.
for num in numbers:
# We use the modulus operator to check if the number is even or odd.
if num % 2 == 0:
# If the number is even, we use the continue statement to skip to the next iteration.
continue # This statement allows us to skip to the next iteration without executing the code below.
# If the number is odd, we print it out.
print(num) # This statement prints out the odd number.
# Output: 1, 3, 5
But wait what if we want to do something else instead of printing out the odd numbers? Maybe we just want to count them. No problem! We can use a counter variable and increment it inside our for loop like this:
# Define a list of numbers
numbers = [1, 2, 3, 4, 5]
# Initialize a counter variable to keep track of odd numbers
odd_count = 0
# Loop through each number in the list
for num in numbers:
# Check if the number is even
if num % 2 == 0:
# If it is, skip to the next iteration
continue
# If the number is odd, increment the counter variable by 1
odd_count += 1
# Print the number of odd numbers in the list
print(f"There are {odd_count} odd numbers.")
Python’s for loop and try statement two features that might seem simple at first, but can lead to some pretty interesting results. So go ahead, my friend give them a spin and see what kind of magic you can create!