The range() function in Python is a built-in function that generates a sequence of numbers. It can be used to create a sequence of integers that can be iterated over in a loop or used to create a list. The syntax for the range() function is as follows:
____________________________________________________________
range(start, stop[, step])
____________________________________________________________
start is the starting value of the sequence. If omitted, it defaults to 0.
stop is the ending value of the sequence (exclusive). This argument is required.
step is the increment between values in the sequence. If omitted, it defaults to 1.
The range() function returns an object that represents the sequence of numbers. To use it as a list, you can convert it using the list() function. Here are some examples:
____________________________________________________________
Example 1: create a sequence of numbers from 0 to 9
for i in range(10):
print(i)
Example 2: create a sequence of even numbers from 0 to 8
for i in range(0, 10, 2):
print(i)
Example 3: create a list of squares of numbers from 1 to 5
squares = [i**2 for i in range(1, 6)]
print(squares)
____________________________________________________________
OUTPUT
____________________________________________________________
0
1
2
3
4
5
6
7
8
9
0
2
4
6
8
[1, 4, 9, 16, 25]
____________________________________________________________
In the first example, range(10) generates a sequence of numbers from 0 to 9, which is iterated over in the for loop. In the second example, range(0, 10, 2) generates a sequence of even numbers from 0 to 8 with a step of 2. In the third example, a list of squares of numbers from 1 to 5 is created using a list comprehension with range(1, 6) to generate the sequence of numbers.
On this page of the site you can watch the video online 018 Range function in Python with a duration of hours minute second in good quality, which was uploaded by the user Harsh Ramoliya 01 April 2023, share the link with friends and acquaintances, this video has already been watched 3 times on youtube and it was liked by 0 viewers. Enjoy your viewing!