0% found this document useful (0 votes)
2 views5 pages

Xia2 CH10 Lect6

The document contains programming exercises focused on list manipulation in Python. It includes programs for appending to a list, finding the minimum element and its index, calculating the mean of a list, and providing a menu for inserting or deleting elements from a list. Additionally, it features a program to search for an element within a list.

Uploaded by

gokubleachnigoi
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)
2 views5 pages

Xia2 CH10 Lect6

The document contains programming exercises focused on list manipulation in Python. It includes programs for appending to a list, finding the minimum element and its index, calculating the mean of a list, and providing a menu for inserting or deleting elements from a list. Additionally, it features a program to search for an element within a list.

Uploaded by

gokubleachnigoi
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/ 5

Chapter 6: List Manipulation

Lecture -6

PRG 6: Write a program that asks the user to input a number / a list to be
appended to an existing list. Whether the user enters a single number
or a list of numbers, the program should append the list accordingly.

LIST = [7, 90, 54]


print('_'*52)
print('Existing list is : ', LIST)
print('_'*52)
NEW = eval(input("Enter a number or a list to be appended:"))
if type(NEW) == type([]):
LIST.extend(NEW)
elif type(NEW) == type(1):
LIST.append(NEW)
else:
print(" Please enter either an integer or a list")
print('_'*52)
print('Appended list is :', LIST)
print('_'*52)

Output:

____________________________________________________
Existing list is : [7, 90, 54]
____________________________________________________
Enter a number or a list to be appended:[20,50,70]
____________________________________________________
Appended list is : [7, 90, 54, 20, 50, 70]
____________________________________________________

1
PRG 7: Program to find the minimum element from a list of elements along
with its index in the list.

print('_'*52)
LIST = list(eval(input("Enter a list elements:\n")))
length = len(LIST)
min_ele =LIST[0]
min_index = 0
for a in range(1,length):
if LIST[a] < min_ele:
min_ele =LIST[a]
min_index = a
print('_'*52)
print(f"Given list is: {LIST}")
print(f"The minimum element of the given list is: \
{min_ele} at index {min_index}")
print('_'*52)

Output:
____________________________________________________
Enter a list elements:
100, 500, -800, 700, 23
____________________________________________________
Given list is: [100, 500, -800, 700, 23]
The minimum element of the given list is: -800 at index 2
____________________________________________________

PRG 8: Program to calculate the mean of a given list of numbers.

print('\n','_'*52)
LIST = list(eval(input("Enter list elements:\n")))
length = len(LIST)
MEAN = Sum = 0

for a in range(0, length):


Sum += LIST[a]
MEAN = Sum / length
print('_'*52)
print(f"Given list is:\n {LIST}")
print(f"The MEAN of the given list is:\t {round(MEAN)}")
print('_'*52)

2
Output:
____________________________________________________
Enter list elements:
90, 78, 85, 82, 92
____________________________________________________
Given list is:
[90, 78, 85, 82, 92]
The MEAN of the given list is: 85
____________________________________________________

PRG 9: Program that displays options for inserting or deleting elements in a


list. If the user chooses a deletion option, display a submenu, and
ask if element is to be deleted with value or by using its position or
a list slice is to be deleted.

print('_'*52)
LIST = [18, 54, 90, 70, 10, 40, 80]
print('The list is:', LIST)
while True:
print("------------------- Main Menu------------------- ")
print('1. Insert')
print('2. Delete')
print('3. Exit')
ch = int(input("Enter your choice 1/2/3:"))
if ch == 1:
item = int(input('Enter item:'))
pos = int(input("Insert at which position?"))
index = pos -1
LIST.insert(index, item)
print(f'List now is : {LIST}')
elif ch == 2:
print(' ----------Deletion Menu ----------')
print('1. Delete using Value')
print('2. Delete using index')
print('3. Delete a sub list')
dch= int(input('Enter choice (1 or 2 or 3):'))
if dch == 1:
item = int(input('Enter item of item to be deleted:'))
LIST.remove(item)
print(f'List now is: {LIST}')
elif dch == 2:
index =int(input("Enter index of item to be deleted:"))
LIST.pop(index)
print("List now is :", LIST)
elif dch == 3:
lower =int(input("Enter lower limit of list slice to be deleted:"))

3
upper=int(input("Enter upper limit of list slice to be deleted:"))
del LIST[lower:upper]
print("List now is :",LIST)
else:
print("Valid choices are 1/2/3 only.")
elif ch == 3:
break;
else:
print("Valid choices are 1/2/3/ only.")
print('_'*52)

Output:

____________________________________________________
The list is: [18, 54, 90, 70, 10, 40, 80]
------------------- Main Menu-------------------
1. Insert
2. Delete
3. Exit
Enter your choice 1/2/3:1
Enter item:1000
Insert at which position?4
List now is : [18, 54, 90, 1000, 70, 10, 40, 80]
------------------- Main Menu-------------------
1. Insert
2. Delete
3. Exit
Enter your choice 1/2/3:2
----------Deletion Menu ----------
1. Delete using Value
2. Delete using index
3. Delete a sub list
Enter choice (1 or 2 or 3):1
Enter item of item to be deleted:10
List now is: [18, 54, 90, 1000, 70, 40, 80]
------------------- Main Menu-------------------
1. Insert
2. Delete
3. Exit
Enter your choice 1/2/3:2
----------Deletion Menu ----------
1. Delete using Value
2. Delete using index
3. Delete a sub list
Enter choice (1 or 2 or 3):2
Enter index of item to be deleted:5
List now is : [18, 54, 90, 1000, 70, 80]
------------------- Main Menu-------------------
1. Insert
2. Delete
3. Exit

4
Enter your choice 1/2/3:2
----------Deletion Menu ----------
1. Delete using Value
2. Delete using index
3. Delete sub list
Enter choice (1 or 2 or 3):3
Enter lower limit of list slice to be deleted:2
Enter upper limit of list slice to be deleted:5
List now is : [18, 54, 80]
------------------- Main Menu-------------------
1. Insert
2. Delete
3. Exit
Enter your choice 1/2/3:3
____________________________________________________

PRG 10:
Program to search for an element in a given list of numbers.

print('_'*52)
LIST = list(eval(input('Enter list:')))
length =len(LIST)
ele = int(input('Enter element to searched for :'))
print('_'*52)
for d in range(length):
if LIST[d] == ele:
print(ele, ' found at index', d)
break;
else:
print( ele,' not found in given list')
print('_'*52)

Output:
____________________________________________________
Enter list:45, 78, 58, 79, 98, 75
Enter element to searched for :58
____________________________________________________
58 found at index 2
____________________________________________________

You might also like