0% found this document useful (0 votes)
251 views

Practical 2

The document contains a Python code with multiple functions to perform operations on lists, tuples and dictionaries. It defines functions like AddValues to add values to lists, DispValues to display list values, functions to rearrange list contents like SwapPair and SwapHalf, functions to perform operations on dictionary keys and values like creating a dictionary with corresponding tuple and list values. The code contains examples of calling the defined functions in different orders to test their implementation.

Uploaded by

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

Practical 2

The document contains a Python code with multiple functions to perform operations on lists, tuples and dictionaries. It defines functions like AddValues to add values to lists, DispValues to display list values, functions to rearrange list contents like SwapPair and SwapHalf, functions to perform operations on dictionary keys and values like creating a dictionary with corresponding tuple and list values. The code contains examples of calling the defined functions in different orders to test their implementation.

Uploaded by

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

Delhi Public School, R.K.

Puram, New Delhi


Computer Science Practical List 2
1. Write a Python code to accept Marks of English and Maths in two separate lists E and M for 5 students, create
another list as T to have the total marks as sum of corresponding marks of E and M, display the following
(without using Builtin Functions/methods):
a. Content of E
b. Content of M
c. Content of T
d. Minimum marks of E, M and T
e. Maximum marks of E, M and T
f. Average marks of E, M and T
Answer:-
E=[]
M=[]
T=[]
leng=0
while True:
EM=int(input("Enter English Marks:"))
MM=int(input("Enter Mathematics Marks:"))
E+=[EM]
M+=[MM]
T+=[EM+MM]
leng+=1
choice=input("More(y/n):")
if choice in "Nn":
break
minE=maxE=E[0]
minM=maxM=M[0]
minT=maxT=T[0]
sumE=sumM=sumT=0
for i in range(0,leng,1):
if minE>=E[i]:
minE=E[i]
if minM>=M[i]:
minM=M[i]
if minT>=T[i]:
minT=T[i]
if maxE>=E[i]:
maxE=E[i]
if maxM>=M[i]:
maxM=M[i]
if maxT>=T[i]:
maxT=T[i]
sumE+=E[i]
sumM+=M[i]
sumT+=T[i]
print("Minimum of English Marks:",minE)

[REF:DPSR/COMP12/2023-2024/L2]
Delhi Public School, R.K.Puram, New Delhi
print("Maximum of English Marks:",maxE)
print("Average of English Marks:",sumE/leng)
print("Minimum of Mathematics Marks:",minM)
print("Maximum of Mathematics Marks:",maxM)
print("Average of Mathematics Marks:",sumM/leng)
print("Minimum of Total Marks:",minT)
print("Maximum of Total Marks:",maxT)
print("Average of Total Marks:",sumT/leng)

2. Write a Python code to perform the following:


a. To initialize a list A=[10,20,30,40]
b. To initialize a list B=[35,25,15,45]
c. To create a list C to get the content from A and B as [10,35,20,25,30,15,40,45]
d. To display content of C
Answer:-
A=[10,20,30,40]
B=[35,25,15,45]
C=[]
for i in range(0,len(B)):
C.append(A[i])
C.append(B[i])
print(C)

3. Write a Python code with the following functions:


a. AddValues(L) - To add integer values entered by the user in the list L
b. DispValues(L) - To display values of L
c. RevContent(L) - To re-arrange content of L by reversing the order of values
d. AddEven(L) - To add all the even values of L and display the sum
e. AddOdd(L) - To add all the odd values of L and display the sum
f. AddZeroEnd(L) - To add all the values of L, which are ending with 0 and display the sum
g. Show7Ten(L) - To display those values of L, which have 7 at tens place
h. Show20_50(L) - To display those values of L, which are between 20 and 50.
Also, call the above function in the order as a, b, c, b, d, e, f, g and h
Answer:-
def AddValues(L):
while True:
IT=int(input("enter Nos:"))
L.append(IT)
choice=input("More(y/n)?")
if choice in "nN":
break
return L
def DispValues(L):
for i in L:
print(i)
def RevContent(L):
L=L[::-1]
def AddEven(L):

