Skip to content

Instantly share code, notes, and snippets.

@shinysu
Last active November 5, 2020 14:09
Show Gist options
  • Save shinysu/ec3c5af9c62aa17d4473ca899bb40837 to your computer and use it in GitHub Desktop.
Save shinysu/ec3c5af9c62aa17d4473ca899bb40837 to your computer and use it in GitHub Desktop.
Dictionaries
'''
A sample dictionary
'''
birthdays = {'Arun': '01-09-2002', 'Ben': '08-12-2001', 'Cathy': '21-04-2001'}
print(birthdays)
print(type(birthdays))
print(len(birthdays))
'''
A birthday calendar using dictionary where you can search a name and get the birthday of the person
Concept : how to get a value from the dictionary by using the key
'''
birthdays = {'Arun': '01-09-2002', 'Ben':'08-12-2001', 'Cathy':'21-04-2001'}
print("Birthday calendar")
name = input("Enter the name: ")
print(birthdays[name])
'''
Prints all the names, birthdays from the birthday calendar
concepts - how to get the keys, values from a dictionary
'''
birthdays = {'Arun': '01-09-2002', 'Ben':'08-12-2001', 'Cathy':'21-04-2001'}
names = birthdays.keys()
dates = birthdays.values()
print(list(names))
print(list(dates))
for key, value in birthdays.items():
print(key, value)
'''
A contactbook app where you store the contact names and phone numbers using dictionary
Add a new contact into the contactbook
'''
def add_contacts():
name = input("Enter the name: ")
number = int(input("Enter the phone number"))
contactbook[name] = number
contactbook = {'Arun': 999999, 'Ben':888888}
add_contacts()
print(contactbook)
'''
A contactbook app where you store the contact names and phone numbers using dictionary
delete a contact from the contactbook
'''
def delete_contact():
name = input("Enter the name of the contact to be removed: ")
if name in contactbook:
del contactbook[name]
else:
print("Contact is not found")
contactbook = {'Arun': 999999, 'Ben':888888}
delete_contact()
print(contactbook)
'''
A contactbook app where you store the contact names and phone numbers using dictionary
Edit the phone number of a contact
if the name is not found, then it will add a new contact with that name
'''
def edit_contact():
name = input("Enter the name of the contact to be edited: ")
new_number = int(input("Enter the new phone number"))
contactbook[name] = new_number
contactbook = {'Arun': 999999, 'Ben':888888}
edit_contact()
print(contactbook)
'''
A quiz app where questions and answers are stored in a dictionary
The key for the dictionary is the question and the value is the list of options to be given to the user
'''
quiz = {
'What is the capital of India': ['Chennai', 'Delhi', 'Mumbai'],
'What is the national bird of India': ['peacock', 'pigeon', 'parrot'],
'What is the national animal of India': ['Lion','Cheetah', 'Tiger']
}
for questions, answers in quiz.items():
print(questions)
print(answers)
'''
prints the questions and the options
'''
quiz = {
'What is the capital of India': ['Chennai', 'Delhi', 'Mumbai'],
'What is the national bird of India': ['peacock', 'pigeon', 'parrot'],
'What is the national animal of India': ['Lion','Cheetah', 'Tiger']
}
for questions, answers in quiz.items():
print(questions)
for word in answers:
print(word)
'''
include the correct answer as the last value in the answer list
get the answer input from the user and check if it matches with the lat value in the answer list
'''
quiz = {
'What is the capital of India': ['Chennai', 'Delhi', 'Mumbai', 'delhi'],
'What is the national bird of India': ['peacock', 'pigeon', 'parrot', 'peacock'],
'What is the national animal of India': ['Lion', 'Cheetah', 'Tiger', 'tiger']
}
for questions, answers in quiz.items():
print(questions)
for word in answers:
print(word)
user_input = input()
if user_input == answers[-1]:
print("Correct")
else:
print("Wrong answer")
'''
change the for loop that prints the answer options to use range(), so that the last value in the list(the correct answer)
is not printed
included lower() method to convert user input to lower case.
Remember, the correct answer that you give as last value in the answer list should also be in lower case
'''
quiz = {
'What is the capital of India': ['Chennai', 'Delhi', 'Mumbai', 'delhi'],
'What is the national bird of India': ['peacock', 'pigeon', 'parrot', 'peacock'],
'What is the national animal of India': ['Lion', 'Cheetah', 'Tiger', 'tiger']
}
for questions, answers in quiz.items():
print(questions)
for i in range(len(answers)-1):
print(answers[i])
user_input = input().lower()
if user_input == answers[-1]:
print("Correct")
else:
print("Wrong answer")
'''
Count the occurance of each letter in the given string.
Eg:- s occurs 8 times, h - 4 times etc
'''
line = 'she sells sea shells in the sea shore'
lettercount = {}
for x in line:
if x in lettercount:
lettercount[x] = lettercount[x] + 1
else:
lettercount[x] = 1
print(lettercount)
'''
A program to store the length of the words of a string in a dictionary
key will be the word and value will be the length of the word
'''
line = 'A quick brown fox jumped over the lazy dog'
words = line.split()
wordlength = {}
for x in words:
wordlength[x] = len(x)
print(wordlength)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment