Question 1: Write a program to find the second largest element in list ‘n’.
print("Enter the Number of elements in the list:")
N = int(input () )
i=0
num = []
while i < N:
print("Enter list values:")
num1= int(input() )
[Link](num1)
i +=1
print("The Original List is:", end= ' ')
for i in range(N) :
print(num[i],end= '')
if (num[0] > num[1] ) :
m, m2 = num[0],num[1]
else:
m, m2 = num[1],num[0]
for x in num[2:]:
if x > m2:
if x > m:
m2, m = m, x
else:
m2 = x
print()
print("The Second Largest element in the list is:",m2)
Enter the Number of elements in the list:
Enter list values:
Enter list values:
18
Enter list values:
2009
The Original List is: 9 18 2009
The Second Largest element in the list is: 18
Question 2: Write a Python code to accept values from a user up to a certain
limit; if the number is even, then add it to the list.
L=[]
n=int(input("Enter the total number of values in the list:"))
i=1
while i<=n:
a=int(input("Enter the elements:"))
if a%2==0:
[Link](a)
i=i+1
print (L)
Enter the total number of values in the list:6
Enter the elements:9
Enter the elements:8
Enter the elements:7
Enter the elements:6
Enter the elements:5
Enter the elements:2
[8, 6, 2]
Question 3: Write a program to read a list of elements. Input an element from
the user that has to be inserted in the list. Also, input the position at which it is
to be inserted. Apply a built-in function to insert the element at the desired
position in the list.
L1=[]
n=int(input("Enter the no. of elements that have to be entered into the list:"))
print("Enter the elements for the list:")
for i in range (n) :
elem=int(input())
[Link](elem)
print("List: ", L1)
print()
ele=int(input("Element to be inserted: "))
pos=int(input("Position at which it is to be inserted: "))
[Link](pos,ele)
print("New List: ",L1)
Enter the no. of elements that have to be entered into the list:4
Enter the elements for the list:
19
27
36
List: [9, 19, 27, 36]
Element to be inserted: 18
Position at which it is to be inserted: 2
New List: [9, 19, 18, 27, 36]
Question 4: Write a Python program to delete all odd numbers and negative
numbers from a given numeric list.
list1 = [11,-1,22,-2,33,-4,44,-5,55,-6,66,-7,77]
len1 = len(list1)
i=0
while i < len1:
if (list1[i] < 0):
del list1[i]
len1 = len1 - 1
i = i -1
elif (list1[i] % 2 != 0):
del list1[i]
len1 = len1 - 1
i=i-1
i = i +1
print("Updated list after deletion of elements:",list1)
Updated list after deletion of elements: [22, 44, 66]
Question 5: Write a menu driven program to perform various list operations.
i)append
ii)insert
iii)append a list to another list
iv)modify element
v)delete existing element from given position
vi)delete existing element with the given value
l1=[]
n=int(input("How many elements do you want to enter in the list:"))
print("Enter the elements:")
for i in range (n) :
ele=int(input())
[Link](ele)
print("The entered list is: ",l1,'\n')
while True:
print("\nLIST OPERATIONS")
print("1. Append the element")
print("2. Insert an element at desired position")
print("3. Append a list to the given list")
print("4. Modify an existing element")
print("5. Delete existing element by its position")
print("6. Delete exisiting element by its value")
print("7. Exit\n")
choice=int(input("Enter your choice(1-7): "))
if choice==1:
ele=int(input("Enter the element to be appended: "))
[Link](element)
print("New list: ",l1,'\n')
elif choice==2:
element=int(input("Enter the element to be inserted: "))
pos=int(input("Enter the position at which it is to be inserted: "))
[Link](pos,element)
print("New list: ",l1,'\n')
elif choice==3:
new_list=list(eval(input("Enter the list to be appended: ")))
[Link](new_list)
print("New list: ",l1,'\n')
elif choice==4:
pos=int(input("Enter the position of the element to be modified: "))
new_element=int(input("Enter the new element: "))
l1[pos]=new_element
print("New list: ",l1,'/n')
elif choice==5:
pos=int(input("Enter position of element to be deleted: "))
[Link](pos)
print("New list: ",l1,'\n')
elif choice==6:
element=int(input("Enter the element to be deleted: "))
[Link](element)
print("New list: ",l1,'\n')
elif choice==7:
break
else:
print("Invalid choice opted for")
How many elements do you want to enter in the list:4
Enter the elements:
The entered list is: [1, 4, 5, 2]
LIST OPERATIONS
1. Append the element
2. Insert an element at desired position
3. Append a list to the given list
4. Modify an existing element
5. Delete existing element by its position
6. Delete exisiting element by its value
7. Exit
Enter your choice(1-7): 4
Enter the position of the element to be modified: 1
Enter the new element: 12
New list: [1, 12, 5, 2] /n
LIST OPERATIONS
1. Append the element
2. Insert an element at desired position
3. Append a list to the given list
4. Modify an existing element
5. Delete existing element by its position
6. Delete exisiting element by its value
7. Exit
Enter your choice(1-7): 5
Enter position of element to be deleted: 3
New list: [1, 12, 5]
LIST OPERATIONS
1. Append the element
2. Insert an element at desired position
3. Append a list to the given list
4. Modify an existing element
5. Delete existing element by its position
6. Delete exisiting element by its value
7. Exit
Question 6: Write a Python code to create a tuple by accepting elements from
the user using while loop.
t=tuple()
n=int(input("Enter number of elements :"))
i=1
while (i<=n):
a=input("Enter number:")
t=t+(a,)
i=i+1
print("Tuple created as:")
print(t)
Enter number of elements :4
Enter number:100
Enter number:200
Enter number:300
Enter number:400
Tuple created as:
('100', '200', '300', '400')
Question 7: Write a Python code to store n number of subjects in a tuple.
t = tuple()
n=int(input("How many subjects you want to add: "))
print ("Enter all subjects one after another")
for i in range(n):
a = input("Enter the subject: ")
t+=(a,)
print("output is")
print(t)
How many subjects you want to add: 5
Enter all subjects one after another
Enter the subject: Physics
Enter the subject: Chemistry
Enter the subject: Mathematics
Enter the subject: Computer Science
Enter the subject: English
output is
('Physics', 'Chemistry', 'Mathematics ', 'Computer Science', 'English')
Question 8: Write a Python program to create a nested tuple to store roll
number, name and the marks of the students.
st=((200,"Dhwani",90),(201,"Ramneek",50),(202, "Rakshita", 90),(203, "Sahaj", 60))
print("S_No"," Roll_No", " Name", " Marks")
for i in range(0,len(st)):
print((i+1),'\t',st[i][0],'\t','st[i][2]')
S_No Roll_No Name Marks
1 200 90
2 201 50
3 202 90
4 203 60
Question 9: Write a program to perform linear search on a tuple of numbers.
Tuple= (1, 2, 3, 4, 5, 6)
n =int(input("Enter the element to be searched: "))
flag=False
for i in range(len(Tuple)):
if Tuple[i] ==n:
print("Element found at index number",i)
flag=True
if flag==True:
print("Successful seach")
else:
print("Element not found")
Enter the element to be searched: 5
Element found at index number 4
Successful seach
Question 10: Write a Python program to input any two tuples and swap their values.
t1 = tuple()
n = int(input("Total no. of elements in the first tuple: "))
for i in range(n):
a = input("Enter the elements :")
t1 = t1+(a,)
t2 = tuple()
m = int(input("Total no. of values in the second tuple: "))
for i in range(m):
a = input("Enter the elements :")
t2 = t2+(a,)
print("First tuple :")
print(t1)
print("Second tuple :")
print(t2)
t1,t2=t2,t1
print("After swapping :")
print("First tuple :")
print(t1)
print("Second tuple :")
print(t2)
Total no. of elements in the first tuple: 3
Enter the elements :10
Enter the elements :20
Enter the elements :30
Total no. of values in the second tuple: 4
Enter the elements :100
Enter the elements :200
Enter the elements :300
Enter the elements :400
First tuple : ('10', '20', '30')
Second tuple : ('100', '200', '300', '400')
After swapping :
First tuple :
('100', '200', '300', '400')
Second tuple :
('10', '20', '30')
Question 11: Write a Python program to input the total number of sections and steams of
class XI and display all the information on the output screen.
classxi=dict()
n=int(input("Enter total number of sections in xi class: "))
i=1
while i<=n:
a=input("Enter Section: ")
b=input("Enter the Stream: ")
classxi[a]=b
i=i+1
print("Class", '\t' "Section", '\t', "Stream name")
for i in classxi:
print("XI", '\t', i, '\t',classxi[i])
Enter total number of sections in xi class: 5
Enter Section: A
Enter the Stream: Science with Biology
Enter Section: B
Enter the Stream: Science with Maths
Enter Section: C
Enter the Stream: Commerce with Maths
Enter Section: D
Enter the Stream: Commerce without Maths
Enter Section: E
Enter the Stream: Humanities
Class Section Stream name
XI A Science with Biology
XI B Science with Maths
XI C Commerce with Maths
XI D Commerce without Maths
XI E Humanities
Question 12: Write a Python program to enter the name of employees and their salaries as
input and store them as dictionary.
num = int(input("Enter the Number of Employees whose data has to be stored: "))
count = 1
employee = dict()
while count <= num:
name = input("Enter the Name of the Employee: ")
salary = int(input("Enter the Salary of the Employee: "))
employee[name] = salary
count +=1
print("\n\nEMPLOYEE_NAME\tSALARY")
for k in employee:
print(k,'\t\t',employee[k])
Enter the Number of Employees whose data has to be stored: 2
Enter the Name of the Employee: Dhwani
Enter the Salary of the Employee: 1000000
Enter the Name of the Employee: Rakshita
Enter the Salary of the Employee: 100000
EMPLOYEE_NAME SALARY
Dhwani 1000000
Rakshita 100000
Question 13: Write a Python program to count the number of times a character appears in a
string using the dictionary.
str = input("Enter a string: ")
dict1 = {}
for ch in str:
if ch in dict1:
dict1[ch] +=1
else:
dict1[ch] = 1
for key in dict1:
print(key, ':', dict[key])
Enter a string: I Love CS
I : dict['I']
: dict[' ']
L : dict['L']
o : dict['o']
v : dict['v']
e : dict['e']
C : dict['C']
S : dict['S']
Question 14: Write a Python program to store information of students like admission number,
roll number and names in the form of dictionary and display them in dictionary.
SCL=dict()
i= 1
n=int(input("Enter the number of entries: "))
while i<=n:
Adm=input("\nEnter the admission no. of a student: ")
nm=input("Enter name of the student:")
section=input("Enter the class and section: ")
per=float(input("Enter percentage of a student:"))
b=(nm,section,per)
SCL[Adm]=b
i = i+1
l = [Link]()
for i in 1:
print("\nAdmno-","class\t","per\t")
for j in z:
print(j, end="\t")
Enter the number of entries: 3
Enter the admission no. of a student: 100
Enter name of the student:DHWANI
Enter the class and section: XIB
Enter percentage of a student:100
Enter the admission no. of a student: 200
Enter name of the student:ANSHU
Enter the class and section: XIB
Enter percentage of a student:100
Enter the admission no. of a student: 300
Enter name of the student:ROHIT
Enter the class and section: XIB
Enter percentage of a student:100
Admno- 100 :
Name class per
DHWANI XIB 100
Admno- 200:
Name class per
ANSHU XIB 100
Admno- 300:
ROHIT XIB 100
Question 15: Write a Python program to implement keys function in dictionary:
Keys1 =[1,2,3,4]; values1=1000
D1=[Link](Keys1,values1)
print(D1)
Keys2 =['A','B','C','D']; values2="Undefined"
D2=[Link](Keys2,values2)
print(D2)
Keys3 =('100','200','300','400')
D3=[Link](Keys3)
print(D3)
D1=[Link]([100,200,300,400],[1,2,3,4])
print(D1)
{1: 1000, 2: 1000, 3: 1000, 4: 1000}
{'A': 'Undefined', 'B': 'Undefined', 'C': 'Undefined', 'D': 'Undefined'}
{'100': None, '200': None, '300': None, '400': None}
{100: [1, 2, 3, 4], 200: [1, 2, 3, 4], 300: [1, 2, 3, 4], 400: [1, 2, 3, 4]}