You are on page 1of 18

Q1.

Write a program that asks a user for a number of years and then print
number of days ,hours, minutes and seconds in that number of years.
CODE:
n=int(input('enter no of years:'))
d=n*365
h=d*24
s=h*60
print('no of days:',d)
print('no of hours:',h)
print('no of seconds:',s)
OUTPUT:
Q2. Write a program to display sum of squares of first 20 numbers.
CODE:
sum=0
for i in range(1,21):
sum=sum+i*i
print('sum of all squares:',sum)

OUTPUT:
Q3 Write a program to enter 10 numbers. Display the second largest number
from the entered numbers.
CODE:
a=[]
for i in range(10):
n=int(input('enter no:'))
a.append(n)
a.sort()
print('second largest no:',a[10-2])
OUTPUT:
Q4. Write python program to print first ten Mersenne numbers. Numbers in the
form 2n-1 are called Mersenne number.
CODE:
n=0
for i in range(11):
x=((2)**(n))-1
n+=1
print('mersenne',n,':',x)
OUTPUT:
Q5. Write python program to print the sum of given
sequence: 2/9-5/13+8/17…….(upto 7 term).
CODE:
n=2
d=9
m=1
sum=0
for i in range(7):
t=n/d
sum+=t*m
n+=3
d+=4
m*=-1
print('sum=',sum)
OUTPUT:
Q6. Write a menu driven program to calculate and display
perimeter/circumference and area of shapes such as triangle, rectangle, square
and circle.
CODE:
print('select:')
q=input('''enter p:to find perimeter
enter a:to find area''')
if q=='p':
s=input('''enter shapes,
t:triangle,r:rectangle,c:circle,s:square''')
if s=='t':
s1=int(input('enter side 1:'))
s2=int(input('enter side 2:'))
s3=int(input('enter side 3:'))
print('perimeter of triangle:',s1+s2+s3)
elif s=='r':
l=int(input('enter length:'))
w=int(input('enter width:'))
print('perimeter of rectangle:',2*(l+w))
elif s=='c':
r=int(input('enter radius:'))
print('perimeter of circle:',2*(3.14)*(r))
elif s=='s':
a=int(input('enter side:'))
print('perimeter of square:',4*a)
else:
print('not in menu')
elif q=='a':
s=input('''enter shapes,
t:triangle,r:rectangle,c:circle,s:square''')
if s=='t':
b=int(input('enter base:'))
h=int(input('enter height:'))
print('area of triangle:',(0.5)*(b)*(h))
elif s=='r':
l=int(input('enter length:'))
w=int(input('enter width:'))
print('area of rectangle:',(l)*(w))
elif s=='c':
r=int(input('enter radius:'))
print('radius of circle:',(3.14)*(r)**2)
elif s=='s':
a=int(input('enter side:'))
print('area of square:',a**2)
else:
print('not in menu')
OUTPUT:
Q7. Write a program to read list of elements.Modify the list by removing
duplicate elements, i.e., all elements occurring multiple times in the list should
appear only once.
CODE:
l=eval(input('enter a list:'))
for i in range(len(l)):
for j in l:
if l.count(j)>1:
l.remove(j)
l.sort()
print('new list:',l)
OUTPUT:
Q8. Write a program that asks the user to input a number a to be appended
to an existing list. Whether the user enters a single number or a list of
numbers, the program should append in the list accordingly.
CODE:
l=[1,2,3,4,5]
print('existing list:',l)
d=eval(input('enter list:'))
l.extend(d)
print(l)
OUTPUT:
Q9. Write a program to input a list of numbers and swap elements at the even
location with the elements at the odd location.
CODE:
str='abcdefghijklmnopqrstuvwxyz'
l=[]
c=1
for x in str:
l.append(x*c)
c+=1
print(l)
OUTPUT:
Q10. Write a python program to extract two list slices out of a given list of
numbers. Display and print the sum of elements of the first slice which contains
every second element of the list.
CODE:
x=[1,2,3,4,5,6,7,8,9,10]
odd=x[::2]
even=x[1::2]
a=sum(odd)
print('sum of first list:',a)
print('odd:',odd)
print('even:',even)
OUTPUT:
Q11. Write a program to create a dictionary with numbers as values. Display the
highest and lowest numbers from the values.
CODE:
d={'a':1,'b':2,'c':3,'d':4,'e':5}
v=d.values()
mx=max(v)
mi=min(v)
print('dictionary:',d)
print('max:',mx)
print('min:',mi)
OUTPUT:
Q12. Create a dictionary of friend’s name and their phone numbers. Write a
menu driven program to perform following:
a) Display name and phone numbers of all
b) Add a new key-value pair
c) Delete particular name
d) Modify the phone number of existingfriend
e) Check of a friend is present or not

CODE:
d={}
n=int(input('enter no of friend:'))
for i in range(n):
name=input('enter name:')
ph=int(input('enter phone no:'))
d[name]=ph
print('your friend dictionary:',d)
print('''select:
a:display name and phone no of
all b:add a new key value pair
c:delete particular name
d:modify the phone no of existing friend
e:check if a friend is present or not''')
q=input('enter:')
if q=='a':
print(d)
elif q=='b':
n=input('enter name:')
p=int(input('enter phone no:'))
d[n]=p
print('new dictionary:',d)
elif q=='c':
nm=input('enter name tobe deleted:')
if nm in d:
del d[nm]
print(nm,'deleted')
else:
print('not in dictionary')
elif q=='d':
nam=input('enter name to be modify:')
if nam in d:
ph=int(input('enter ph no:'))
d[nam]=ph
else:
print('not in dictionary')
elif q=='e':
name=input('enter name:')
if name in d:
print('yes present:')
else:
print('not present in list')
else:
print('not in menu')
OUTPUT:
Q13. Store details of 10 students in a dictionary at the same time.
Details include-rollno, name, marks ,grade etc.
CODE:
d={}
x=10
for i in range(x):
n=input('enter name:')
r=eval(input('enter roll.no:'))
m=eval(input('enter marks:'))
g=input('enter grade:')
print()
d[n]={'roll.no':r,'marks':m,'grade':g}
print(d)
OUTPUT:
Q14. Write a program to check if a dictionary is contained in another
dictionary.
e.g., ifd1 = {1:11, 2:12} and d2 = {1:11, 2:12, 3:13, 4:15}
Then d1 is contained in d2.
CODE:
x=0
d1=eval(input('enter dictionary:'))
d2=eval(input('enter dictionary:'))
print(d1,'d1')
print(d2,'d2')
for i in d1:
for j in d2:
if i==j:
x+=1
if x==len(d1):
print('d1 is contained in d2')
else:
print('d1 is not contained in d2')
OUTPUT:

You might also like