Skip to main content

ARRAYS IN PYTHON







An array in python is a data structure that can hold more than one value at a time just like a list but their is a slight different in both of them which is discussed below. arrays can hold values of only single data type e.g integers or strings. All values in an array are indexed hence have their own unique address by which they can be called at any time. Now the indexing of array values in python always starts from 0, so if I have an array starting from 1 till 100 the first value of this array i.e 1 will have its index number as 0 and the array's last value i.e 100 will have an index number of 99.

to visualise it we can say:

array:     1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ,9 , .........., 100
         
array's     
index  :  0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , .........,  99

So now you can easily see how arrays are indexed in python so If using python code which is (discussed below), call out a specific index number it will give me the value stored at that specific index number. Another benefit of indexing is that if in an array values are repeating they will still have a unique different index number so that they are uniquely identified.

THE DIFFERENCE BETWEEN LISTS AND ARRAYS.

The question regarding array is that what is the main difference between array and lists since their functionality is same. the answer is pretty simple and straight-forward that is: arrays can store only single data type value, one array can be specified to only contain one data type values be it integers or strings etc ,while list can store any data type value be it strings, integers or floats together in one list.

Its benefit? to put it simply if you want a number to be multiplied by many integers you put those integers into an array so that whole array can be multiplied whereas if you put those in a list and tried to multiply them, python will show an error.

HOW TO CREATE ARRAYS 

to create an array in python we first need to import the array module which can be done by typing:

import array  or  import array as arr  or  from array import *

a simple code to create an array would be:

import array

a = array.array('i',[1,2,3,4,5,6,7])

print(a)

ACCESSING ARRAYS

Values or elements in arrays are accessed using their unique address i.e their index number (discussed above). before I show you the code you need to know that their exists a negative indexing too just like the normal indexing starts from 0 and goes from left to right, the negative indexing starts from right with its first index number as -1 and goes to left increasing in its negative number. so if an array has 100 as its last vaue its index would be 99 but its negative index would be -1. and when accessing arrays both the index numbers either 99 or -1 can be used to call the value 100.
 
a code to access values in an array:

import array

a = array.array('i',[1,2,3,4,5,6,7])

print(a[2])

if you look closely there is minimal difference in the codes written above that's the benefit of python as it makes task simpler and quicker. As you can see when accessing a value from inside an array you simply type its index number with the array's variable in the print function. In this print function we are printing out the a of 2 that is the value stored in the array a at index place 2 and the output will be 3. 

THE LENGTH OF AN ARRAY

To find the length of an array as in how many values does an array holds you can simply use the python len function. what you will do is that you will create another variable let's say length and in that you will use the len function to count the number of values in that array and then print that variable length.

a code to find length of an array:

import array

a = array.array('i',[1,2,3,4,5,6,7])

length = len(a)

print(length)

CHANGING VALUES IN ARRAY

To change a certain value in array you can simply take that particular value's index number as variable and assign a new value to it.

a code to change a certain value:

import array

a = array.array('i',[1,2,3,4,5,6,7])

a[2] = 5

print(a)

In this code i wanted to change the value stored at index 2 so I took it as my variable a[2] and assigned it the value 5 and when I printed it the array had value 5 on the index number 2.

ADDING VALUES IN AN ARRAY

You can easily add values in an array by using python's in-built append function. you just take the array's variable and append the new value into it.

a code appending a value into an array:

import array

a = array.array('i',[1,2,3,4,5,6,7])

a.append(8)

print(a)

So in this code I just appended value 8 into the array variable and when we print this array out we will surely see the value 8 right after 7.

REMOVING VALUES FROM AN ARRAY

To remove a value from an array thank to python we again use an in-built function called pop. yeah it is called pop in the sense to burst that value like a baloon so it doesnt exist anymore. 

a code removing a value from an array:

import array

a = array.array('i',[1,2,3,4,5,6,7])

a.pop(3)

print(a)

In this code the value stored at index number 3 is being popped out and when we print it we won't see the 3rd value that was 4.


So these were some basics of arrays in python. hope you all did learn something from it.






Comments

Popular posts from this blog

ARRAYS VS VARIABLES IN PYTHON

Beginners mostly prefer variables over arrays, usually because they dont know the real power of array and they end up wasting time storing bunch of items in tens of variables whereas they can use an array and store all those into one variable ` Why people use variables than array Most programmers when starting out learn how to create variables to store information in as a way to make their program more dynamic. However, when they start writing a program, they soon find themselves using lots of variables for the same datatype which results in a waste of time and memory, then they say to themselves, “There has to be a better way.” If they are smart enough and love to research and update their programming, they will turn to using arrays. Arrays are a complex data structure compared to variables but once the progammer gets the hang of it he will be using it all the time for storing same datatype items. (btw in this blog post: https://cyber1571.blogspot.com/2022/10/arrays-in-python.html...

HOW HAS THE CREATION OF CODE IMPACTED US AS HUMANS

The invention of coding has revolutionized the way we live and communicate. today almost everything around us is being run by a written code and thus has made our lives easier. look at the device you are reading this, the mobile phone where you get a notification, your washing machine, everything has a code that makes it function.  Coding, or programming, has had a significant impact on society and the way we live our lives. It has enabled the development of many technologies and tools that we use daily, such as computers, smartphones, and the internet. It has also brought in the idea of automation of processes leading to AI, and this has increased productivity and efficiency. It has hence contributed to the creation of new industries and job opportunities. interesting fields such as the internet of things, artificial intelligence, virtual reality, and 3d game development are created due to the advent of code. The Internet which came into existence due to coding has given humans li...