Introduction
The range()
function in Python is used to generate a sequence of numbers. It is often used with the for
loop to execute a block of code a specific number of times. The range()
function is highly versatile and allows for efficient iteration over a sequence of numbers.
Syntax
The range()
function has three different ways to be called:
range(stop)
: Generates numbers from0
tostop - 1
.range(start, stop)
: Generates numbers fromstart
tostop - 1
.range(start, stop, step)
: Generates numbers fromstart
tostop - 1
, with a specified step.
Example
for i in range(5):
print(i)
Output:
0
1
2
3
4
Using range(stop)
Generates numbers from 0
to stop - 1
.
Example
for i in range(5):
print(i)
Output:
0
1
2
3
4
Using range(start, stop)
Generates numbers from start
to stop - 1
.
Example
for i in range(2, 7):
print(i)
Output:
2
3
4
5
6
Using range(start, stop, step)
Generates numbers from start
to stop - 1
, with a specified step.
Example
for i in range(1, 10, 2):
print(i)
Output:
1
3
5
7
9
Using break in a for Loop with range()
The break
statement is used to exit the loop prematurely, regardless of the loop condition.
Example
for i in range(10):
if i == 5:
break
print(i)
Output:
0
1
2
3
4
Using continue in a for Loop with range()
The continue
statement skips the current iteration and proceeds to the next iteration of the loop.
Example
for i in range(10):
if i % 2 == 0:
continue
print(i)
Output:
1
3
5
7
9
Iterating Over a String Using range()
A for
loop can be used to iterate over the indices of a string using the range()
function.
Example
text = "Hello"
for i in range(len(text)):
print(text[i])
Output:
H
e
l
l
o
Simple Programs Using range() in for Loop
Program 1: Sum of First N Natural Numbers
This program calculates the sum of the first n
natural numbers.
n = 10
sum = 0
for i in range(1, n + 1):
sum += i
print(f"The sum of the first {n} natural numbers is {sum}")
Output:
The sum of the first 10 natural numbers is 55
Program 2: Factorial of a Number
This program calculates the factorial of a given number.
num = 5
factorial = 1
for i in range(1, num + 1):
factorial *= i
print(f"The factorial of {num} is {factorial}")
Output:
The factorial of 5 is 120
Program 3: Print Each Character of a String
This program prints each character of a given string using the range()
function.
text = "Hello, World!"
for i in range(len(text)):
print(text[i])
Output:
H
e
l
l
o
,
W
o
r
l
d
!
Program 4: Iterate Over a List of Numbers and Print Their Squares
This program iterates over a list of numbers and prints their squares.
numbers = [1, 2, 3, 4, 5]
for i in range(len(numbers)):
print(f"The square of {numbers[i]} is {numbers[i]**2}")
Output:
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
Program 5: Print Even Numbers from 1 to 10
This program prints all the even numbers from 1 to 10 using the range()
function.
for i in range(1, 11):
if i % 2 == 0:
print(f"{i} is an even number")
Output:
2 is an even number
4 is an even number
6 is an even number
8 is an even number
10 is an even number
Conclusion
The range()
function is used for generating sequences of numbers in Python, making it especially useful for for
loops. By understanding how to use range()
with different parameters (stop
, start, stop
, and start, stop, step
), you can create efficient and flexible loops to iterate over a wide variety of sequences. The provided examples and programs demonstrate practical applications of the range()
function in solving common programming tasks.