Professional Documents
Culture Documents
-> immutable in nature, you cannot change contents once defined like string.
-> you cannot append, extend, insert, del, pop anything
-> 10 times faster in accessing elements as compared to lists
-> storing imp information - adhaar card, SSN, passport no
lists []
tuples ()
question :-
1. create dictionary having keys as emp ids and names as emp names
till the time user enters -999 for emp id
{201:'Amit',202:'Sumit',203:'AMIT', 204:'Suman'}
3. take emp name from user and print all emp ids in a form of list
if emp name does not exist print not found. name search should be case
insensitive
empid = 0
d= {}
while empid!= -999:
empid = int(input("Enter the empid:"))
if empid != -999:
empname = input("Enter the emp name:")
d[empid] = empname
print(d)
list1 = []
flag = 0
emname = input("Enter the employee name to search:")
for k,v in d.items():
if v.upper() == emname.upper():
list1.append(k)
flag=1
if flag ==0:
print("Employee not found", list1)
else:
print(list1)
-----------------------------------------------------------------------
functions
----------------------------------------------------------------------
functions can be reused again and again.
make our code more modular
we need to define function first then only call it
question
---------------------------
write a function take_input to take input radius of a circle and return to main
write a function area to return area of a circle
write a function perimeter to return perimeter of a circle
import math
def fun():
rad=int(input("Enter Radius: "))
return rad
def area(rad):
return math.pi * rad * rad
def perimeter(rad):
return 2*math.pi*rad
r=fun()
ar=area(r)
pr=perimeter(r)
print("area is ",ar)
print("perimeter is ", pr)
--------------------------------------------------------
write a function to take an input from the user CP and SP and return to main
program
write another function which should calculate profit /loss / no profit no loss
along with amount of profit /loss
and return it
in the main program, print amount of profit or loss or no profit no loss
def input_price():
cp = int(input('Enter CP'))
sp = int(input('Enter SP'))
return cp,sp
def prof_loss(price):
cp=price[0]
sp=price[1]
if cp > sp:
return "Loss",cp-sp
elif sp>cp:
return "Profit",sp-cp
else :
return "No Profit, No loss",0
price = input_price()
profit_loss = prof_loss(price)
print(profit_loss[0],profit_loss[1])
-----------------------------------------------------------------------------------
---------
write a program of guessing game.
if user enters number which is odd, divisible by 3 and divisible by 5, you win the
game
you will get 3 chances to reinter your input
after 3 failed attempts print message "lost the game"
do not return any value from function instead main program should print Win or Lost
the game
win=0
def game():
global win
count=1
while count<=3 and win==0:
print("Attempt", count)
num=int(input("please enter a number:"))
if num%3==0 and num%5==0:
win=1
count+=1
game()
if win==0:
print("you lost the game")
else:
print("you won the game")
Home