Ad Code

Responsive Advertisement

Basic Python Program - 39

Question 39: Write a Python program to find the highest 3 values in a dictionary.
Download hole Program / Project code, by clicking following link:
Write a Python Program using Dictionary to create a simple contact list dictionary and perform operation on it ?
Certainly! Here's a Python program that uses a dictionary to create a simple contact list. It allows you to add, view, and delete contacts:

In this program:
  • We initialize an empty dictionary called contacts to store contact information.
  • We define functions to add, view, and delete contacts from the dictionary.
  • Inside the main program loop, the user is presented with options to add, view, or delete contacts, or to exit the program.
  • The program takes user input and performs the corresponding operation.
  • It runs until the user chooses to exit the program.
You can run this program to create a simple interactive contact list manager using dictionaries.
# Initialize an empty dictionary to store contacts
contacts = {}

# Function to add a new contact to the dictionary
def add_contact(name, phone_number):
    if name not in contacts:
        contacts[name] = phone_number
        print(f"Contact '{name}' added successfully!")
    else:
        print(f"Contact '{name}' already exists!")

# Function to view a contact's phone number
def view_contact(name):
    if name in contacts:
        print(f"Contact: {name}")
        print(f"Phone Number: {contacts[name]}")
    else:
        print(f"Contact '{name}' does not exist!")

# Function to delete a contact
def delete_contact(name):
    if name in contacts:
        del contacts[name]
        print(f"Contact '{name}' deleted successfully!")
    else:
        print(f"Contact '{name}' does not exist!")

# Main program loop
while True:
    print("\nContact List")
    print("1. Add Contact")
    print("2. View Contact")
    print("3. Delete Contact")
    print("4. Exit")
    
    choice = input("Enter your choice (1/2/3/4): ")
    
    if choice == '1':
        name = input("Enter Contact Name: ")
        phone_number = input("Enter Phone Number: ")
        add_contact(name, phone_number)
    elif choice == '2':
        name = input("Enter Contact Name: ")
        view_contact(name)
    elif choice == '3':
        name = input("Enter Contact Name: ")
        delete_contact(name)
    elif choice == '4':
        print("Exiting Contact List.")
        break
    else:
        print("Invalid choice. Please select a valid option.")
        
 # Thanks for Reading.
Programming Code:
Following code write in: BP_P39.py
# Find highest 3 values in a Dictionary

# import lib.
from heapq import nlargest

# Create a dictionary
dict1 = {'a': 705, 'b': 546, 'c': 550, 'd': 400, 'e':250}

print("Dictionary 1 is: ", dict1)

# highest 3 values

three_values = nlargest(3, dict1, key= dict1.get)
print("Three largest values of Dictionary 1 is: ", three_values)

# Thanks for Reading.
Output:

Post a Comment

0 Comments

Ad Code

Responsive Advertisement