One of the fundamental concepts you need to understand is loops. Looping statements allow you to execute a block of code multiple times. The for
loop is a popular looping statement that you will frequently encounter in Python. In this article, we will explore how to use the for loop with the range function to perform repetitive tasks in Python.
What is the range()
Function?
The range()
function is a built-in Python function that generates a sequence of numbers. These numbers can be used for iteration purposes using a loop statement like for
. The range()
function takes three optional arguments - start, stop and step, which specify the starting point, ending point and the increment of the sequence respectively. If not specified, the default values for start
and step
are 0 and 1 respectively.
The Range Function
The range()
function in Python generates a sequence of numbers. You can specify the starting number, the ending number, and the step size. Here's the basic syntax:
range(start, stop[, step])
start
: The starting number of the sequence (inclusive). If not specified, it defaults to 0.stop
: The ending number of the sequence (exclusive). This argument is required.step
: The increment between each number in the sequence. If not specified, it defaults to 1.
Here's an example of how to use the range()
function:
>>> for x in range(5):
... print(x)
...
0
1
2
3
4
In this case, we generated a sequence of numbers from 0 to 4, which was used to iterate through a block of code five times.
Using range()
with for
Loops
Now that we've seen how the range()
function works, let's explore how to use it with a for
loop. The syntax for using range()
with a for
loop is as follows:
for variable in range(start, stop, step):
#code block to be executed
Let's look at some examples of how to use range()
with for
loops.
Example 1: Basic Usage
This example shows how to use the range()
function to generate a sequence of numbers:
for i in range(5):
print(i)
Output:
0
1
2
3
4
In this example, we've used range(5)
to create a sequence of numbers from 0 to 4 (inclusive). The for
loop then iterates over each number in the sequence and prints it to the console.
Example 2: Specifying a Starting Point
You can also specify a starting point for the sequence using the start
argument. Here's an example:
for i in range(1, 6):
print(i)
Output:
1
2
3
4
5
In this example, we've started the sequence at 1 instead of 0. The range()
function generates a sequence from 1 to 5 (inclusive), and the for
loop iterates over each number in the sequence and prints it to the console.
Example 3: Adding a Step Size
You can also specify a step size for the sequence using the step
argument. Here's an example:
for i in range(0, 10, 2):
print(i)
Output:
0
2
4
6
8
In this example, we've used range(0, 10, 2)
to create a sequence of even numbers from 0 to 8 (inclusive). The for
loop then iterates over each number in the sequence and prints it to the console.
Example 4: Printing the First Five Natural Numbers
for i in range(1, 6):
print(i)
Output:
1
2
3
4
5
In this example, we used the range()
function to generate a sequence of numbers from 1 to 5. The loop then iterates through this sequence and prints each number in turn.
Example 5: Summing the First Ten Positive Integers
sum = 0
for i in range(1, 11):
sum += i
print("The sum is:", sum)
Output:
The sum is: 55
In this example, we initialize a variable sum
to zero, and then use the range()
function to generate a sequence of numbers from 1 to 10. The loop then iterates through this sequence and adds each number to the sum
variable. Finally, we print the total sum.
Exercises
Here are some exercises you can try to practice using range()
with for
loops:
- Write a program to print the first ten even numbers.
- Write a program to calculate the factorial of 5.
- Write a program that asks the user for an integer input, and then prints all the odd numbers from 1 to that input.
Conclusion
In summary, the range()
function allows you to generate a sequence of numbers that can be used for iteration purposes using a for
loop. By understanding how to use range()
with for
loops, you can perform repetitive tasks efficiently in Python. With practice and perseverance, you'll soon become proficient in using loops in your code.