Built in Methods in Python.

BUILT IN METHOD:

append()

append() method add a list item in the end of list.

books=['java','python','DBMS','Ds'] print(books[2:4]) books.append('os') print(books)

output:

['DBMS', 'Ds'] ['java', 'python', 'DBMS', 'Ds', 'os']

Count()

Count() return the numbers of time x apper in list.

m1=[1,2,3,1,5,6,1] print(m1) print(m1.count(1))

output:

[1, 2, 3, 1, 5, 6, 1] 3

Clear()

It will remove all the item from list.

m1=[1,2,3,4] print(m1) m1.clear() print(m1)

output:

[1,2,3,4] []

Copy()

This method is used to return a same copy of the list.

l1=['a','b','c'] print(l1) l2=l1.copy() print(l2)

output:

['a','b','c'] ['a','b','c']

extend(object x)

append all element of the list l2 to the list l1 or vice-versa.

l1=[1,2,3] print(l1) l2=[4,5,6] print(l2) l1.extend(l2) print(l1)

output:

[1,2,3] [4,5,6] [1,2,3,4,5,6]

index(object x)

Return the index of the first occurance of the element x from the list.

l1=[1,2,3,1,4] print(l1.index(1))

output:

0

pop()

Remove the element from the given position. The parameter is optional if it is not specify then it remove the last element from the list

l1=[1,2,3,4,5] l1.pop() print(l1)

output:

[1,2,3,4]

Remove (object x)

Remove the first occurance of element x from the list .

Ebooks = ['java','c','c++','DS'] Ebooks.remove('c++') print(Ebooks)

output:

['java','c','Ds']

Insert (int x,object c)

Insert() method add a item in list to given index.

books=['java','c','c++','Ds'] books.insert(2,'Android') print(books)

output:

['java','c','Android','c++','Ds']

Reverse()

it reverse the element of the list.

l1=[1,2,3,4] l1.reverse() print(l1)

output:

[4,3,2,1]

Sort()

These method is used to sort the element in the list.

l1=[10,15,11,18] l1.sort() print(l1)

output:

[10,11,15,18]

Comments

Popular posts from this blog

Introduction to Python.

Decision Making Statement.