5,6,7,8

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

5.

Shopping
AIM:
To define a function that uses dictionary to store the items and displays item lists.
SOURCE CODE:
lib={'Varun':['Apple','Tomato'],'Ram':['Phone','Computer']}
def addlib():
bname=input('Enter customer name:')
l=[]
n=int(input('Enter number of items:'))
for i in range(n):
n=input("Enter the items:")
l.append(n)
lib[bname]=l
def dislib():
global lib
try:
print('Items')
for i in lib:
print(lib[i])
except EOFError:
pass

addlib()
dislib()
6. Roots of Quadratic Equation
AIM:
To define a function that takes values a,b,c to find roots of a quadratic equation and
displays the roots by importing math module.
SOURCE CODE:
import math
def eqroots(a,b,c):
dis=b*b-4*a*c
sqrt_val=math.sqrt(abs(dis))
if dis>0:
print("Real and different roots")
print((-b+sqrt_val)/(2*a))
print((-b-sqrt_val)/(2*a))
elif dis==0:
print("Real and equal roots")
print(-b/(2*a))
else:
print("Complex roots")
print(-b/(2*a),"+i",sqrt_val)
print(-b/(2*a),"-i",sqrt_val)
eqroots(1,2,3)
7. Lottery
AIM:
To define a function that generates a random number for a dice been thrown and
display the result accordingly. Use dictionary to save dice combination and result by
importing random module.
SOURCE CODE:
dict1={}
import random
def lottery():
global dict1
key=input('Enter the key:')
n=int(input('max no. of times a dice is thrown:'))
x1=[]
b=random.randint(1,n)
for i in range(b):
x=random.randint(1,6)
x1.append(x)
dict1[key]=x1
print(dict1)
lottery()
8. Population Survey
AIM:
To define a function that lists a city population and display median and mean
population by importing statics module.
SOURCE CODE:
import statistics
def mean_and_median(n):
x=statistics.median(n)
y=statistics.mean(n)
print('Median Population',x)
print('Mean Population',y)
n=[270,255,249,300,309,259,287]
mean_and_median(n)

You might also like