Python range() Function


Python is a modern, general-purpose, and high-level programming language that comes with powerful features. Python has many built-in modules to support diverse operations. The range() function is a built-in function used to perform specific tasks or actions for a definite number of times. In other words, the range() function is used to perform a task iteratively. This function is used in conjunction with the for loop and the while loop.

The range() function allows you to generate a sequence of numbers in a specified range. The range() function contains the following two constructor definitions:

    1. range(stop)
    2. range(start,stop,step)

The range(stop) definition of the range() function takes the stop value as an argument. The stop value defines the ending points of the sequence. The range() function stops returning the sequence of numbers when it reaches the stop value. The range() function also takes the start, stop, and step value as an argument.

The parameters are defined as follows:

  • start:  the start of the sequence of numbers.
  • stop: the ending point of the sequence of numbers.
  • step: the increment value in the sequence.

For example, to print a sequence of numbers from 1 to 20 with each value incremented by two, the start value is 1, the stop value is 21, and the step value is 2. In this case, the stop value is required, while the start and step values are optional.

Let us look at some examples that use the Python range() function.

Using range() with for Loop

The range() function is mostly used in combination with the for and while loops. In the given program, we have defined only the stop value.

We will print a sequence of numbers using the for loop and the range() function.

#defining the range of number until 20
number = range(21)
for i in number:
    #printing the sequence of numbers
     print("The number is:", i)

Output

Next, we will define the range() function with the start, stop, and step values and print the sequence of numbers using the for loop.

#defining the range of numbers from 1 to 21
#the step value is 2
number = range(1,21,2)
for i in number:
    #printing the sequence of numbers
    print("The number is:", i)

Output

Creating a List of Numbers Using range() Function

By definition, the range() produces a sequence of numbers in a given range. We can create a list of numbers using the range() function in a straightforward way. Let us create multiple lists using the range() function.

#creating a list with range
#using the stop value
#stop value is 20
my_list = range(20)
#printing the list
print(list(my_list))
#using the start and stop value
#start value is 7 and stop value is 20
my_list = range(7,20)
#printing the list
print(list(my_list))
#using start, stop, and step value
#start value is 7, stop value is 20, and step value is 3
my_list = range(7,20,3)
#printing the list
print(list(my_list))

Output

Next, we will print a list of the first 10 odd numbers and even numbers using the range() function.

#generating first 10 odd numbers
my_list = range(1,20,2)
#printing the list
print("The list of odd numbers:",list(my_list))
#generating first 10 even numbers
my_list = range(2,21,2)
#printing the list
print("The list of even numbers:" ,list(my_list))

Output

Creating a Tuple of Numbers Using the range() Function

As for a list, we can also create a tuple of numbers using the range() function.

#creating a tuple with range
#using stop value
#stop value is 30
my_tuple = range(30)
#printing the tuple
print(tuple(my_tuple))
#using the start and stop value
#start value is 7 and stop value is 30
my_tuple = range(7,30)
#printing the tuple
print(tuple(my_tuple))
#using the start, stop, and step value
#start value is 7, stop value is 30, and step value is 3
my_tuple = range(7,30,3)
#printing the tuple
print(tuple(my_tuple))

Output

Using the range() Function to Iterate to the Length of an Object

The range() function can also be used to perform an iteration. The len() function determines the length of the sequence object, i.e. the list, tuples, and length value are passed to the range() function as an argument. Let us see an example of this.

#creating a list
my_list = [1,2,3,4,5,6,7,8,9]
for x in range (len(my_list)):
    print(my_list[x])

Output

Conclusion

This article explained the Python range() function through various examples. Python is a modern and high-level programming language that provides many built-in modules and functions to perform specific tasks. The range() function is one such built-in function of Python. The range() function is used to perform a task for a certain number of times. It can also be used to generate numbers in a sequence. To learn more about Python and the various features you can use with this language, check out linuxhint.com.



from Linux Hint https://ift.tt/2HxoGQ7

Post a Comment

0 Comments