How to declare an array in Python
In this article, we will see various methods for declaring array in python
Declaring array using list
arr = [1, 2, 3, 4, 5] for i in arr: print(i)
Output:
1 2 3 4 5
Declaring Array using array functions
import array arr = array.array('i', [1, 2, 3, 4]) for i in arr: print(i, end=" ")
Output:
1 2 3 4
Declaring Array using NumPy
NumPy has several functions to create arrays
import numpy as np # Creating an array from 0 to 9 arr = np.arange(10) print(arr) ar = np.array([1, 2, 3, 4, 5]) print(ar)
Output:
[0 1 2 3 4 5 6 7 8 9] [1 2 3 4 5]
Subscribe
Login
Please login to comment
0 Discussion