Ex.
No:08
Date:7.02.2021
7. Develop python programs to perform operations on list and tuples.
Exercises:
1. Write a program to perform the following operations using list and tuples
i. Create the list
ii. Update list using negative index
iii. Create a sub list using Slicing
iv. Display the elements in sorting order
v. Delete the elements using Remove, pop, and clear
vi. Converts a list into tuple
Coding:
a=[4,5,6,7,8,9,0,10]
print("List:",a)
a.insert(8,12)
print("Updated List:",a)
print("Sublist:",a[0:4])
a.sort()
print("Sorted List:",a)
a.remove(7)
print("After Removing:",a)
a.pop(-3)
print("After Pop:",a)
b=a.clear()
print("After Clearing:",b)
c=[1,2,3,5,6,7,354,2]
d=tuple(c)
print("Tuple=",d)
1
Output:
List: [4, 5, 6, 7, 8, 9, 0, 10]
Updated List: [4, 5, 6, 7, 8, 9, 0, 10, 12]
Sublist: [4, 5, 6, 7]
Sorted List: [0, 4, 5, 6, 7, 8, 9, 10, 12]
After Removing: [0, 4, 5, 6, 8, 9, 10, 12]
After Pop: [0, 4, 5, 6, 8, 10, 12]
After Clearing: None
Tuple= (1, 2, 3, 5, 6, 7, 354, 2)
Result:Thus the Program has been successfully completed
2
2.
a. Find the length, maximum and minimum value
b.Concatenation
c.Repetition
d.Membership
e.Iteration
Coding:
a=(4,5,6,7,23,25,57,85,99)
print(len(a))
print(max(a))
print(min(a))
b=(5,10,15,20,25,30)
print(a+b)
print(a*4)
print(4 in a)
print(15 in b)
for x in a:
print(x)
c=0
while (c<4):
c=c+1
print("Thank You")
3
Output:
99
(4, 5, 6, 7, 23, 25, 57, 85, 99, 5, 10, 15, 20, 25, 30)
(4, 5, 6, 7, 23, 25, 57, 85, 99, 4, 5, 6, 7, 23, 25, 57, 85, 99, 4, 5, 6, 7, 23, 25, 57, 85, 99, 4, 5,
6, 7, 23, 25, 57, 85, 99)
True
True
23
25
57
85
99
Thank You
Thank You
Thank You
Thank You
4
Result:Thus the Program has been successfully completed
5
3.Write a python function, find_pairs_of_numbers() which accepts a list of positive integers with no
repetitions and returns count of pairs of numbers in the list that adds up to n. The function should
return 0, if no such pairs are found in the list.
Coding:
def find_pairs_of_numbers(num_list,n):
count=0
for num1 in num_list:
for num2 in num_list:
if(num1+num2)==n:
count=count+1
return int(count/2)
num_list=[1,2,3,4,5,6,7,8,9,10,11,0]
n=11
print(find_pairs_of_numbers(num_list,n))
Output:
6
Result:Thus the Program has been successfully completed
6
4.A teacher is in the process of generating few reports based on the marks scored by the students of
her class in a project based assessment.
Assume that the marks of her 10 students are available in a tuple. The marks are out of 25.
Write a python program to implement the following functions:
1. find_more_than_average(): Find and return the percentage of students who have scored
more than the average mark of the class.
2. generate_frequency(): Find how many students have scored the same marks. For example,
how many have scored 0, how many have scored 1, how many have scored 3….how many
have scored 25. The result should be populated in a list and returned.
3. sort_marks(): Sort the marks in the increasing order from 0 to 25. The sorted values should
be populated in a list and returned.
Sample Input Expected Output
70.0
list_of_marks = [0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 2, 0, 2,
(12,18,25,24,2,5,18,20,20,21) 1, 0, 0, 1, 1]
[2, 5, 12, 18, 18, 20, 20, 21, 24, 25]
Coding:
list_of_marks= (5,4,5,10,13,13,20,21,25,25)
def find_more_than_average():
global list_of_marks
marks=0
count=0
for x in list_of_marks:
marks+=x
average=marks/len(list_of_marks)
for x in list_of_marks:
if x>average:
count+=1
percentage=(count*100)/len(list_of_marks)
return percentage
def sort_marks():
global list_of_marks
list_of_marks=sorted(list_of_marks)
return list_of_marks
def generate_frequency():
freq=[]
global list_of_marks
for x in range(101):
count=0
for y in list_of_marks:
if x==y:
count+=1
freq.append(count)
return freq
print(find_more_than_average())
print(generate_frequency())
print(sort_marks())
7
Output:
40.0
[0, 0, 0, 0, 1, 2, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[4, 5, 5, 10, 13, 13, 20, 21, 25, 25]
>>>
Result:Thus the Program has been successfully completed
8
Ex.No:09
Date:7.02.2021
9.Implement dictionary and sets in python
Exercises:
1. Write a program to perform the following operations using sets in python.
i. Find the common letters in the given sets
ii. Find the Letters which are presented in the First set but not in the Second set and vice
versa.
iii. Find the Symmetric difference between the set
iv. Find the given sets are pangram or not
v. Count the number of vowels and constants
Coding:
a={'a','b','c','d','x','y','z'}
b={'z','y','a','c','q','r','b'}
c= list(set(a)&set(b))
print("The common letters are ", c)
d= set(a)- set(b)
e= set(b)- set(a)
print("The letters present in a but not in b are ", d)
print("The letters present in b but not in a are ", e)
print('The symmetric differnce between a and b are ', a.symmetric_difference(b))
import string
def ispangram(str):
alphabet="abcdefghijklmnopqrstuvwxyz"
for char in alphabet:
if char not in str.lower():
return False
return True
f= str(input(" Enter the pangram : "))
if(ispangram(f)==True):
print("It is a pangram")
else:
print("It is not an pangram")
g= input("Enter a string : ")
vowels=0
consonants=0
for i in g:
if(i=="a" or i=="e" or i=="i" or i=="o" or i=="u" or i=="A" or i=="E" or i=="I" or i=="O" or
i=="U"):
vowels=vowels+1
else:
consonants=consonants+1
print("The number of vowels are : ", vowels)
print("The number of consonants are : ", consonants)
9
Output:
The common letters are ['y', 'a', 'b', 'z', 'c']
The letters present in a but not in b are {'x', 'd'}
The letters present in b but not in a are {'r', 'q'}
The symmetric differnce between a and b are {'q', 'r', 'x', 'd'}
Enter the pangram : Sphinx of black quartz,judge my vow
It is a pangram
Enter a string : Sphinx of black quartz,judge my vow
The number of vowels are : 8
The number of consonants are : 27
Result:Thus the Program has been successfully completed
10
2. Create a Dictionary for collecting the student’s details to perform the following operation.
i. Update
ii. Copy
iii. Remove an element
iv. Display keys and items
v. Delete all the items
Coding:
a={"Name":"Sanjay S","Age":"18","Course":"BE","Branch":"ECE","Sec":"B"}
print("Dictionary is" ,a)
a["Sec"]="C"
print("updated Dictionary ",a)
b=a.copy()
print("Copied Dictionary ",b)
c= a.pop("Course")
print("The Dictionatry with a removed element ",c)
d= a.keys()
print("The Keys present in the Dictionary are ",d)
e= a.values()
print("The Items present in the Dictionary are ",e)
f= a.clear()
print("Dictionary is ", f)
Output:
Dictionary is {'Name': 'Sanjay S', 'Age': '18', 'Course': 'BE', 'Branch': 'ECE', 'Sec':
'B'}
updated Dictionary {'Name': 'Sanjay S', 'Age': '18', 'Course': 'BE', 'Branch': 'ECE',
'Sec': 'C'}
Copied Dictionary {'Name': 'Sanjay S', 'Age': '18', 'Course': 'BE', 'Branch': 'ECE',
'Sec': 'C'}
The Dictionatry with a removed element BE
The Keys present in the Dictionary are dict_keys(['Name', 'Age', 'Branch', 'Sec'])
The Items present in the Dictionary are dict_values(['Sanjay S', '18', 'ECE', 'C'])
Dictionary is None
11
Result:Thus the Program has been successfully completed
12
3. Care hospital wants to know the medical specialty visited by the maximum number of
patients. Assume that the patient id of the patient along with the medical specialty visited by
the patient is stored in a list. The details of the medical specialties are stored in a dictionary as
follows:
{
"P":"Pediatrics",
"O":"Orthopedics",
"E":"ENT
}
Write a function to find the medical specialty visited by the maximum number of patients and
return the name of the specialty.
Also write the pytest test cases to test the program.
Note:
1.Assume that there is always only one medical specialty which is visited by maximum number of
patients.
2.Perform case sensitive string comparison wherever necessary.
Expected
Sample Input
Output
[101,P,102,O,302,P,305,P] Pediatrics
[101,O,102,O,302,P,305,E,401,O,656,O] Orthopedics
[101,O,102,E,302,P,305,P,401,E,656,O,987,E] ENT
Coding:
def max_visited_speciality(patient_medical_speciality_list,medical_speciality):
p_count,o_count,e_count=0,0,0
for key,val in medical_speciality.items():
i=1
for x in range(len(patient_medical_speciality_list)//2):
if 'P' == patient_medical_speciality_list[i]:
p_count += 1
if 'O' == patient_medical_speciality_list[i]:
o_count += 1
if 'E'==patient_medical_speciality_list[i]:
e_count += 1
i+=2
if p_count>o_count:
if p_count>e_count:
return medical_speciality['P']
if o_count>p_count:
if o_count>e_count:
return medical_speciality['O']
if e_count>p_count:
if e_count>o_count:
return medical_speciality['E']
patient_medical_speciality_list=[101,'O',102,'E',302,'P',305,'P',401,'E',656,'O',987,'P']
medical_speciality={"P":"Pediatrics","O":"Orthopedics","E":"ENT"}
speciality=max_visited_speciality(patient_medical_speciality_list,medical_speciality)
print("speciality:",speciality)
13
Output:
speciality: Pediatrics
>>>
Result:Thus the Program has been successfully completed
Final Result:All the Program has Executed Successfully with Proper Output
14