Hello, Explorer! Today, we will revisit Python loops, essential tools that simplify repetitive tasks. Think of loops like a marathon of TV series episodes. We will venture into the Python looping universe and acquire hands-on experience by applying loops to collections like lists
and strings
.
Have you ever experienced repeating a favorite song on a loop? That's what loops are all about in programming, too. For example, you can print greetings for a list of friends using a For loop
:
Python1friends = ['Alice', 'Bob', 'Charlie', 'Daniel'] 2# `friend` is the loop variable, taking each name in the `friends` list 3for friend in friends: 4 # for each `friend`, this line is executed 5 print(f"Hello, {friend}! Nice to meet you.") 6""" 7Prints: 8Hello, Alice! Nice to meet you. 9Hello, Bob! Nice to meet you. 10Hello, Charlie! Nice to meet you. 11Hello, Daniel! Nice to meet you. 12"""
Loops allow us to automate repetitive sequences efficiently.
In Python, a for
loop works with any sequence, like lists
or strings
. Let's print a range of numbers using a for
loop:
Python1# `num` runs through each number in the range of 5 2for num in range(5): 3 # This line will print numbers from 0 to 4 4 print(num)
Each loop iteration updates the variable (num
) to the next sequence value before executing the code block.
While loops
execute code continuously until a condition becomes false. Here's a simple example:
Python1num = 0 2# The loop keeps running until num is greater than or equal to 5 3while num < 5: 4 print(num) 5 # increase num by 1 each iteration 6 num += 1
As you can see, before each iteration, Python checks the condition (num < 5
). If it's true, the code block runs; otherwise, Python exits the loop.
Python loops can directly work with collections, making it easy to handle elements of a list
or string
.
For instance, consider looping over a list:
Python1# list of fruits 2fruits = ['apple', 'banana', 'cherry'] 3# `fruit` stands for each fruit in the `fruits` list 4for fruit in fruits: 5 print(fruit) # prints each fruit
And, looping over a string:
Python1word = 'hello' 2# `letter` is each individual character in the `word` 3for letter in word: 4 print(letter) # prints each letter in 'hello'
Loops are integral to programming. They are used in various programming applications, such as summing numbers in a list or parsing text.
Python1# List of numbers 2numbers = [1, 2, 3, 4, 5] 3total = 0 4# `num` is each number in `numbers` 5for num in numbers: 6 total += num # add each number in the list 7print(total) # prints the total sum
Python1# string 2text = 'hello' 3vowel_count = 0 4# `letter` is each character in `text` 5for letter in text: 6 # If a vowel letter is found, increment the count 7 if letter in 'aeiou': 8 vowel_count += 1 9print(vowel_count) # prints the count of vowels
Well done on getting a grasp of Python loops! We brushed up on looping fundamentals, Python's For
and While
loops, and how to loop over collections. Now, it's time for some essential practice exercises to cement your learning. The more problems you solve, the more adept you become. Onward, we continue on the programming voyage!