Introduction to Python
Component of Python
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)
Our Array is been formed here .
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)
Our Array is been formed here .
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)
We get 'Sony' here because it is accessing that value which is having the index number '0' .
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)
We get the length of our array.
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)
Here we get all elements in a sequential manner.
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)
Here we just added our 'Iphone' element in the array.
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)
Here we just remove our 'Samsung' element in the array.
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)
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