[REF:DPSR/COMP12/2023-2024/L2]
Delhi Public School, R.K.Puram, New Delhi
SumE=0
for i in L:
if i%2==0:
SumE+=i
def AddOdd(L):
SumO=0
for i in L:
if i%2!=0:
SumO+=i
def AddZeroEnd(L):
SumZE=0
for i in L:
d=i%10
if d == 0:
SumZE+=i
def Show7Ten(L):
for i in L:
d=(i//10)%10
if d==7:
print(i)
def Show20_50(L):
for i in L:
if i >20 and i<50:
print(i)
L=[]
L=AddValues(L)
DispValues(L)
RevContent(L)
DispValues(L)
AddEven(L)
AddOdd(L)
AddZeroEnd(L)
Show7Ten(L)
Show20_50(L)

4. Write a Python code with the following functions:


a. AddValues(L) - To add integer values entered by the user in the list L
b. DispValues(L) - To display values of L
c. SwapPair(L) - To re-arrange the content of L by swapping each pair of adjacent neighbouring values
d. SwapHalf(L) - To re-arrange content of L by swapping first half of the list with second half of the list
Also, call the above functions in the order as a, b, c, b, d and b.
Answer:-
def AddValues(L):
while True:
IT=int(input("enter Nos:"))
L.append(IT)

[REF:DPSR/COMP12/2023-2024/L2]
Delhi Public School, R.K.Puram, New Delhi
choice=input("More(y/n)?")
if choice in "nN":
break
return L
def DispValues(L):
for i in L:
print(i)
def SwapPair(L):
for i in range(0,len(L),2):
L[i],L[i+1]=L[i+1],L[i]
def SwapHalf(L):
hv=len(L)//2
L[:hv:],L[hv::]=L[hv::],L[:hv:]
L=[]
L=AddValues(L)
DispValues(L)
SwapPair(L)
DispValues(L)
SwapHalf(L)
DispValues(L)

5. Write a Python code with the following functions:


a. AddCity(C) - To add names of cities (as strings) entered by user in the list C
b. AllUcase(C) - To change the names of cities in uppercase in the list C
c. ShowDisp(C) - To display names of cities from list C
d. Arrange(C) - To arrange the names of cities from list C in alphabetical order (descending).
e. ShortNameCities(C) - To display the names of those cities from list C, which have 4 or less number of
characters
f. BigNameCities(C) - To display the names of those cities from list C, which have more than 4 characters
g. CityNameLength(C) - To display number of alphabets present in each name of the city in list C
Also, call the above functions in the order as a, c, b, c, d, c, e, f and g
Answer:-
def AddCity(C):
C=[]
while True:
city=input("enter city name:")
C.append(city)
opt=input("more Y/N?")
if opt=="N":
break
return C

def AllUCase(C):
l=len(C)
for i in range(0,l,1):
C[i]=C[i].upper()
def ShowDisplay(C):

[REF:DPSR/COMP12/2023-2024/L2]
Delhi Public School, R.K.Puram, New Delhi
print(C)
def Arrange(C):
C=sorted(C,reverse=True)
print(C)
def ShortNameCities(C):
ShortCity=[]
for i in C:
if len(i)<=4:
ShortCity.append(i)
print(ShortCity)
def BigNameCities(C):
BigCity=[]
for i in C:
if len(i)>4:
BigCity.append(i)
print(BigCity)
def CityNameLength(C):
Length=[]
for i in C:
LEN=len(i)
Length.append(LEN)
print(Length)
L=[]
L=AddCity(L)
ShowDisplay(L)
AllUCase(L)
ShowDisplay(L)
Arrange(L)
ShowDisplay(L)
ShortNameCities(L)
BigNameCities(L)
CityNameLength(L)

6. Write a Python code with the following functions:


a. AddBooks(B) - To add titles of books (as strings) entered by user in the list B
b. AllIndia(B) - To find and display titles of all the books, which contain "INDIA" or "India" as part of its title.
c. ShowBooks(B) - To display titles of books from list B
d. SingleWords(B) - To display the uppercase equivalent of single word titles of books from list B.
e. CountSingle(B) - To display the number of books from the list B which contain only a single word in its title.
f. ThetoA(B) - To change the presence of the word “The” to “A” in the titles of the list B.
Also, call the above functions in the order as a, c, b, d, e and f
Answer:-
def AddBooks(B):
B=[]
while True:
book=input("enter book title:")

[REF:DPSR/COMP12/2023-2024/L2]
Delhi Public School, R.K.Puram, New Delhi
B.append(book)
opt=input("more Y/N?")
if opt in "Nn":
break
return B
def AllIndia(B):
b=B
for i in range(0,len(b)):
b[i]=b[i].lower()
if "india" in b[i]:
print(B[i])
def ShowBooks(B):
print(B)
def SingleWords(B):
for i in B:
S=i
c=1
S.strip()
for i in range(0,len(S)):
if S[i]==" ":
c+=1
if c==1:
print(S.upper())
def CountSingle(B):
count=0
for i in B:
S=i
c=1
S.strip()
for i in range(0,len(S)):
if S[i]==" ":
c+=1
if c==1:
count+=1
print(count)
def ThetoA(B):
for i in range(0,len(B)):
S=B[i]
S=S.title()
S=S.replace("The","A")
B[i]=S
print(B)
B=[]
B=AddBooks(B)

[REF:DPSR/COMP12/2023-2024/L2]
Delhi Public School, R.K.Puram, New Delhi
ShowBooks(B)
AllIndia(B)
SingleWords(B)
CountSingle(B)
ThetoA(B)

7. Write a Python code to perform the following on a tuple T=(10,40,20,30,50,70)


a. To display the content of T in reverse order
b. To add and display the sum of values stored in T
c. To find and display minimum and maximum values present in T
d. To display sum of each adjacent pair of values
e. To find those pair (any pair 1st-2nd, 1st-5th, 3rd-6th,...) of values from the content of T, whose sum is
same as one of the values in T
Answer:-
T=(10,40,20,30,50,70)
print(T[::- 1])
s=0
for i in T:
s+=i
print(s)
print(max(T),min(T))
for i in range(0,len(T),2):
print(T[i]+T[i+1])
for i in range(0,len(T)):
for j in range(i,len(T)):
if T[i]+T[j] in T:
print(T[i],T[j],T[i]+T[j])

8. Write a Python code to perform the following:


a. To initialize a tuple WD containing (1,2,3,4,5,6,7)
b. To initialize a list WDN containing ['SUN','MON','TUE','WED','THU','FRI','SAT']
c. To create a dictionary W with key-value pairs with corresponding values from WD and WDN
d. To display content of W
e. To re-arrange the content of dictionary in such a way that it becomes as follows:
{1:'MON',2:'TUE',3:'WED',4:'THU,5:'FRI',6:'SAT',7:'SUN'}
f. To display the content of W
g. To copy the partial content of W in dictionaries MyDays and OfficeDays, MyDays should have content
from keys 2,4 and 7 and rest from W to be the content of OfficeDays.
h. To display the contents of MyDays and OfficeDays
Answer:-
WD=(1,2,3,4,5,6,7)
WDN=['SUN','MON','TUE','WED','THU','FRI','SAT']
W={}
for i in range(0,len(WD)):
W[WD[i]]=WDN[i]
print(W)
for i in range(0,len(WD)-1):
W[WD[i]]=WDN[i+1]
W[7]=WDN[0]

[REF:DPSR/COMP12/2023-2024/L2]
Delhi Public School, R.K.Puram, New Delhi
print(W)
MyDays={}
OfficeDays={}
MyDays[2]="TUE"
MyDays[4]="THU"
MyDays[7]="SUN"
del W[2]
del W[4]
del W[7]
OfficeDays=W
print(MyDays,OfficeDays)

9. Write a Python code to perform the following operations:


a. To initialize a tuple TL=('RED','YELLOW','GREEN')
b. To accepts names of 10 colors from user and store them in a list CL
c. To display the color names from CL along with corresponding message "TRAFFIC LIGHT" and "NOT
TRAFFIC LIGHT" after checking from the content of TL
d. To initialize another tuple TM=('STOP','BE READY TO START/STOP','GO')
e. To create a dictionary TLM by combining corresponding key-value pairs from TL and TM.
f. To display the content of TLM
Answer:-
TL=('RED','YELLOW','GREEN')
CL=[]
for i in range(0,10):
C=input("Colour?:")
CL.append(C)
for i in range(0,10):
if CL[i].upper() in TL:
print(CL[i],"TRAFFIC LIGHT")
else:
print(CL[i],"NOT TRAFFIC LIGHT")
TM=('STOP','BE READY TO START/STOP','GO')
TLM={}
for i in range(0,len(TL)):
TLM[TL[i]]=TM[i]
print(TLM)

General Instructions:
i. Type the solution of all the problems using Python Language on Python IDLE or Colab
ii. Type the following on top of each of your program as comment lines
"""
Program List : <Practical Assignment List number appears here>
Program No : <Question number in the list>
Developed By : <Your name appears here>
Date : <Date when the program was coded appears here>
"""
iii. On successful execution with meaningful data, copy and paste the sample meaningful output at the bottom of the

[REF:DPSR/COMP12/2023-2024/L2]
Delhi Public School, R.K.Puram, New Delhi
program as comment lines.
iv. Add all the codes in separate pages of a single Google Doc named as in the format as
<Class>-<Section>-<L2>-<Name> For example XII-F-L2-Jai Mathir (If the Name is Jai Mathir of XII-F)
NOTE: Recommended Font, “Courier New”, Style “Bold” Size “10” for all programs with single line
spacing. Default indentation 2 Character only.
v. “Turn in” the Doc as a single document in response to this assignment submission with all programs. Once
verified by the teacher, take out a hardcopy from the printer and get them signed by the respective computer
teacher.

[REF:DPSR/COMP12/2023-2024/L2]

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