× About Us How it works Pricing Student Archive Subjects Blog Contact Us

Enrich your knowledge with our informative blogs

What are arrays in python? 

In general terms, Python array is a collection of elements having same datatype or typecodes. It can hold more than one value with the same name. 

For example: If you want to marks of three students we generally use three variables, but what if the number ranges beyond capacity say 300. 

The answer to this is an array! 

You can easily traverse, append, remove and do all sorts of operations on a similar collection of data.  

As such Python does not support array, it supports lists. We need to import the array module for using arrays. 

Let’s check some terms used in python array. 

  • Element: item stored in an array are called element. 
  • Index: The location of the element in the array is called index. The first element of the python array has an index 0. 

Now, Let’s have a look how array is initialized or declared in  a python program  

from array import * 

nameof thearray = array(datatype, [Initializers]) 

Datatype stands for what type of value the array will hold such as integer, float etc.  

There are mainly 7 types of datatypes or typecodes. 

Datatype  Type/Value  Size 
b  Signed Integer  1 Byte 
B  Unsigned Integer  1 Byte 
c  Character   1 Byte 
i  Signed integer  2 Bytes 
l  Unsigned Integer  2 Bytes 
f  Floating point integer  4 Bytes 
d  Floating point integer  8 Bytes 

 

Let’s create an array and display all the elements in it: 

from array import * 

array_1 = array(‘l’, [100,150,200,250,300]) 

for y in array_1: 

 print(y) 

Here is the output  

100 

150 

200 

250 

300 

Accessing the elements in an Array 

Every array element can be accessed by their corresponding index number.  

For example, in the above array,  

array_1[0] will give you 100. 

 array_1[1] will give you 150. 

array_1[2] will give you 200. 

And so on.  

Insert() 

This function is used to insert elements into the array at a specific location, be it beginning, end or anyway in the middle.  

Syntax  

Array_name.insert(index_intended, value) 

To add an element 125 at 2nd position, we will use the following program. 

from array import * 

array_1 = array(‘l’, [100,150,200,250,300]) 

array_1.insert (1, 175); 

for y in array_1: 

 print(y) 

Here is the output  

100 

175 

150 

200 

250 

300 

Here, the number of elements in an array has also increased by 1. 

remove() function 

To delete a value from the array, you can use the remove() function. After deletion, all the elements would be reorganized again.  

Syntax 

arrayname.remove (element) 

The interpreter will find the value and delete it from the array and shifts the other values up. 

In case, the value is not found, it’ll give an error.    

Search Operation 

By using the built-in index() function/method you can easily find the index of an element. 

Syntax  

array_name.index (element) 

This will return the place or index of the element. If not found, the compiler will give an error.  

Let’s check some other methods that are used to operate on arrays. 

1). append() 

This method is used to add element at the end of the array and increases the length by 1. 

Syntax: 

array_name.append(<new value>)  

2). count() 

This method counts and returns the number of elements having the given value.  

Syntax: 

array_name.count(<element value>)  

3). reverse() 

It reverses the list making the first element last and brings the last element on first place.  

Syntax: 

array_name.reverse() 

4). pop()  

Just like remove() method uses the element value, pop() function uses index to remove an element. 

Syntax: 

array_name.pop(<index>)  

5). len() 

This method/function calculates the length of the array or number of elements in the array. 

Syntax: 

len(array_name)  

 

Read More – Coding and Programing Questions

View More – Useful links for Your Child’s Development 

Unveil your gateway to a lucrative career!
Unveil your gateway to a lucrative career!

Unleash the power of true logic building with Real-time instructions and live coding exposure.

Book A Demo Class

Tel Guru
Tel Guru

Register For The Demo Class

[footer-form]