Sitemap

Python Learning (Part 5)

3 min readOct 25, 2019

--

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 :

Press enter or click to view image in full size

We can also use for in loop to loop through all elements of an array as shown below

Output:

Press enter or click to view image in full size

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:

Press enter or click to view image in full size

pop():

Output:

Press enter or click to view image in full size

remove(): Removes the element for which the value has been specified.

Press enter or click to view image in full size

Output:

Press enter or click to view image in full size

sort(): sorts the list in ascending order.

Output:

Press enter or click to view image in full size

reverse(): Reverses the order of the list.

Output:

Press enter or click to view image in full size

index(): only returns the first occurrence of the specified value.

Output:

Press enter or click to view image in full size

clear(): Removes all the elements from the list.

Output:

Press enter or click to view image in full size

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:

Press enter or click to view image in full size

Now when we use same data for array it will show me error.

Output:

Press enter or click to view image in full size

Being new to Python, suggesting to start with the below practical project video learning: -

--

--