Array in Python.


Array in Python.

In this post we are going to learn about Arrays in Python. Let's start..!

The very first thing you should know What is array ? 

Array : 

The array is a collection of multiple value of same type under a single variable. It simply means a all values in arrays should be of same type. Array is almost same as list or tuple etc. but the only difference between them is list or tuple can store values of multiple types but array can't store values of multiple types.
Array can stores multiple values but can't store values of multiple types.
Arrays are of specific type.

Now How to create an array ?

So arrays are not available in python by default. So we need to import array module. now What is module ? 

In Python we have so many modules and they are not available by default so we need to import it.

Module : 

Modules means a source code of function that we use to do some operations or to perform actions in Python. 
for ex. print() , input() etc. we use this function many times in our code. But How this functions were created ? so Developer of python Guido van Rossum created these functions and write the code for this functions. And to access this function we need to import it. some of them are available by default. But some need to import to use them.

To work with array or to use functions related to arrays in Python we need to import it. How to import array ? 

For this we use a import keyword; along with this we use one more keyword i.e. from

Syntax : from module_name import function_name 
( if you want use or import all functions in specified module you can use * . This * means import all function. )

So we will write 'from array import *' This will import all functions related to arrays.

After importing arrays Now it's time to create array. To create an array we use a special function that is array( ) . 
This function actually takes two parameters. First parameter is so important because the first parameter confirms that which type of values this array can store. It can store integer or float or character values. But How to specify the type ? 

For this we need to specify a typecode. now What is typecode ? 
Typecode is simply a code given to each type. You can see typecode of each type in table below.



The mostly use typecodes is 
1] 'u' : 
    This is a typecode of type character. Whenever we want to store character values then use 'u' typecode.

2] 'i' : 
    This is a typecode of type integer. Whenever we want to store integer values it can be positive or negative both are accepted, then use 'i' typecode.

3] 'I' : 
    This typecode is also of type integer. Whenever we want to store only positive integer values then use 'I' typecode. Negative values are not accepted in this typecode. 

4] 'f' : 
    This is a typecode of type float. Whenever we want to store float values ( numbers with decimal points ) then use 'I' typecode. 


The second parameter is an actual values of an array. To store values first we have to write a square bracket ( [ ] ) inside this square bracket we can give values that we want. To separate the values inside array we use comma (,). 
In Python we don't need to specify the length of an array. It means we can add more values anytime after creating an array. Like this we can remove existing value from array. We have so many functions to perform different operations on array. 

First create a simple array to demonstrate how we can create an array. 

for ex. Suppose you want to store a marks of 5 student. For this we will use an array. Suppose here all values are integers. So we will use a 'I' typecode to store the marks. We can also use 'i' typecode. But marks can't be in negative so we are using 'I' typecode here ( you can use 'i' or 'I' any of this ). 
First import the array and then create a variable ( of any name doesn't matter what name you are giving but it should follow the rules of variables ) ; After creating variables simply put a equal to sign ( = ) and write a function array( parameter1, parameter2 ). Here first parameter is our typecode that is 'I' in this case and second parameter is marks of 5 students [ 91, 92, 93, 94, 95 ] ( here I am giving any random values you can give any values that you want ). And our job is done. Now just print a array to check everything worked perfectly or not. 

# using array 
# importing array module
from array import *
# creating an array 
# and storing values
marks =  array('i', [ 9192939494])

#print a array
print(marks)


Output : 


array('i', [9192939494])







Now we will see some mostly or commonly used function of an array.

1} append() : 

This function is used to add new value in an array but in last position. It means the new value you are going to append it will take a last position in an array. 

Syntax : array_name.append( value )

for ex. You want to add marks of new 6th student. then use a append function. Simply write marks.append( marks_of_new_student )

# using array 
# importing array module
from array import *
# creating an array 
# and storing values
marks =  array('i', [ 9192939494])

#print a array
print(marks)
# adding new element 
# at last position
marks.append(90)

# printing updated array
print(marks)


Output : 


array('i', [9192939494])
array('i', [919293949490])






2} remove() : 

This function is used to remove an specified value of an array. It means we can remove any value that we want, from an array. 

Syntax : array_name.remove( existing_value )

for ex. You want to remove marks of student who's marks is 94. so you can use remove() function. Simply write marks.remove( 94 ).

# using array 
# importing array module
from array import *
# creating an array 
# and storing values
marks =  array('i', [ 9192939495])

#print a array
print(marks)
# removing all values
marks.remove94)

# printing updated array
print(marks)



Output :


array('i', [9192939495])
array('i', [91929395])






3} reverse() : 

This function is used to reverse a value of an array. It means it will totally reverse an array. This function does not take any parameter.

Syntax : array_name.reverse( )

for ex. You want to reverse the list of marks of student so you can use reverse() function. Simply write marks.reverse( ).

# using array 
# importing array module
from array import *
# creating an array 
# and storing values
marks =  array('i', [ 9192939495])

#print a array
print(marks)
# reversing all values
marks.reverse()

# printing updated array
print(marks)



Output : 


array('i', [9192939495])
array('i', [9594939291])






4} insert() : 

This function is used to insert a new value on specified position in an array. It means we can insert new value in array at any position. This function takes two parameter first the index number on which position we want to insert a new value; and second is an actual value. The parameter can be variable or actual value. 

Syntax : array_name.insert( index_number , value )

for ex. You want to add marks of new student at 3rd position in an array. so you can use insert() function. Simply write marks.insert( 2, 99 ). ( here we want new mark on 3rd position but I write two because indexing always start with 0 , index number of 3rd position is 2 ).

# using array 
# importing array module
from array import *
# creating an array 
# and storing values
marks =  array('i', [ 9192939495])

#print a array
print(marks)
# inserting new values
marks.insert(299)

# printing updated array
print(marks)



Output : 


array('i', [9192939495])
array('i', [919299939495])






This is all about arrays in Python. Create an array and try this all function.


If you have any doubts in this post then please ask me in comment section below. I will try to solve it.


If you like these post and you have learned something from this then share this post with your friends and family.

Keep learning always.....!!! 😇😇😇








Previous Post  Next Post