How can I use Python's range function

How can I create a list with sequential numbers?


Say when you've had a number entry into Python and want to make a list of sequential numbers that you like

How could you do that



Reply:


In Python 3, you can use the built-in function as follows

Note 1: The Python 3.x function returns an object. If you want a list, you have to explicitly convert it to a list using the function as I showed it in the answer.

Note 2: We pass the number 9 to the function because the function generates numbers up to the specified number, but does not contain the number. So we give the actual number + 1.

Note 3: There is a small difference in functionality between Python 2 and 3. Please refer to this answer for more information.


Using the Python built-in range function:

Python 2

Python 3


Just to give you another example, while range (value) is by far the best way to do this, this might help you with something else later.



Note: - Of course, in Python-3x you have to use the Use the range function. It works to generate numbers when needed. The standard way to use the Range function to create a list of consecutive numbers

If you a function to generate a list of sequential numbers using the Range function want to create, pay attention to this code!

Good luck!



Also, depending on how you want to get the result, you can print each number in a for loop:

For example, if the user input was 7:

You can also delete the '+1' in the for loop and put it in the print statement, which will change it to 1 instead of 0.


You can use list comprehension for this problem as it is only solved in two lines. Code-

Many Thanks.

We use cookies and other tracking technologies to improve your browsing experience on our website, to show you personalized content and targeted ads, to analyze our website traffic, and to understand where our visitors are coming from.

By continuing, you consent to our use of cookies and other tracking technologies and affirm you're at least 16 years old or have consent from a parent or guardian.

You can read details in our Cookie policy and Privacy policy.