Python coding To make dictionary

 

CODE MASTERS CLASS Dictionary




 PYTHON CODING 



#In this we will learn more programming on dictionary data type

Q.Write a program to print dictionary to find maximum and minimum value of the key and sum of the key values.


Testcase:1

INPUT
n=2
R=12,2
M=100, 90


OUTPUT:
{12:100,2:90}
Maximum values of the keys :12
Minimum values of the keys :2
sum of the values :100+90=190


""""WAP to print dictionary to find maximum and minimum value of the key and sum of the key values
"""
n=int(input("Enter  a number of friends: "))
dict={ }
for i in range(n):
    R=eval(input("Enter a roll number of the students:"))
    M=eval(input("Enter a marks of the students:"))
    dict[R]=M
print(dict)
m=max(dict)
n=min(dict)
sum=0
for keys in dict:
    sum=sum+dict[keys]



print("Maximum values of the key is:",m)
print("Minimum values of the keys is :",n)
print("sum of the values",sum)


#Let understand to initialize the program and to create a dictionary always input number of elements

n=int(input("Enter  a number of friends: "))
#Now Take empty dictionary
dict={ }
#To iterate use for loop to go this loop  upto number of elements
for i in range(n):

#First time loop run and take input of roll no. and marks of single students
    R=eval(input("Enter a roll number of the students:"))
    M=eval(input("Enter a marks of the students:"))
#Then assign the marks of the student and similary this loop will run and ask for roll no. and its marks
    dict[R]=M #through this it will individually assign the marks 

#To print dictionary use print()  function  
print(dict)
#usemax() function to find the key that has maximum value
m=max(dict)
#usemin() function to find the key that has minimu value
n=min(dict)
#take sumvariable equal to 0 this is assigning operator
sum=0
#again take a  for loop 
for keys in dict:
#And calculate the sum of the values(marks) that is assign with it keys(roll no.)
    sum=sum+dict[keys]


#Then use print() function to print all the thing that you want
print("Maximum values of the key is:",m)
print("Minimum values of the keys is :",n)
print("sum of the values",sum)
                     
 




OUTPUT:



Steps have to follow:
1) First you have to think and have to know about keywords and python grammer to write the first line that no. of students.

Q.WAP To  which accept SalesAmount from user then calculate and print discount amount per following criteria 

In this only you have to test a condition that is given by the user so for that you have to follow only 2 steps:
1)First you must you have to use input function to take input from the user if it is nummerical value then use before input function like int() ,or float() or eval() functions.
2)Second step to check condition using if, elif and else structure,,after that you just have to use print() function. 

""""WAP To  which accept SalesAmount from user then calculate and print discount amount per following criteria :
 Sales Amount                       Discount 
                         Less than 1000                    5% of Sales Amount
                         1001 to 3000                       7% of sales Amount
                         More than 3000                  10% of Sales Amount

"""
print("1.*****SALE SALE SALE *****")

salesamount=int(input("Enter a sales amount:"))
discount=0
if salesamount<1000:
    discount=5/100*salesamount
elif 1001<= salesamount>3000 :
    discount=7/100*salesamount
else:
    discount=10/100*salesamount
print(discount,"Discount on your purchase of",salesamount)



OUTPUT:





Q.Write a Python program to input 10 student names and their marks in a dictionary as a key value pair then print the names of those students who secures marks more than 60


In this you have to learn that how to make a dictionary but if you do not know lnow so worry about it ,here you can learn again .To do this program Just follow these steps :

1)First to use input() function to take input that how many students or employee are there  and run the program until number of not over.To take name  write just or use only input() function and input the marks of the students .
2)Now you have to convert into a dictionary form ,so take name of the students as a key and theirs marks as a its value.

3)Write a always this shortcut trick that is (dict[name]=marks  ) you can change like instead of name you can write any other thing or you can change the name of the dictionary that here used dict.

4)After that again you have to use loop to test the condition and then use print()  function.

""""
 Write a Python program to input 10 student names and their marks in a dictionary as a key value pair then print the names of those students who secures marks more than 60

"""
n=int(input("Enter a number of students:"))
dict={ }
for i in range(0,n):
    name=input("Enter a name of the students")
    marks=int(input("Enter a mark of the students:"))
    dict[name]=marks
print("Dictionary of the students and their marks:",dict)

for keys in dict:                          # this is called a range in which it will go to every key value
    if dict[keys]>60:                      #here it checks every key value it means every students marks 
    
    



        print(keys,end=' '" got above 60 marks   and  " )
    
        


OUTPUT:



Q.Write a program in python to input five state names and their capital in a Dictionary as a Key Value pair, then ask the user to enter state and print its capital if found in a dictionary, otherwise print message “State not found in dictionary”








Testcase:1

INPUT:

n=2
state:gujarat,maharastra
capital=gandhinagar,mumbai
State=gujarat

OUPUT:
{"gujarat":"gandhinagar","maharastra":"mumbai"}

gandhinagar is the capital of gujarat

Testcase:2
INPUT:
n=4
state=punjab,orrisa,rajasthan,maharastra
capital=chandigarh,buvneshwar,jaipur,mumbai
State=rajasthan


OUTPUT:

{"punjab":"chandigarh","orrisa":"buvneshwar","rajasthan":"jaipur","maharastra":"mumbai"}

jaipur is the capital of rajasthan

---->This is program  given below

"""
Write a program in python to input five state names and their capital in a Dictionary as a Key Value pair, then ask the user to enter state and print its capital if found in a dictionary, otherwise print message “State not found in dictionary”

"""
n=int(input("Enter a number of states:"))
dict={ }
for i in range(0,n):
    state=input("Enter a name of the states:")
    capital=input("Enter a capital of the state::")
    dict[state]=capital
print("Dictionary of the students and their marks:",dict)
State=input("Enter a state name to get its capital:")
for key in dict:
    if State in dict:
        print(dict[State],"is the capital of ",State)
    else:
        print('Your state not found')

Copy the code by selecting 

#Let understand to initialize the program and to create a dictionary always input number of elements

n=int(input("Enter a number of states:"))

#Take empty dictionary
dict={ }
#To iterate use for loop to go this loop  upto number of elements
for i in range(0,n):

#First time loop run and take input the name of state
    state=input("Enter a name of the states:")
#Now use input() function to input capital of the state taht input
    capital=input("Enter a capital of the state::")

#Then assign the capital of that state comes and similary this loop will run and ask for state and its capital
    dict[state]=capital

#To print dictionary use print()  function
print("Dictionary of the students and their marks:",dict)
#If you want to saerch capital of state then take input of the name of the state
State=input("Enter a state name to get its capital:")

#now again take loop to search that element(state) name in dictionary
for key in dict:
#Now test the conditon ,and use memebership operator to check state name in dictionary  if the condition is true then print its capital
    if State in dict:
        print(dict[State],"is the capital of ",State)
    #and in the else use print() function to print whatever you want 
    else:
        print('Your state not found')
                     
 


                     
OUTPUT:








No comments:

Powered by Blogger.