In this tutorial, you'll learn how to use the for loop in Python along with the versatile range() function. These tools are fundamental for iterating over sequences like lists and strings, controlling iteration steps, and tracking indices. Understanding these concepts is crucial for writing efficient and readable code.
The for loop is a control flow statement that allows you to execute a block of code repeatedly for each item in a sequence (like a list or string). The range() function generates a sequence of numbers, which can be used to control the number of iterations in a for loop. Together, they provide powerful ways to handle repetitive tasks and process data.
You can use a for loop to iterate over each element in a list. Here's a simple example:
1fruits = ["apple", "banana", "cherry"]23for fruit in fruits:4print(fruit)
apple banana cherry
In this example, the for loop goes through each item in the fruits list and prints it.
Strings are sequences of characters, so you can iterate over them as well:
1greeting = "Hello"23for char in greeting:4print(char)
H e l l o
Here, the loop iterates over each character in the string "Hello" and prints it.
The range() function is used to generate a sequence of numbers. It can take one, two, or three arguments:
range(stop): Generates numbers from 0 up to, but not including, stop.range(start, stop): Generates numbers from start up to, but not including, stop.range(start, stop, step): Generates numbers from start up to, but not including, stop, incrementing by step.Here's a basic example using range():
1for i in range(5):2print(i)
0 1 2 3 4
This loop prints numbers from 0 to 4.
You can specify both the start and stop values:
1for i in range(2, 7):2print(i)
2 3 4 5 6
This loop prints numbers from 2 to 6.
You can also specify a step value:
1for i in range(0, 10, 2):2print(i)
0 2 4 6 8
This loop prints even numbers from 0 to 8.
Sometimes you need both the index and the value of each item in a sequence. The enumerate() function can help with this:
1fruits = ["apple", "banana", "cherry"]23for index, fruit in enumerate(fruits):4print(f"Index {index}: {fruit}")
Index 0: apple Index 1: banana Index 2: cherry
In this example, enumerate() returns both the index and the value of each item in the list.
Let's create a practical example that combines these concepts. We'll write a program to print the squares of numbers from 1 to 5:
1for i in range(1, 6):2square = i * i3print(f"The square of {i} is {square}")
The square of 1 is 1 The square of 2 is 4 The square of 3 is 9 The square of 4 is 16 The square of 5 is 25
This program uses a for loop with range() to iterate over numbers from 1 to 5, calculates their squares, and prints the results.
By mastering these concepts, you'll be able to write more efficient and readable code that handles repetitive tasks effectively.
In the next tutorial, we'll explore the while loop, which allows for more flexible control over iterations compared to the for loop. This will give you even more tools to handle various programming scenarios. Stay tuned!