Table of Contents
- Introduction
- How to create an array of 1 to 10 in Python?
- Using the range() function to create an array of 1 to 10 in Python.
- Using list comprehension along with the range() function to create an array of 1 to 10 in Python.
- Using a user-defined function to create an array of 1 to 10 in Python.
- Using the NumPy.arange() function to create an array of 1 to 10 in Python.
- Conclusion
In this article, we will see how to create an array of 1 to 10 in Python.
Introduction
Arrays are an essential part of any programming language and are widely used by programmers in every field of coding. Python seems to have a different approach related to the use of arrays, as it does not directly support the use of arrays. However, in Python, we make use of either Lists or an array indirectly from the NumPy
library. This tutorial demonstrates the different ways available to create an array of 1 to 10 in Python.
Before we move on, let us understand what the basic terminologies involved in this article are.
What is a list in Python?
As you now know, there is no direct support available for arrays in Python. Lists are perfectly capable of acting as one-dimensional arrays, or for that matter, a list of lists can act as a multi-dimensional array, but there is always a limit to the number of dimensions in this method. Lists are capable of storing a number of items in one single variable, a quality that is similar to arrays.
What are NumPy
arrays in Python?
There is yet another indirect way to deal with arrays in Python, and that is to import the NumPy
library and utilize its functions. If dealing with multi-dimensional arrays, NumPy
arrays tend to be a better option than generic lists as they use a lot less memory and are more convenient in terms of the readability of the code.
How to create an array of 1 to 10 in Python?
Using the range()
function to create an array of 1 to 10 in Python.
The range()
function, as its name suggests is predominantly utilized to provide the range between which a sequence of numbers needs to be returned, this range being the arguments of this function. If not specified, the starting point of the range function defaults to 0
.
The range()
function’s syntax has been explained below for ease of understanding.
1 2 3 |
range(start, stop, step) |
The range()
function has the following parameters.
- start: It specifies the point of the beginning of the range that is to be specified. It defaults to
0
. - stop: It specifies the point of the end of the range that is to be specified. This parameter is exclusive in the range.
- step: An optional parameter, it indicates the step value that specifies the amount to be incremented. It defaults to
1
.
The following code uses the range()
function to create an array of 1 to 10 in Python 2.x versions.
1 2 3 4 5 |
# Python 2 code x = range(1,11) print x |
The above code provides the following output:
In the versions Python 2.x, we only need to directly utilize the range()
function to create a list with the specified values.
However, in the versions released after Python 3, we also need to utilize the list()
function along with the range()
function to successfully create the specified list.
The following code uses the range()
function to create an array of 1 to 10 in Python 3.x versions.
1 2 3 4 5 |
# Python 3 code x = list(range(1,11)) print (x) |
The above code provides the following code:
We should note that the stop parameter is exclusive in the range()
function, which means that it is not included in the list of the specified numbers. Here, we needed to create an array from 1 to 10, therefore, we have specified the stop
parameter in the range()
function as 11
.
Further reading:
Using list comprehension along with the range()
function to create an array of 1 to 10 in Python.
List comprehension is a concise way of creating lists as it reduces the lines of code needed to create a list. The range()
function, when combined with list comprehension is capable of achieving the same result as using the list()
function, but in a more easy-to-understand way.
The following code uses list comprehension along with the range()
function to create an array of 1 to 10 in Python.
1 2 3 4 |
a = [x for x in range(1,11)] print(a) |
The above code provides the following output:
Using a user-defined function to create an array of 1 to 10 in Python.
We can simply create a user-defined function with the help of the for
loop that manually increments each value and appends it to a given list.
The following code uses a user-defined function to create an array of 1 to 10 in Python.
1 2 3 4 5 6 7 8 |
def UDlist(n): x = [] for a in range(n+1): x.append(a) return(x) print(UDlist(10)) |
The above code provides the following output:
Using the NumPy.arange()
function to create an array of 1 to 10 in Python.
Till now, we have been taking a look at functions that create a list. This method, however, deals with the creation of NumPy
arrays.
The term NumPy
is an acronym for Numerical Python
and is a Python library that makes it possible to deal with arrays and matrices in Python. We can utilize the arange()
function from the NumPy
library to create an array with the desired specifications.
The following code uses the NumPy.arange()
function to create an array of 1 to 10 in Python.
1 2 3 4 5 |
import numpy as np x = np.arange(1,11) print(x) |
The above code provides the following output:
Conclusion
The article was focused on and provided the different ways available to create an array of 1 to 10 in Python. The article provides the different ways by which an array can be indirectly created and utilized in a way that is supported in Python. The article covers four ways in total. Out of these four, the first three ways are utilized to create a list that acts as an alternative to a simple array, while the fourth way creates an array using the popular NumPy
library.