×

Html & Html5

The World's best Software Development Study Portal

Python Array


Arrays is a collection of element of the same type. Python does not support built-in a arrays, but python list can be used instead .Arrays are used to multiple value in one single variables. Arrays of the numerical values are supported in Python by array modules.
let's have an example of creating an array containing mobile name:-


mobile = ["Nokia", "vivo", "Sony","Motorola"]

print(mobile)



Output of this Program


array1

Our Array is been formed here .





Creating an array


As array is a collection of same type in a special or in any variable.
let's have an example of creating an array containing mobile name :-


mobile = ["Nokia", "vivo", "Sony","Motorola"]

for x in mobile :

print(x)




Output of this Program



array2

Our Array is been formed here .




Access Element of array



We can access the element on array using the index of the element.and a index start with 0 not a (1).
Here is the example :-


mobile = ["Nokia", "vivo", "Sony","Motorola"]

x=mobile[2]

print(x)




Output of this Program


array3

We get 'Sony' here because it is accessing that value which is having the index number '0' .




The length of arrays


A len() method gives the length of the array(the number of the elements)
Here is the example :-


mobile = ["Nokia", "vivo", "Sony","Motorola"]

x=len(mobile)

print(x)




Output of this Program


array4

We get the length of our array.




Looping in array elements


We use for loop ,here for looping through all elements of arrays
Here is the example :-


mobile=["Sony","Nokia","vivo","Motorola"]

for x in mobile:

print(x)




Output of this Program


array5

Here we get all elements in a sequential manner.




Adding array element


We can use the append () method and add the element on an array.
Here is the example :-


mobile=["Sony","Nokia","Sumsung","Motorola"]

mobile.append("Iphone")

print(mobile)




Output of this Program



array6

Here we just added our 'Iphone' element in the array.




Removing arrays elements


1.

We can use the pop() method to remove the elements the an array.
Here is the example :-


mobile=["Sony","Nokia","Sumsung","Motorola"]

mobile.pop(2)

print(mobile)




Output of this Program



array7

Here we just remove our 'Samsung' element in the array.




2.

We also use the () remove() method to remove an element for an arrays.
Here is the example :-


mobile=["Sony","Nokia","vivo","Motorola"]

mobile.remove("Nokia")

print(mobile)




Output of this Program



array8

Here we just remove our 'Nokia' element in the array.




Some methods of arrays and their functionality



Python has set of built-in method that you can use on list/arrays.



Method Description
append() Add the element in the list
clear() Removes all the elements on the list
copy() Return copy on the list
count() Return the number and specific value return
extends() Add the element on the list to end of the last list
index() Return the index of first element with the specified value
insert() Insert the element specified position
pop () Remove the element specified position
remove() Removes first item with specific value
reverse() Reverse the order list
sort() Sorts the list

python training insitute| Best IT Training classes in Gurgaon | python training