Xia2 CH10 Lect6
Xia2 CH10 Lect6
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.
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
____________________________________________________
print('\n','_'*52)
LIST = list(eval(input("Enter list elements:\n")))
length = len(LIST)
MEAN = Sum = 0
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
____________________________________________________
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
____________________________________________________