Assignment 1701572062

Download as pdf or txt
Download as pdf or txt
You are on page 1of 38

CHAPTER-11

LIST

UNIT - 2 (WEIGHTAGE: 45 Marks)


CHAPTER-11 SYLLABUS
Introduction, Indexing, Traversing using loops,

6 built-in functions len(), sum(), min(), max(), sorted(), list()

9 list methods/functions:
append(), extend(), insert(), pop(), remove(),
count(), index(),
reverse(), sort(),

list operations (concatenation, repetition, membership and slicing)

Nested lists

suggested programs:
● finding the maximum, minimum & mean of numeric values stored in a list,
● linear search on list of numbers
● counting the frequency of elements in a list.
REVIEW OF CHAPTER-10

we learnt: 8 Questions

&

25 Lab Programs (i.e Lab programs-76 TO 100)


Question-1 2 MARKS
Explain the following 5 list functions:
append(), insert(), extend()
pop(), remove(),
append() function insert() function
is used for adding one data at the end of the list.
is used for adding one data at the specified
Ex: index
L1=[5, 6, 10]
L1.append(30)
L1.insert(2, 23.15)
Syntax:
L1.insert (1, “COMPUTER”) list.insert(index , data)
L1.append([70, 80, 90])
print(L1)
The data can be any
o/p:
[5, 'COMPUTER', 6, 23.15, 10, 30, [70, 80, 90]]
ie: either
int/decimal/string/list/tuple/dictionary
Syntax: extend() function
list.append(data) is used for adding many datas at the end of the list.
The data can be any Syntax:
ie: either int/decimal/string/list/tuple/dictionary list.extend(sequence)
pop() function remove() function

● is used for deleting one data from the list. ● is used for deleting one data from the list.
● deletion is done based on the specified index ● deletion is done based on the given data
● Also, pop() function returns the deleted data

Ex:
Ex: L1=[5, 6, 10, 20, 30] o/p:
o/p:
L1=[5, 6, 10, 20, 30] 10 print(L1.remove(10)) None
print(L1.pop( 2 )) 30 print(L1.remove(20)) None
print(L1.pop( 3 )) 20 print(L1) [5, 6, 30]
print(L1.pop( )) [5, 6]
print(L1)

Syntax: Syntax:
list.pop(index =) list.remove(data)
By default, index value will be taken as -1
Question-2 2 MARKS
Explain the following 3 list functions:
count(), insert(), reverse()
reverse() function
Syntax:
list.reverse( ) reverses the datas in the list

Ex:
a=[10, 20, 5, “cs”] o/p:
a.reverse() ['cs', 5, 20, 10]
print(a)
STRING count() function LIST count() function
takes atmost 3 parameters takes only 1 parameter
Ex: Ex:
a="Computer Evolution" a=[11, 22, 22, 33, 11, 10, 11]
b=a.count('o') b=a.count(11)
c=a.count('o', 10,20) c=a.count(11, 4, 8) #ERROR because only argument
print(b, c) print(b, c)

Syntax: Syntax:
string.count(substring, startindex=, stopindex=) list.count( data )

By default, startindex = 0 stopindex =len(string)


STRING index() function LIST index() function
takes atmost 3 parameters takes atmost 3 parameters
Ex: Ex:
a="Computer Evolution" a=[11, 22, 22, 33, 11, 10, 11]
b=a.index('o') b=a.index(11)
c=a.index('o', 10,20) c=a.index(11, 3, 10)
print(b, c) print(b, c)

Syntax: Syntax:
string.index(substring, startindex=, stopindex=) list.index(substring, startindex=, stopindex=)

index() function checks whether the specified value(i.e data)


is present in the given list or not.
By default, If present, it returns the index of the first occurrence of the
startindex = 0 , stopindex =len(string) specified substring
OUTPUT
QUESTIONS
3a. Find and write the output of the following python code :
L1=[11, 15, 24, 38, 82]
L1.append("cs") o/p:
[11, 15, 'ip', 24, 38, 82, 'cs'] 7
L1.insert(2, "ip") 53
print(L1, len(L1)) [11, 'ip', 24, 82, 'cs'] 5
print(L1.pop(1) + L1.pop(3))
print(L1, len(L1))

3b. Find and write the output of the following python code :
L1=list('aeiou')
L1.remove('o') o/p:
L1.insert(1, 'p') ['a', 'p', 'e', 'i', 'u'] u
2
print(L1, max(L1)) ['a', 'p', 'e', 'i', 'u', 'e'] u
L1.append('e')
print(L1.index('e', 1, 5))
print(L1, max(L1))
3c. Example for Nested List
Consider the nested list structure L=['Computer', 'Is', ['An', 'Electronic'], 'Device']
Which of the following will access the string "Electronic"
(a) L[2][1]
(b) L[2][0]
(c) L[1][2]
(d) L[2]

o/p: Option a L[2][1]


Question-4 2 MARKS
Explain the following 2 list functions:
sort(), extend()
sort() function

is used for arranging the list datas either in ascending or descending order

Syntax:
list.sort( reverse= )

By default, reverse input will be taken as False

Ex:
L1.sort() #It will arrange list datas in ascending order

L1.sort(reverse=True)
#It will arrange list datas in descending order
append() function extend() function

1. It is used for adding one data at the end of the list 1. It is used for adding many datas at the end the list

2. No of parameters: 1 2. No of parameters: 1

3. Parameter may be of any tuple 3. Parameter must be a sequence.


