You are on page 1of 6

1a) To display the list elements (in reverse order, even,

odd position elements)

CODE
list1=[1,2,3,4,5,6]

print(list1[::-1])

print(list1[::2])

print(list1[1::2])

OUTPUT
[6, 5, 4, 3, 2, 1]

[1, 3, 5]

[2, 4, 6]

#c) append, sort, search an elements in a list

code
list1= [1, 2, 3, 4, 10, 12, 5, 6, 7]

list1.sort()

print(list1)

i=int(input("enter the element to append:"))

print("list befor append")

print(list1)

list1.append(i)

print ("list after appending")

print(list1)

n=int(input("enter the element to search:"))

if (n in list1):

print("element is found")

else:
Print ("element is not found")

OUTPUT
[1, 2, 3, 4, 5, 6, 7, 10, 12]
Enter the element to append: 13
List before append
[1, 2, 3, 4, 5, 6, 7, 10, 12]
List after appending
[1, 2, 3, 4, 5, 6, 7, 10, 12, 13]
Enter the element to search: 11
Not found

To remove duplicate elements from a


list.
Code
list1= [ ]
list2= [ ]
n=into (input ("range"))
for I in range (n):
list1.append(int(input()))
for i in (list1):
if i not in(list2):
list2.append(i)
print(list2)
OUTPUT
range10
1
1
32
2
2
3
3
5
6
6
[1, 32, 2, 3, 5, 6]
E,To count the no. of times an element occurs in a
list.
code
list1=[]
n=int(input("range"))
for i in range(n):
list1.append(int(input()))
o=int(input("enter the element to check occurence"))
c=0
for i in range (len(list1)):
if o==list1[i]:
c+=1
print(o,"occured",c,"times")
OUTPUT
range10
1
1
1
1
1
2
1
31
1
2
enter the element to check occurence1
1 occured 7 times
a) To find the length of the elements, minimum and
maximum values in a list
CODE
list1=[]
n=int(input("range"))
for i in range (n):
list1.append(int(input()))
print("the length",len(list1))
print("the maximum of",max(list1))
print("the minimum no",min(list1))
OUTPUT
range5
1
5
2
52
1
the length 5
the maximum of 52
the minimum no 1
(2-a)To sort given list of elements
code
list1=[]
n=int(input("range"))
for i in range (n):
list1.append(int(input()))
for i in range(len(list1)):
for j in range(i + 1, len(list1)):
if list1[i] > list1[j]:
temp=list1[i]
list1[i]=list1[j]
list1[j]=temp
print(list1)
OUTPUT
range5
1
5
6
4
2
[1, 2, 4, 5, 6]
(2-b)To search the given number in a list
CODE
list1=[]
n=int(input("range"))
for i in range (n):
list1.append(int(input()))
v=int(input("enter the element to search"))
for i in list1:
if i==v:
print(v,"element is found at ",i)
print(list1)
OUTPUT
range5
1
2
3
4
5
enter the element to search5
5 element is found at 5
[1, 2, 3, 4, 5]

(2-C)to find Mean, Variance and Standard Deviation for a list of numbers.

CODE
import math
list1=[]
n=int(input("range"))
for i in range(n):
list1.append(int(input()))
while(1):
option=int(input("enter the option 1.mean\
n2.variance\n3.standard deviation\n"))
if(option==1):
sum=0
for i in range(0,len(list1)):
sum=sum+list1[i]
mean=sum/n
print(mean)
elif(option==2):
variance=0
for i in range(0,n):
variance+=((list1[i]-mean)**2)
variance1=variance/n
print(variance1)
elif(option==3):
deviation=math.sqrt(variance1)
print(deviation)
else:
break
OUTPUT
range5
1
2
3
4
5
enter the option 1.mean
2.variance
3.standard deviation
1
3.0
enter the option 1.mean
2.variance
3.standard deviation
2
2.0
enter the option 1.mean
2.variance
3.standard deviation
3
1.4142135623730951
enter the option 1.mean
2.variance
3.standard deviation

You might also like