Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
19 views

Class 9 List Methods Tuple

Uploaded by

kunala
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Class 9 List Methods Tuple

Uploaded by

kunala
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

List Data type:- if we want to represent a group of values as a single entity where

insertion orede to preseved and duplicate are allowed then we should go for list
data type.

i.Insertion order are preseved.


ii.hetrogeneous object are allowed.
iii.duplicates are allowed.
iv.Growable in nature.
v.values should be enclosed within squre bracket.

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:-

'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove',


'reverse', 'sort'

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]

2.Insert:-Insert an element of specified position.

ex:-
>>> l = [10,20,40,50]
>>> l.insert(2,30)
>>> l
[10, 20, 30, 40, 50]

3.extend:-Add contents to list2 to the end of list 1.

>>> 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]

4.count:-calculate total occurrence of given element of list.


ex:-
>>> l = [1,2,3,1,1,1,2,3,3,1,2,2,3]
>>> len(l)
13
>>> l.count(1)
5
>>> l.count(2)
4
>>> l.count(3)
4
>>>
5. index:- Return the index of the first occurrance start and end index are not
nessary parameter.

ex:-
>>> l = [10,20,30,40,50,60,70]
>>> l.index(60)
5

6.clear:- clear the list and get a empty list.


ex:-
>>> l = [10,20,30,40,50,60,70,60]
>>> l.clear()
>>> l
[]

7.copy:- make a duplicate list using this method.


ex:-
>>> l = [10,20,30,40,50,60,70,60]
>>> s = l.copy()
>>> s
[10, 20, 30, 40, 50, 60, 70, 60]

8. pop():- delete the element from last position of the list.


ex:-
>>> l = [10,20,30,40,50,60]
>>> l.pop()
60
>>> l.pop(2)
30
>>> l
[10, 20, 40, 50]
>>>

9.remove():-Element to be deletedis mentrioned using list name and element.

ex:-
>>> l = [10,20,30,40,50,60]
>>> l.remove(20)
>>> l
[10, 30, 40, 50, 60]

10.sort:- converted accending order.

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]
>>>

11.reverse:- just reverse the list


>>> l = [1,9,7,8,0,6,8,3,7,11,99,77,90]
>>> l.reverse()
>>> l
[90, 77, 99, 11, 7, 3, 8, 6, 0, 8, 7, 9, 1]
>>>

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.

mutable:- Mutable means we can change or modify the element.

immutable:- immutable means we can not change or modify the element,we can just
watch the data,but cann't change it.

Note:- we can make tuple enclosed with parethesis :- ()

ex:-
>> t = (1,2,3)
>>> type(t)
<class 'tuple'>

You might also like