5 Write a program thatinputs
a list, it
replicates
twiceand
thenprints i
the sorted list ascending and deescending
am
ordeis.
("Enter
(input
val = eval a :"))
list
print list
("Original :",val)
val= val* 2
("
print list val)
Replicated :",
val.sort()
inascendingorder:",
("Sorted
print val)
Enteralist 11,29]
:[23,
:[23,11,29]
list
original
Replicated
Sorted in
list:
[23,11,
29, 23, 11,
29]
ascendingorder:
[11, 11,
6.8 Program to calculate
the mean of a givenlist
of numbers.
lst
= eval(input : "))
1ist
("Enter
rogram
length= len(1st)
mean = Sum = 0
i
for inrange(0, length):
sum += lst
[i]
mean = sum /length
("Given
print is:",
list lst)
print("The is:",
mean of thegivenlist mean)
Enter
Given
:
list[7,23, -1l,
1istis [7,:
55, 13.5,20.05, -5.5]
-11,55, 13.5,20.05, -5.5]
23,
The mean is:14.578571428571427
of the given1ist
6.9 Program to searchforan element ina givenlist
of numbers.
lst list
= eval(input("Enter:"))
rogram
length= len(lst)
element= int(input elementtobe searched for:"
("Enter
i
for inrange(0,
length)
:
ifelement lst[i]
: ==
"foundatindex",
(element,
print i)
break
else : #elseofforloop
print "notfoundingivenlist")
(element,
Two sample runsofabove programare beinggivenbelow :
Enter 1ist
: [2,8, 9, 11,-55, -11,22, 78, 67]
Enterelement to be searched for: -11
-11found at index5
Enter 1ist : [2,8, 9. 11,-55, -11, 22, 78, 67]
Enter element to be searched for : -22
-22 not found in given 1ist
39. Writea menu-drivenprogram to performvariouslist
operations:
(a)Append
(b)Insert
toanotherlist
(c) Append a list
(d) Modifyelement
(e) Delete elementfrom givenposition
existing
Ans. 1l)
()Deleteexisting elementwithgivenvalue
"))
("Howmany elementsyou want to enter:
n=int(input
print elements")
("Enter
for iinrange(n)
:
ele-int
(input())
11.append(ele)
print("Theentered is: list ",11,
'\n')
whileTrue:
print ("\nLIST
OPERATIONS")
("1.
print Append an element")
print
("2.Insert position")
an elementat desired
("3.Append a list
print togivenlist")
print Modifyan existing
(""4. element")
element
existing by its
position")
("5.
print Delete itsvalue"
elementby
)
print("6.Deleteexisting
Exit\n")
("7.
print
("Enter
(input
choice=int
"))
your choice (1-7) :
ifchoice-1: theelement to be appended:
"))
("Enter
(input
element-int
(element)
1l.append
:",ll,"\n)
print("New list
elif
choice=m2:
be "))
inserted:
the elementto
("Enter
(input
element=int
point(input("Enterthe position:"))
11.insert
(pos,element)
print("New list:", l1,"\n')
elif choice==3:
new list=list(eval the list
("Enter
(input to be appended: ")
) )
11.extend(new list)
print ("New list:",l1,'\n')
elifchoice==4:
pos=int(input ("Enter of element to be modified:
position "))
new (input("Enternew element:") )
element=int
list
[pos]=new element
("New list:
print ",11,'\n')
elif
choice==5:
(input
pos=int position
("Enter "))
of elementto be deleted:
11.pop
(pos)
print
("New list:",l1,
'\n')
elif
choice==6:
("Enter
(input
element=int "))
elementto be deleted:
11.remove
(element)
("Newlist:
print ",11,
'\n')
elifchoice==7:
break
else:
print
("Invalid
choice")