How To Take Array Input In Python
Last Updated : Jan 1, 2023
IN - Python
In this tutorial we will show you the solution of how to take array input in python, an array is basically a data structure which can hold multiple values simultaneously at a time.
This can be considered as a collection of ordered elements which are of the same type within the code.
Since array can initialize only single data type values, so all the entered values should be of same data type within the code.
In this tutorial, we’re going through the various methods of taking input as an array in python programming.
Step By Step Guide On How To Take Array Input In Python :-
Method 1 -
This is the most simplest and basic method for the task,
x = list() y = input("Enter the number of values you want in array:" ) print ('Enter numbers for array ') for i in range(int(y)): n = input("Number :") x.append(int(n)) print ('stored array numbers are ',x)
- First, we created a variable ‘x’ by using the list() function in order to get input as the list within the code - x = list()
- Then again we created a variable ‘y’ and took input through that by using input function - y = input("" )
- Here, we used loop as using i and set the range of variables along with the number of elements we’ve given in the variable x. for i in range(int(y)):
- Now, the numbers we took as input is stored in variable ‘n’ n = input("Number :")
- At the end, we used append() function to add the elements stored in n variable within the list ‘x’. x.append(int(n))
- At last, we printed all the elements by referring to the elements of x as it has returned all the values of n variable.
- Now, we got our result.
Method 2 -
In this method, we will create a list based array, and then we’re going to add values to it by taking input of size of array and it’s values within the code.
array_1 =  n = int(input('size of array you want? \n')) for i in range(n): temp = int(input('Enter numbers to add in the array 1 \n')) array_1.append(temp) print(array_1)
- First, we created an blank array named ‘array_1’
- Now, we created a variable named ‘n’ and took input of the size of array user want in the code. n = int(input(''))
- We set the range of n variable as per the number given by user in the variable n. range(n):
- Here, we again created a variable named ‘temp’ to take input of the values user want in the array.
- Here, we just added the entered values of temp variable, and added it to the array_1 by using append function. array_1.append(temp)
- The last step, we just printed the values of ‘array_1’ by using the print() function and got the required result.
In this tutorial, we’ve gone through two method of taking input as an array in python programming by some easy steps of code.
Usage of array is plays a very important role in python, so understand it deeply and start practicing.
I hope this tutorial on how to take array input in python helps you.