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

Practical Assignment-5

The document contains 11 Python programs demonstrating various operations on lists such as appending, inserting, sorting, finding maximum/minimum elements, counting occurrences, segregating into positive and negative lists, calculating median, and reversing lists. The programs contain sample inputs, outputs and explanations of the list operations performed.

Uploaded by

Vishesh Aggarwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Practical Assignment-5

The document contains 11 Python programs demonstrating various operations on lists such as appending, inserting, sorting, finding maximum/minimum elements, counting occurrences, segregating into positive and negative lists, calculating median, and reversing lists. The programs contain sample inputs, outputs and explanations of the list operations performed.

Uploaded by

Vishesh Aggarwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Computer Science

Assignment-5 : Lists
~By- Vishesh Aggarwal
I. Program-1
#1. Menu Based Program
print("Curretly, list is empty.")
print("LIST OPPERATIONS : ")
print("1. Append an element")
print("2. Insert an element at the desired position")
print("3. Append a list to the given list")
print("4. Modify an existing element ")
print("5. Delete an existing element by its position")
print("6. Delete an existing element by its value ")
print("7. Sort the list in ascending order ")
print("8. Sort the list in descending order ")
print("9. Display the list ")
print("10. Exit")
opp=0
mylist=[]
while True:
opp=int(input("\nEnter your Choice(1-10) : "))
if opp==1:
el=int(input("Enter element to add."))
mylist.append(el)
print("The list has been appended.")

elif opp==2:
pos=int(input("Enter the position : "))
el=int(input("Enter element to add :"))
mylist.insert(pos-1,el)
print("The element is inserted.")

elif opp==3:
newlist=eval(input("Enter new list (seperated by commas) : "))
mylist.extend(newlist)
print("The new list has been added.")

elif opp==4:
pos=int(input("Enter the position : "))
el=int(input("Enter element to replace/modify :"))
old_value,mylist[pos-1]=mylist[pos-1],el
print("The list is modified.",old_value,"is removed.")

elif opp==5:
pos=int(input("Enter the position : "))
old_value=mylist[pos-1]
mylist.pop(pos-1)
print("The list is modified.",old_value,"is removed.")

elif opp==6:
el=int(input("Enter element to remove : "))
mylist.remove(el)
print("The list is modified.",old_value,"is removed.")

elif opp==7:
mylist.sort()
print("The list is in ascending order, now.")

elif opp==8:
mylist.sort(reverse = True)
print("The list is in descending order, now.")

elif opp==9:
print("The List is : ", mylist)
elif opp==10:
break
else:
print("Invalid Input")

Output:
Curretly, list is empty.
LIST OPPERATIONS :
1. Append an element
2. Insert an element at the desired position
3. Append a list to the given list
4. Modify an existing element
5. Delete an existing element by its position
6. Delete an existing element by its value
7. Sort the list in ascending order
8. Sort the list in descending order
9. Display the list
10. Exit

Enter your Choice(1-10) : 67


Invalid Input

Enter your Choice(1-10) : 3


Enter new list (seperated by commas) : 478,96,63,32
The new list has been added.

Enter your Choice(1-10) : 6


Enter element to remove : 63
The list is modified. 63 is removed.

Enter your Choice(1-10) : 9


The List is : [478, 96, 32]

Enter your Choice(1-10) : 10

II. Program-2
#2. Retrieve information from Given Student's Record List.
stRecord=['Raman','A-36',[56,98,99,72,69],78.8]
print("The Percentage is : ",stRecord[3])
print("Marks in 5th subject : ",stRecord[2][4])
print('Maximum marks : ',max(stRecord[2]))
print('Roll Number : ',stRecord[1])
stRecord[0]='Raghav'
print('Name is changed to Raghav.')
print("The Record: ",stRecord)
Output:
The Percentage is : 78.8
Marks in 5th subject : 69
Maximum marks : 99
Roll Number : A-36
Name is changed to Raghav.
The Record: ['Raghav', 'A-36', [56, 98, 99, 72, 69], 78.8]

III. Program-3
#3. Find of times, an element occur.
newlist=eval(input("Enter new list (seperated by commas) : "))
el=int(input("Enter element to count its occurences :"))
print("The number of occurences is",newlist.count(el))

Output:
Enter new list (seperated by commas) : 10,10,90,20,30,40,10
Enter element to count its occurences :10
The number of occurences is 3

IV. Program-4
#4. Take a list of n integers, segregate them in 2 list, one with positive & other with negat
ive.
newlist=eval(input("Enter new list (seperated by commas) : "))
p_list=[]
n_list=[]
for i in newlist:
if i>=0:
p_list.append(i)
else:
n_list.append(i)
print("The list of positive numbers :",p_list)
print("The list of negative numbers :",n_list)

Output:
Enter new list (seperated by commas) : 30,-10,78,-89,67
The list of positive numbers : [30, 78, 67]
The list of negative numbers : [-10, -89]

V. Program-5
#5. Return largest element of list.
newlist=list(eval(input("Enter new list (seperated by commas) : ")))
print('The largest element of list : ',max(newlist))

Output:
Enter new list (seperated by commas) : 56,45,87,89,23,43
The largest element of list : 89

VI. Program-6
#6. Find Second Largest No. from list.
newlist=list(eval(input("Enter new list (seperated by commas) : ")))
newlist.sort()
print('The second largest element of list : ',newlist[-2])

Output:
Enter new list (seperated by commas) : 23,56,43,76,45,90,102
The second largest element of list : 90

VII. Program-7
#7. Find median from n integers.
newlist=list(eval(input("Enter new list (seperated by commas) : ")))
newlist.sort()
i=len(newlist)
if i%2==1:
median=newlist[(i+1)//2-1]
elif i%2==0:
med=newlist[i//2]
medi=newlist[i//2-1]
median=(med+medi)/2
print("The median is",median)
Output:
Enter new list (seperated by commas) : 65,78,90,35,67,23
The median is 66.0

VIII. Program-8
#8. Delete mutiple occurences.
newlist=list(eval(input("Enter new list (seperated by commas) : ")))
for i in newlist:
if newlist.count(i)!=1:
newlist.remove(i)
print("New Modified List: ",newlist)

Output:
Enter new list (seperated by commas) : 45,65,78,65,42,45
New Modified List: [78, 65, 42, 45]

IX. Program-9(A)
#9. a) Delete element of given position.
def del_pos(mylist,pos):
old_value=mylist[pos-1]
mylist.pop(pos-1)
print(old_value,"is removed.")
return mylist
newlist=list(eval(input("Enter new list (seperated by commas) : ")))
pos=int(input("Enter the position : "))
del_pos(newlist,pos)

Output:
Enter new list (seperated by commas) : 45,67,89,23,45,35
Enter the position : 3
89 is removed.
[45, 67, 23, 45, 35]
X. Program-9(B)
#9. b) Delete specified value from list.
def del_el(mylist,el):
mylist.remove(el)
return mylist
newlist=list(eval(input("Enter new list (seperated by commas) : ")))
el=int(input("Enter the element : "))
del_el(newlist,el)

Output:
Enter new list (seperated by commas) : 45,98,34,56,24,64
Enter the element : 34
[45, 98, 56, 24, 64]

XI. Program-10
#10. Reverse the string without creationg a new list.
newlist=list(eval(input("Enter new list (seperated by commas) : ")))
newlist.reverse()
print(newlist)

Output:
Enter new list (seperated by commas) : 23,56,78,90,53
The Modified List : [53, 90, 78, 56, 23]

You might also like