Python Learning (Part 5)
In this article we will get to know what is an Array in Python, elements of Arrays and what’s the difference between an array and a list
Python Arrays :
Arrays are used to store multiple values in one single variable.
It nothing but a data-type collection which means it can hold more than one value at a time.
To use an array in Python we can use import array as shown below
Output :
We can also use for in loop to loop through all elements of an array as shown below
Output:
Also there are various elements of arrays :
Get Arjun Sharma’s stories in your inbox
Join Medium for free to get updates from this writer.
append(): adds an element in the end of the list.
Output:
pop():
Output:
remove(): Removes the element for which the value has been specified.
Output:
sort(): sorts the list in ascending order.
Output:
reverse(): Reverses the order of the list.
Output:
index(): only returns the first occurrence of the specified value.
Output:
clear(): Removes all the elements from the list.
Output:
The difference between array and list :
In List we can have different data-types of data but in Array we can have set of just a single data-type as shown below
Here I have used list and in that we have integer, string and float data-type of data present in list1 as shown above
Output:
Now when we use same data for array it will show me error.
Output:
Being new to Python, suggesting to start with the below practical project video learning: -







