Assignment 1701572062
Assignment 1701572062
Assignment 1701572062
LIST
9 list methods/functions:
append(), extend(), insert(), pop(), remove(),
count(), index(),
reverse(), sort(),
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
&
● 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 )
Syntax: Syntax:
string.index(substring, startindex=, stopindex=) list.index(substring, startindex=, stopindex=)
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]
is used for arranging the list datas either in ascending or descending order
Syntax:
list.sort( reverse= )
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
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
(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
Ex: Ex:
L1 = [9, 3, 40, 21] L1.sort() ( for ascending order)
L1.sort(reverse=True)
sorted( L1 )
Syntax:
min ( list )
finds and returns the sum of all the numbers in the list
Syntax:
sum ( list)
list() function
OR
Examples:
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
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.
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
L[5:7] =[10]
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
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: