Skip to content

Latest commit

 

History

History
172 lines (137 loc) · 5.02 KB

dictionaries.md

File metadata and controls

172 lines (137 loc) · 5.02 KB

Imagine that we will create a simple PostalPH bot that allows a user to ask his/her postal code giving the bot his/her area.

PostalPHBot: Hello! What is your area?
Goblin: binondo
PostalPHBot: Hi! Your postal area is 1006.

How will you store the ff. mapping in Python?

Binondo: 1006
Intramuros: 1002
Malate: 1004
Ermita: 1000
Paco: 1007
Inagi-shi: 13225
Hino-shi: 13212
and many more...

Hmmm.... with a loooong conditional statements like this?

area = input('Hello! What is your area?').lower()

if area == 'binondo':
    print('Hi! Your postal area is 1006.')
elif area == 'intramuros':
    print('Hi! Your postal area is 1004.')
# and a lot more if-else!

Creation and Value Access

Good thing, Python allows us to store collections for mapping or associating a key with a value, just like how a dictionary allows us to search for a word(the key) and its meaning(the value)! Python dictionaries are collections just like lists but instead of having numbers as its index, we can use strings, numbers or tuples instead. Let's try to use dictionary to our postal bot!

postal_mapping = {
    'binondo': 1006,
    'intramuros': 1002,
    'malate': 1004,
    'ermita': 1000,
    'paco': 1007,
    'inagi-shi': 13225,
    'hino-shi': 13212
}

area = input('Hello! What is your area?').lower()

if area in postal_mapping:
    print('Hi! Your postal area is {}'.format(postal_mapping[area]))
else:
    print('Sorry! The area you provided was not found on our records.')

We created a dictionary by enclosing in braces({}) the key-value pairs which are separated by colon(:).

See how we accessed the dictionary value? We used strings(the area) as the dictionary key and used that key to retrieve the postal value.

your_dictionary[key]

Why did we check first if the area is in our dictionary? See what will happen if we have the code below and we input 'makati' to our postal bot.

postal_mapping = {
    'binondo': 1006,
    'intramuros': 1002,
    'malate': 1004,
    'ermita': 1000,
    'paco': 1007,
    'inagi-shi': 13225,
    'hino-shi': 13212
}

area = input('Hello! What is your area?').lower()
# We removed the checking. YOLO!
print('Hi! Your postal area is {}'.format(postal_mapping[area]))

Dictionary update and methods

Postal Bot update! Let's add Quaipo to our dictionary and also, remove the odd Inagi-shi and Hino-shi as we found out it was for PostJP bot not PostPH bot!

postal_mapping = {
    'binondo': 1006,
    'intramuros': 1002,
    'malate': 1004,
    'ermita': 1000,
    'paco': 1007,
    'inagi-shi': 13225,
    'hino-shi': 13212
}

# Let's add Quiapo
postal_mapping['quaipo'] = 1001

# Let's delete Inagi-shi
del postal_mapping['inagi-shi']

# Dictionaries have methods too!
# Let's delete Hino-shi using the pop method
deleted_value = postal_mapping.pop('hino-shi')
print('Removed {} from mapping'.format(deleted_value))

# Dictionaries have functions too!
# Let's print its length using the len function
print('We now have a total of {} postal mapping'.format(len(postal_mapping)))

Challenge

Good job! You are nearly complete in knowing some basic programming concepts using Python! Let's get our fingers and mind working!

Just like in grocery stores where items where scanned one by one and the total amount due is summed, please help Aling Nena's Sari-sari store to compute a customer's total order amount and print the receipt by:

  • Input the customer's products
  • The product dictionary contains Aling Nena's products and maps it's price.
  • If the product name is NOT in her list, just print 'Item void!' and skip it from the computation of total amount due.
  • If the product is in the list, please print current purchased products and their quantity.
  • If done punching the items:
    • Print each purchased products, it's quantity, price, total price (quantity * price)
    • Print the total items and total amount due.

Example:

>> Item name:  sunsilk
{'sunsilk': 1}
>> Are you done? Press y to finish: n
>> Item name:  SUNsilk
{'sunsilk': 2}
>> Are you done? Press y to finish: n
>> Item name:  ligo
{'sunsilk': 2, 'ligo': 1}
>> Are you done? Press y to finish: n
>> Item name:  Spam
{'sunsilk': 2, 'ligo': 1, 'spam': 1}
>> Are you done? Press y to finish: n
>> Item name:  test
Item test void!
>> Are you done? Press y to finish: n
>> Item name:  ligo
{'sunsilk': 2, 'ligo': 2, 'spam': 1}
>> Are you done? Press y to finish: y
***** PRINTING YOUR RECEIPT *****
sunsilk 7.5 * 2 = 15.0
ligo 12.0 * 2 = 24.0
spam 115.25 * 1 = 115.25
5 items --------- 154.25

!> Please use below template

dictionaries01

challenge_partial

Put your thinking cap on!

  • Is there a way to avoid a KeyError when using pop() to an non-existent key?
  • What are the allowed keys of a dictionary? What attribute do they share in common?
  • How do you check if a certain key exists in a dictionary?
  • How do you loop through a dictionary, getting both it's key and value pair?