(i.e) (i.e)
a number or a string or
a decimal no or a list or
a string or a tuple or
a list or a dictionary
a tuple or
a dictionary

4. Description:
4. Description: Each data or character in the parameter will be taken
The parameter will be added as a single data at the end and added as separate data at the end of the list.
of the list

5. Syntax: list.append(data) 5. Syntax: list.extend(sequence)


EXERCISE
QUESTION
Q5 Write appropriate function names for the specified tasks:
(i) to place an element 88 at the position 4 in the list L1

(ii) to delete the last element of the list L1

(iii) to arrange the elements of the list L1 in ascending order

(iv) to know the position of the element “tech” in the list L1

(v) to delete the element present at the index 3

(vi) to find out the number of times the element 35 occurred in the list L1
Question-6 2 MARKS
Explain the following 6 built-in functions:
len()
min()
max()
sum()
sorted()
list()
sorted() function sort () function
● is one of the built-in function ● is one of the list function

● Using this function, we can arrange the list


● IS used for arranging the list datas either in ascending or
datas only in ascending order descending order

● Moreover changes will not happen in the


given list

Ex: Ex:
L1 = [9, 3, 40, 21] L1.sort() ( for ascending order)
L1.sort(reverse=True)
sorted( L1 )

Syntax: sorted( list )


Syntax: list.sort( reverse =)
min() function max() function

finds and returns the maximum value of the list

Syntax:
min ( list )

len() function sum() function

finds and returns the sum of all the numbers in the list

Syntax:
sum ( list)
list() function

Creates an empty list

OR

Convert the given sequence into list

Syntax: list( data= )

Examples:

L1 = list () This code will create an empty list

L1 = list (“Welcome”) This code will create a list with 7 datas


OUTPUT
QUESTIONS
Q7a. FIND THE OUTPUT
a=[2, 4, 6, 8]
b=[5, 2]
c=[1, 2, 12, 7]
o/p:
print(a>b) ['cs', 5, 20, 10]

print(b>c)
b.append([10, 20, 30])
a.append(a.index(4))
print(a, b, c, sep=" ")
print(len(a), sum(a))
Q7b. FIND THE OUTPUT

LIST=[10, 11, 12, 30, 32, 34, 35, 38, 40, 2]


S=0
for i in range(len(A)//2):
S+=(LIST[i] *2)
o/p:
print(S) 190
Q7c. FIND THE OUTPUT
L1=[10, 11, 22, 33, 46, 55]
S1=S2=0
for i in range(len(l1)):
if L1[i] % 3==0:
S1+= L1[i]
elif L1[i] % 5==0:
S2+= L1[i] o/p:
33 65

print(S1, S2)
Q7d. FIND THE OUTPUT
Subject=['CS', 'HINDI', 'PHYSICS', 'CHEMISTRY', 'MATHS']
n=len(Subject)
for i in range(0, n):
if len(Subject) > 4:
Subject[i]=Subject[i]+ Subject[i]
else: o/p:
Subject[i]=Subject[i] ['CSCS',
'HINDIHINDI’,'PHYSICSPH
print(Subject) YSICS',
'CHEMISTRYCHEMISTRY',
'MATHSMATHS']
THEORY
QUESTION
Q8. what is SLICING ?
String Slicing
String slicing is a method used for obtaining a part from
the string.

Slicing method can be applied on List and String.


Slicing format
Slicing format::
Stringobject [ startindex : stopindex : step ]
OR
Listobject [ startindex : stopindex : step ]

By Default, startindex is 0, stopindex= lastindex, step is 1

Important Note
If the step value is +ve, then slicing will be done in forward direction
If the step value is -ve, then slicing will be done in reverse direction
S=”Repository”
0 S 1 2 3 4 5 6 7 8 9

R e p o s i t o r y

-10 -9 -8 -7 -6 -5 -4 -3 -2 -1

S[0:5] =”Repos” S[::-1] =”yrotisopeR”


S[0:8:2]=”Rpst” S[-1: -4]=””
S[2:]=”pository”
S[::] =”Repository”
S[-1:-4:-1] =”yro”
S[::-2] =”yoioe”
L=[10, 22, 78, 90, 11, 55]
S

L[0:2] =[10, 22]

L[5:7] =[10]

L[::-3] =[90, 10]


LIST PROGRAMS
OUTPUT DESIGN PROGRAM Q9a
Enter number of datas ::: Write a program to read a list of n integers.
6

Enter 6 numbers: Also find the maximum, minimum & mean of


50 numeric values stored in a list.
-3
-57
78
90
67

Maximum value in the list is: 90


MInimum value in the list is: -57
Mean of all values in the list = 225/6= 37.5
OUTPUT DESIGN PROGRAM Q9b
Enter number of datas :::
6
Write a program to read a list of n integers.

Enter 6 numbers: Also search whether the given element is


50
60 present in the list or not using LINEAR
70 SEARCH.
80
60
100

Enter search element: 60

Search Data is present


THEORY
QUESTION
Question-10a 2 MARKS
what is List Traversing?

processing each and every character in the given


list is referred as Traversing.
example: example:for Traversing
for i in L: using indexing
print(i) for i in range(len(L)):
print(L[i]
Question-10b 2 MARKS
List out List operations.

4 listoperations
● concatenation ex: [10, 20] + [45, 67, 78, 89]
● replication ex: [10, 20] * 5
● membership ex: if 10 in [11, 25, 55, 77, 88]
● slicing

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy