Class 9 List Methods Tuple
Class 9 List Methods Tuple
insertion orede to preseved and duplicate are allowed then we should go for list
data type.
ex:-
l = [10,56.87,7+9j,True,'string',[1,2,3,4],10]
>>> l
[10, 56.87, (7+9j), True, 'string', [1, 2, 3, 4]]
>>>
list_methods:-
1.append:-used for appending and adding element to list.It is used to add element
to the last position of list.
ex:-
>>> l = [10,20,30,40]
>>> l.append(50)
>>> l
[10, 20, 30, 40, 50]
ex:-
>>> l = [10,20,40,50]
>>> l.insert(2,30)
>>> l
[10, 20, 30, 40, 50]
>>> l1 = [10,20,30,40,50]
>>> l2 = [60,70,80,90,100]
>>> l1.extend(l2)
>>> l1
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
ex:-
>>> l = [10,20,30,40,50,60,70]
>>> l.index(60)
5
ex:-
>>> l = [10,20,30,40,50,60]
>>> l.remove(20)
>>> l
[10, 30, 40, 50, 60]
ex:-
>>> l = [1,9,7,8,0,6,8,3,7,11,99,77,90]
>>> l.sort()
>>> l
[0, 1, 3, 6, 7, 7, 8, 8, 9, 11, 77, 90, 99]
decending order.
>>> l = [1,9,7,8,0,6,8,3,7,11,99,77,90]
>>> l.sort(reverse= True)
>>> l
[99, 90, 77, 11, 9, 8, 8, 7, 7, 6, 3, 1, 0]
>>>
Tuple:-
A tuple is collection of python objects and it's separated by commas. In someways a
tuple is similar to a list in terms of indexing,but tuple immutable.
immutable:- immutable means we can not change or modify the element,we can just
watch the data,but cann't change it.
ex:-
>> t = (1,2,3)
>>> type(t)
<class 'tuple'>