An array is a collection of items of same type stored at contiguous memory locations. To access the elements, you only need to know the memory address of the first item of an array which is also known as base address. You can access all other items or traverse in an array by simply adding an offset to this base address. Python lists can also be treated as arrays but the lists can store multiple data items of different datatypes. This article is about how to create an array of numbers 1 to N in Python. If you want to learn more about Python Programming, Visit Python Programming Tutorials.
There are different methods to create an array of Numbers 1 to N in python. In this article, we’ll discuss the following.
- CREATION OF AN ARRAY OF NUMBERS 1 TO N USING A RANGE() FUNCTION IN PYTHON.
- CREATE AN ARRAY USING THE USER-DEFINED FUNCTION
- CREATING AN ARRAY USING A NUMPY-ARANGE() FUNCTION
- CREATE AN ARRAY USING PYTHON MODULE ARRAY
In the first three methods, we’ll see how lists can be treated as arrays. Python has a module called array which is used to work only with specific data values. The last method discusses how to create an array using this module. Lets discuss all these methods in detail.
CREATING AN ARRAY USING THE RANGE() FUNCTION
As discussed previously, python lists can be treated as arrays. To create an array of a certain range we can use the range() function as it specifies the range of the list and then typecast the range() by using the list command as shown in the code below. We can set the range of the list from 1 to N and N should be any integer number.
CODE:
#Creation of an array using Range() Function
list = list(range(1,8))
print(list)
[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 append the number in the List_array starting from 0 up to the maximum length ‘n’ as shown below.
CODE:
def List_function(n):
list_array = []
for i in range(n+1):
list_array.append(i)
return(list_array)
print(List_function(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
cREATING AN ARRAY USING nUMPY.ARANGE() FUNCTION
The numpy library provides an arrange() function which takes two parameters as an integers and generate the numbers starting from the first parameter upto 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)
[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 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 an input. The first one is datatype of an array such as ‘i’ for integer. All other datatypes are given in the 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))
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 query regarding this topic or any other topic related to python programming language, let us know in the comments or contact us.