List Manipulation| In Python List Datatype

 

NCERT CLASS XI

            LIST MANIPULATION  


#Now we will move forward to learn about the creation of a list programs.
To make list programs we will learn here about list that how we write a list and how we find length and some shortcut function to make a program easy.


Q.What is List?

List is write in the closed brackets [] .
It is used to write marks of all students in the class. But you can make using list ot make more programs.

For that you must have to learn some of the list to make your program very easy to write.
So Lets Start.
if you have given any list like [2,4,6,3,78]
If anyone say ehat is the length of the list so you have to count only how many elements in the list so there are 5 elements so ots length is 5.
And the index number start from 0 to length of the list -1
It means index value of 2 is 0
index value of 78 is 4.

Q.What are the operators of the list?


There are only limited operators first

1)Membership operators -in and not in 

It checks whether your written value is in the list or not.

Testcases1:
Input

list=[5,56,837,87,9]   
print(5 in list)                            




OUTPUT:
>>>True

Testcases2:
Input

list=[7,87,98,9,8]
print(7 not in list)


OUTPUT:
>>>False


2)Addition Operator 


Testcases1:

Input

lst1=[9,9,8]
lst2=[6,8]
print(lst1+lst2)                            





OUTPUT:
>>>[9, 9, 8, 6, 8]



3)Replication Operator 
it replicate the element of the list how much time  you want


Testcases1:

Input

lst=[9,87]
print(lst*2)                        #we can write in another way lst=lst*2 or lst*=2


OUTPUT:
>>>[9,87,9,87]

NOTE:These operators is come in the exam to find its output and nothing else

Now we will learn about functions that we can use in the program related to the list.

These functions you have write in small etters you never have to write any letter in capital letter.

 

1.append() function-this function is used to add one element in the list. 

Let us understand by example:

Testcases1:

 

L=[4, 5]

 

Output:

[4, 5,6]

 

By programming method:

L=[56, 4]

print(L.append.(6)) 

 

Output:

[56, 4,6]

 

2.extend() function -This function is used to add more than  one elements in the list at the last .

Let us understand by example:

 Testcases1:

l=[1,2,3]

 

Output:

[1,2,3,4,5]

 

Understand using program:

 

l=[1,2,3]

print(l.extend([4,5]))

 

Output:

[1,2,3,4,5]

 

 

3.remove()-This function is used to remove any element from the list using index number or directly putting that number.

 

Testcases1:

L=[1,23,4,6]

remove([1])

 

Output:

[1,4,6]

 

Testcases2:

 

L=[1,2,3,5,6,7,8,9]

remove(1)

 

Output:

[2,3,5,6,7,8,9]

 



#Correct Way
l=[1,2,3,4]
print(l.remove([0]))


l=[1,2,3,4]
print(l.remove(1))

                 
Boths output come same i.e [2,3,4]

4.clear() function -It is used to remove all elements from the list and give output empty list.


Testcases1:
L=[1.2.3.4.5]
L.clear()

Output:
[]

Testcases2:

Input:
l=[222,34,22,5,6,3,5]
l.clear()

Output:

[]

# 1 Correct Way

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

#2
L=[23,4,5,7,5,56,12]
print(l.clear())


5.max() function-It is used to find maximum value from the list.Let us understand using example


Testcases1:

l=[1,2,3,4]
max(l)

Output:
4

Testcases2:

L=[12,35,3,5,335,9008]

max(L)

Output:
9008

#1 Correct Way
L=[23,4,5,7,5,56,12]
print(max(L))

Output:
56

#2
L=[23,43,456,67,12]
print(max(L))

Output:
456
 
6.min() function -This function is used to find minimu value from the list.

Testcases1:
l=[1,2,3,4,5]
min(l)

Output:
1

Testcases2:

L=[2,4,5,5,3,566]
min(L)

Output:
2

#1 Correct way
L=[23,43,1,45,2,23]
print(min(L))


Ouput:
1


#2
L=[2,3,5,3,2,6,3]
print(min(L))

Output:
2















No comments:

Powered by Blogger.