Skip to main content

DICTIONARY PRACTICE QUESTIONS - 13-11-24

 

DICTIONARY PRACTICE QUESTIONS

1. Create a dictionary with the Rollno, Name and marks of students and display the name of students, who have scored marks above 75.

Sol:

# Input the number of students

no_of_std = int(input("Enter number of students: "))

result = {}

# Loop to enter details of each student

for i in range(no_of_std):

    print("Enter Details of student No.", i + 1)

    roll_no = int(input("Roll No: "))           # Input Roll Number

    std_name = input("Student Name: ")          # Input Student Name

    marks = int(input("Marks: "))               # Input Marks

    result[roll_no] = [std_name, marks]         # Store details in dictionary

# Print the dictionary with all students' details

print(result)

# Display names of students who have got marks more than 75

print("Students who scored more than 75 marks are:")

for student in result:

    if result[student][1] > 75:                 # Check if marks > 75

        print(result[student][0])               # Print the student's name


2. Python program to convert a number entered by the user into its corresponding number in words Eg: 123  - one two three [NCERT]

SOL:

num = input("Enter any number: ")

numberNames = {0:'Zero',1:'One',2:'Two',3:'Three',4:'Four',\

 5:'Five',6:'Six',7:'Seven',8:'Eight',9:'Nine'}

result = ''

for ch in num:

     key = int(ch) #converts character to integer

     value = numberNames[key]

     result = result + ' ' + value

print("The number is:",num)

print("The numberName is:",result)

3. Write a menu-driven program to create a dictionary containing roll numbers as keys. The value part is a list with marks of 5 subjects, total marks, percentage, and grade. The grade is assigned based on the given criteria:

Grade Criteria:

  • > 90 → Grade A
  • >= 75 and < 90 → Grade B
  • < 75 → Grade C
SOL:

# -*- coding: utf-8 -*-
"""
Created on Tue Nov 12 09:47:02 2024

@author: Kirti Hora
"""

stud = {}  # Dictionary to store student details
n = int(input("Enter no. of students: "))

# Loop to enter student details
for i in range(n):
    dets = []  # List to store details of each student
    
    # Input student roll number
    rno = int(input("Enter roll no: "))
    
    # Input student name
    name = input("Enter name: ")
    dets.append(name)
    
    # Input 5 marks
    for m in range(1,6):
        marks = float(input("Enter marks for subject:"))
        dets.append(marks)
    
    # Calculating total and percentage
    tot = sum(dets[1:6])  # Sum of marks (index 1 to 5)
    percentage = (tot / 500) * 100
    
    # Adding total and percentage to the details list
    dets.append(tot)
    dets.append(percentage)
    
    
    if percentage >= 90:
        grade = "A"
    elif percentage >= 75:
        grade = "B"
    else:
        grade = "C"
    
    # Adding grade to the details list
    dets.append(grade)
    
    # Storing the student details in the dictionary with roll number as the key
    stud[rno] = dets

# Output the final student dictionary
print("\nStudent Details:")
for roll_no, details in stud.items():
     print("Roll No:",roll_no , "Details",details)


Q4.Write a program to input your friends’ names and their Phone Numbers and store them in the dictionary as the key-value pair. Perform the following operations on the dictionary: 
a) Display the name and phone number of all your friends 
b) Add a new key-value pair in this dictionary and display the modified dictionary 
c) Delete a particular friend from the dictionary 
d) Modify the phone number of an existing friend 
e) Check if a friend is present in the dictionary or not 
f) Display the dictionary in sorted order of names

Sol:
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 13 08:16:26 2024

@author: Kirti Hora
"""
dic = {}

while True:
    print("1. Add New Contact")
    print("2. Modify Phone Number of Contact")
    print("3. Delete a Friend's contact")
    print("4. Display all entries")
    print("5. Check if a friend is present or not")
    print("6. Display in sorted order of names")
    print("7. Exit")
    
    inp = int(input("Enter your choice (1-7): "))

    # Adding a contact
    if inp == 1:
        name = input("Enter your friend's name: ")
        phonenumber = input("Enter your friend's contact number: ")
        dic[name] = phonenumber
        print("Contact Added\n")

    # Modifying a contact if the entered name is present in the dictionary
    elif inp == 2:
        name = input("Enter the name of the friend whose number is to be modified: ")
        if name in dic:
            phonenumber = input("Enter the new contact number: ")
            dic[name] = phonenumber
            print("Contact Modified\n")
        else:
            print("This friend's name is not present in the contact list\n")

    # Deleting a contact if the entered name is present in the dictionary
    elif inp == 3:
        name = input("Enter the name of the friend whose contact is to be deleted: ")
        if name in dic:
            del dic[name]
            print("Contact Deleted\n")
        else:
            print("This friend's name is not present in the contact list\n")

    # Displaying all entries in the dictionary
    elif inp == 4:
        print("All entries in the contact list:")
        if dic:
            for name in dic:
                print(name, "\t\t", dic[name])
        else:
            print("No contacts available.")
        print("\n")

    # Searching a friend's name in the dictionary
    elif inp == 5:
        name = input("Enter the name of the friend to search: ")
        if name in dic:
            print("The friend", name, "is present in the list with contact number:", dic[name], "\n")
        else:
            print("The friend", name, "is not present in the list\n")

    # Displaying the dictionary in sorted order of the names
    elif inp == 6:
        print("Name\t\t\tContact Number")
        for name in sorted(dic.keys()):
            print(name, "\t\t\t", dic[name])
        print("\n")

    # Exit the while loop if user enters 7
    elif inp == 7:
        print("Exiting the program. Goodbye!")
        break

    # Displaying the invalid choice when any other values are entered
    else:
        print("Invalid Choice. Please try again\n")


Comments

Popular posts from this blog

PYTHON - MYSQL CONNECTIVITY CODE

  #INSERTION OF DATA import mysql.connector mydb = mysql.connector.connect( host="localhost", user="root", passwd="root", database="school" ) print("Successfully Connected") #print(mydb) mycursor=mydb.cursor()   v1=int(input("enter ID:")) v2=input("enter name:") v3=input("enter Gender:") v4=int(input("enter age:")) sql='insert into TEACH values("%d","%s","%s","%s")'%(v1,v2,v3,v4) print(sql) mycursor.execute(sql) mydb.commit() print("record added") #MYSQL Connection code – Deletion on database SOURCE CODE: s=int(input("enter id of TEACHER to be deleted:")) r=(s,) v="delete from TEACH where id=%s" mycursor.execute(v,r) mydb.commit() print("record deleted") MYSQL Connection code – Updation on database SOURCE CODE: import mysql.connector mydb = mysql.connector.c...

GRADE XII CS - VIVA QUESTIONS

  VIVA QUESTIONS GRADE XII CS Dear All Be thorough with your project and practical files, as the viva can be asked from anywhere. Stay calm, don’t get nervous, and be confident in front of the examiner. 1. Tell me about your project. 2. Which concepts you have used for your project? 3. What do you mean by front end and back end? How they are important in developing any such projects? 4  Mention the modules and built-in functions you have used in your project. 5. Which real world problems are solved by your project? 6. Explain the most important feature of your project. 7. Name a few mutable data types of python. Lists, Sets, and Dictionaries 8. Name a few immutable data types of python. Strings, Tuples, Numeric 9. Name ordered and unordered data type of python. Ordered – String, List, Tuples Unordred – Set, Dictionaries 10. What is the significance of a pass statement in python? pass is no operation python statement. This is used where python requires syntax but logic requires...