range() Function Of Python🐍

We already have discussed the Built-In Functions and User-Defined Functions in Python. Now, it’s time for one more vital function which is range().
 

range()

 
This is an in-built function of Python. This function is used for listing the integer in a sequence as well as for loop iteration. This function returns the range object. range() functions take one to three arguments as in the form of parameters.
 
Syntax and Examples
 
Syntax
 
range (start, stop[, step])
 
start: it is the starting number of the integer list. It is the lower limit of the list. Almost  always it starts with 0 otherwise we have to specify.
stop: it is the upper limit value of the list. It is not part of the output. 
step: it is the difference between each number in the output. Almost  always it is 1 as the default, otherwise, we have to specify. 
 
Example
range() Function Of Python
 
In this image, there is a total of 9 integers in the sequence. We got integers from 0 to 8 because range() doesn’t include the last (stop) number in the output. In jupyter notebook let's check how it will works,
 
range() Function Of Python
 

range() function with arguments/parameters

 
range() function can be declared with 3 parameters.
 
One argument
 
Example:1 
 
range() Function Of Python
 
In this example, start is 0, step=1 and only stop argument is passed to range(). 
 
Example:2
 
range() Function Of Python
 
In this example, there is quite a small difference in the sequences. range(-4) will return an empty set because there is no integer from the right. range() internally traverses to the right.
 
range() Function Of Python
 
Two arguments
 
Start and Stop arguments are passed to the range() function. By default, step argument is set to the value 1. 
 
Syntax
 
range(start,stop) 
 
Example 3
range() Function Of Python
 
All examples return the specific range from the integer sequence which traverses from 0 to the right. In the last example, there are two arguments: Start=3 and stop=7. step is 1 as default value.
 
Three arguments
 
Syntax
 
range(start,stop,interval)
 
Here, the interval is nothing but the step which has some values apart from 1. 
 
Example
range() Function Of Python
In this example, In[24] there is an odd number defined by step=2, In[25] it has the difference of 3, In[26] again range is defined by 2 numbers differences and in the last example, In[27] step=1.5 which is float value so Python returns an error message. In case of the step, value is 0 then again it will generate an error message.
 

Rules for range()

  • All the arguments: start, stop and step are integer only. Non-integer or other data types are not allowed to be used in range() function. 
  • All these three arguments might be positive or negative. 
  • range() function includes only integer numbers in its output.
  • step value is 0- zero then it will give an error message. By default, it is 1 or more than 1. 

Iteration with for loop


As we have learned that for loop is used to execute some code or statement in an iteration-repetitive form, with a fixed number of times.
 
Example
range() Function Of Python
 
In this example range is 9 with the multiplication of this loop 9 times. 
 

Patterns in Python

 
With using Nested For loops we can create pattern programming for numbers, asterisks *, Pyramids, and diamond shapes.
 
range() Function Of Python
 
This image will give us the basic idea for the pattern programming and how to start with it.
 
To generate patterns there are some basic structures which must be followed by the user,
  • There is nested for loop is used whereas the parent loop defines the rows and child loop defines the columns of the pattern.
  • print() function is used to print the pattern in pyramid, Dimond, asterisk, and number form.
  • The child loop depends on the value of the parent loop.
  •  add a new line after each row with using: print("\n")
The following are some examples that will help us to generates patterns with range() function and for loop.
 
Example 1
 
range() Function Of Python
 
In this example, we can see there is the code for half pyramid pattern.
 
for i and for j are nested for loops.
 
input() function is used to take the number of rows that we inserted as 6 which will be stored in i and iteration implemented with nested j for loop, where i is incremented by 1 up to a range of the value 6 rows.
 
print("\r") is for enter, otherwise use print("\n") for the enter with one row.
 
Example 2
range() Function Of Python
 
This is the example of a number pattern with range(7). In the outer for loop start=1,stop is the value of ending number which is 7. Inner for loop executes the code of iteration until 7 and print the rows and columns in the form of triangle shape with the addition of 1 in each row.
 
Example 3
 
Let's print characters in this pattern,
 
range() Function Of Python
 
ASCII values are used to print characters with chr() function for range() functions.
 
In this example range is 7 where alphabets and letters are printed in 7 rows. The ASCII value 65 which will give us the value of A as the start argument stored in asciiNumber_start variable.
 
chr() functions is used to print characters in range() function. 
 
ASCII 65 to 90 is capital A to Z remaining 91 and 92 are [ and \ respectively.
 
Note
More examples are attached in the zip file: range_func_Python.zip, for the different patterns with range() function.
 
Reference Link
  • https://data-flair.training/blogs/python-range-function/
  • https://pynative.com/print-pattern-python-examples/

Summary

 
In this article, we learned about the range() functions with different patterns of the structures. There are main other usages like arguments in range() function and iteration in for loop with range() function discussed with detailed examples and images. range() function is specifically used with integers but here we have learned to use this function with characters. We will look forward to Numpy where we use this range() function with the float arguments.


Similar Articles