An array is a collection of elements of the same data type stored in contiguous memory locations. In Python, lists are used to achieve array-like functionality, offering flexibility by accommodating different data types within a single list. This article explores various methods to create an array containing numbers from 1 to N in Python.
If you want to learn more about Python Programming, visit Python Programming Tutorials.
To create an array of numbers ranging from 1 to N in Python, there are several methods available. The most common approach is to use the range() function. Additionally, you can create such an array through custom functions, the numpy.arange() function, or the Python array module. These versatile methods offer different ways to achieve the same goal of creating an array containing numbers from 1 to N in Python.
In the first three methods, we will explore how Python lists can be effectively used as arrays. Python provides a specialized module called “array,” designed to work exclusively with specific data types. The last method is about creating arrays using this dedicated module. Let’s discuss these methods in detail..
Creating an array using the Range() Function
The range() function allows you to generate a sequence of numbers with a specified start and end point. The syntax of range() function is:
range(start, stop, step)
The range()
function takes up to three arguments: start
, stop
, and step
, and it returns a sequence of numbers starting from start
, up to but not including stop
, incrementing by step
(which defaults to 1 if not specified).
As discussed previously, python lists can be treated as arrays. To create an array with a specified range, use the range()
function and then cast the result using list()
. Set the range from 1 to N, where N is any integer.
The following example demonstrate how we can use range() function to create arrays of integers within a defined range.
#Creation of an array using Range() Function
list = list(range(1,8))
print(list)
Output:
[1, 2, 3, 4, 5, 6, 7]
Creating an array by a user-Defined Function
Another way is to create a function and pass the length of an array as a parameter to this function. In the example below, we have created a function by the name of List-Function. The function takes parameter ‘n’ which represents the length of the array. In this function, a for loop is used which treats n as the last index of the array and appends the number in the List_array starting from 0 up to the maximum length ‘n’ as shown below.
def List_function(n):
list_array = []
for i in range(n+1):
list_array.append(i)
return(list_array)
print(List_function(10))
Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Creating an array using NumPy.ARRANGE() Function
The NumPy library provides an arrange() function which takes two parameters as integers and generates the numbers starting from the first parameter up to the last parameter. Typecast the arange() function using the list command and an array is created.
import numpy as np
list_array = list(np.arange(1,13+1))
print(list_array)
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
numpy.arange() is used to create an array of large sizes.
Create an array using the Python Module Array
An array module of Python is used to create an array consisting of elements or items of same datatypes. The array module takes two arguments as input. The first one is the datatype of an array such as ‘i’ for integer. All other datatypes are given in this link. The second argument consists of the elements or items of an array.
def display(n,s):
print ("The array created consists of following items: ", end =" ")
for i in range (0, s):
print (n[i], end =" ")
print(" ")
import array as arr
# creating an array of integer datatype
arr1 = arr.array('i', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
#print array
display(arr1,len(arr1))
# creating an array of float datatype
arr2 = arr.array('d', [0.5, 5.21, 3.14])
#print array
display(arr2,len(arr2))
Output:
The array created consists of following items: 1 2 3 4 5 6 7 8 9 10
The array created consists of following items: 0.5 5.21 3.14
In the example above, we have created two arrays arr1 and arr2 of integers and floating numbers. The function display here is used to print the contents of an array created. It takes two arguments: an array ‘n’ and the size of array ‘s’ created.
There are different operations that can be carried out on arrays such as insertion, deletion, sorting arrays into ascending and descending order, etc. Try them on your own. If you have any queries regarding this topic or any other topic related to Python programming language, let us know in the comments or contact us.