Data Type of Python
In Python there are two types of data types which are Mutable and Immutable. Mutable data type contains List and Dictionary data types. And Immutable data type contains String,Tuple and Sets data types.
Now we see what is Mutable and Immutable data type? The main object get changed permanently after applying methods of data type this data type is called as Mutable data type. And the main object will not be changed permanently after applying methods of data type this data type is called as Immutable data types.
Now we see data types one by one!
1) List - List is an ordered collection of mutable datatype. Lists are hashable, iterable. List contain any kind of data. List are declared in square braces[].
- Empty List-
L = [ ] - List with multiple elements -
L1 = [10, 15.10, 'Python', [10,20] ]
In the above list you can add any data type as shown above. In list L1 there is Integer, float,String,list data types. To add string data type you must have to declared in single or double quotes. - Hashable-
print (L1[2]) => 'Python'
In the above example it returns 3rd element of list. In the above example we entered index number 2 in the square braces. To know more about the example please refer blow table.0
1
2
3
10
15.10
'Python'
[10,20]
-4
-3
-2
-1
We will see another example
print(L1[0]) => 10
print(L1[0:3]) => [10, 15.10, 'Python' ]
Now in this example it will print from index number 0 to index number 2. It will return first three elements of list L1. - Iterable-
for i in L1:
print(i)
It will return => 10
=> 15.10
=> 'Python'
=> [10,20] - Membership-
print(10 in L1) => True
print(20 in L1) => False
print(20 not in L1) => True
- append(obj) - It append and object to the end of the list.
For example:-
L = [ 1, 2, 3 ]
L.append(4) => [ 1, 2, 3, 4]
L.append('Hello') => [ 1, 2, 3, 4, 'Hello' ] - clear():- It removes all elements from list.
For example:-
L=[ 1, 2, 3, 4, 'Hello' ]
L.clear() => []
When you apply clear method to list only elements are deleted from list memory allocation is not remove. - count(value):- It count number of occurrences of value in a list.
For example:-
L=[ 1, 2, 3, 4, 2, 3, 2, 1 ]
print(L.count(3)) => 2
Here the value 3 is occur twice in the list.
print(L.count(2)) => 3
Here the value 2 is occur three times in the list. - index(value,start,stop):- It return first index of given value. It raises value error if the value is not present. The start and stop is for slice notation.
For example:-
L=[ 1, 2, 3, 4, 2, 3, 2, 1 ]
print(L.index(3)) => 2
In this example the value 3 is repeat twice but index method return the index number of first occurrence.
print(L.index(3,3)) => 5
In this example it will find the value 3 from the index number 3 not from the index number 0.
print(L.index(1,2,5)) => valueError
In this example the value 1 is not found between index number 2 and index number 5 so it will return value error. - extend(iterable):- It extends the list by appending the elements from iterable. Each elements of an iterable is placed on individual indexes.
For example:-
L=[ 1, 2, 3 ]
L.extend([ 4, 5, 7 ]) => [ 1, 2, 3, 4, 5, 7 ]
L.extend( 'Hello' ) => [ 1, 2, 3, 4, 5, 7, 'H', 'e', 'l', 'l', 'o' ] - insert(index,obj):- It insert an object at given index. The original value at that index is right shifted.
For example:-
L=[ 10, 20, 40, 60 ]
L.insert( 2, 30 ) => [ 10, 20, 30, 40, 60 ]
In the above example we add 30 to the index number two but in original list have value 40 at the index number 2. When we perform insert method the original value of that index is shifted to right side. - pop(index=-1):- It remove and return the item at given index. Default index is last index. It raises index error if index is out of range or list is empty.
For example:-
L=[ 10, 20, 30, 40, 50, 60 ]
print(L.pop()) => 60
print(L) => [ 10, 20, 30, 40, 50 ]
print(L.pop(-2)) => 40
print(L) =>[ 10, 20, 30, 50 ] - remove(value):- It removes the first occurrence of given value. It raises value error if the value is not present.
For example:-
L=[ 10, 10, 20, 30, 20, 40, 30 ]
L.remove(20) => print(L) => [ 10, 10, 30, 20, 40, 30] - reverse():- It reverse a list by in place.
For example:-
L = [ 'Hi', 2, 5, 3, 9, 'Bye', 'World', 4, 2, 1 ]
L.reverse() => [ 1, 2, 4, 'World', 'Bye', 9, 3, 5, 2, 'Hi' ] - sort(key=none, reverse=false):- It sort the list in ascending order default. If we have multiple data types in a list we use key.
For example:-
L=[ 'Hi', 2, 5, 3, 9, 'Bye', 'World', 4, 2, 1 ]
L.sort(key=str, reverse=True) => ['World', 'Hi', 'Bye', 9, 5, 4, 3, 2, 2, 1]
L1=[ 10, 10, 20, 30, 20, 40, 30 ]
L1.sort() => [ 10, 10, 20, 20, 30, 30, 40 ]
Comments
Post a Comment