For Loops
A for
loop is designed to iterate over a sequence of elements, executing a block of code for each element in the sequence. This construct excels at handling scenarios where you need to process multiple items, such as elements in a list, characters in a string, or numbers within a range.
Let's first break down the syntax:
numbers = [1, 2, 3, 4, 5]
total = 0
for num in numbers:
total += num
print(f"The sum of numbers is: {total}")
There are a few moving pieces here.
numbers: A list that can be iterated over. Anything that can be iterated over can be used in a for
loop.
total: Stores the final result. Not always necessary.
num: A variable name represents the current item in the iterable that can be anything you want, as long as it adheres to variable naming requirements. You could write for appleorangepear in numbers:
and it would still iterate over the numbers list.
total += num: Each time the loop iterates, it adds the current value of num
to total
.
A very simplified way of representing of what is happening in the loop would be:
- Add current num to numbers. (0 + 1 = 1)
- Are there still items in numbers?
- If yes, move on to next value.
- Add current num to numbers. (1 + 2 = 3)
- Are there still items in numbers?
- If yes, move on to next value.
- Add current num to numbers. (3 + 3 = 6)
- Are there still items in numbers?
- If yes, move on to next value.
- Repeat, until entire iterable is iterated over.
Code Examples
Example 1: Iterating Through a List of Names
names = ["Alice", "Bob", "Charlie", "David"]
print("Greetings:")
for name in names:
print(f"Hello, {name}!")
Example 2: Calculating the Sum of Numbers
numbers = [1, 2, 3, 4, 5]
total = 0
for num in numbers:
total += num
print(f"The sum of numbers is: {total}")
Example 3: Printing Patterns
rows = 5
for i in range(1, rows + 1):
print("*" * i)
Example 4: Iterating Through a String
sentence = "Python is amazing!"
for char in sentence:
if char != " ":
print(char)