1. Dictionary with functions
1.1. Dictionaries
key: value
pairs.1.2. Dictionary structure
car_dictionary = {"brand": "Ford", "model": "Mustang", "year": 1965}
1.3. Looking up dictionary values by key
- value = dictionary[key]
- Get the value of a specific key in a dictionary.Raises an error if the key doesn’t exist, so a try-except block is needed.
Example:
car_dictionary = {"brand": "Ford", "model": "Mustang", "year": 1965}
# key exists
value = car_dictionary["brand"]
print(value)
# key doesn't exist
try:
value = car_dictionary["brands"]
print(value)
except KeyError:
print("not a valid key")
1.4. Looking up dictionary values by the get method
- dict.get(key, default=None)
Returns the value for the specified key if the key is in the dictionary, otherwise returns the default value.
Example:
car_dictionary = {"brand": "Ford", "model": "Mustang", "year": 1965}
# without default
value = car_dictionary.get("brand")
print(value)
# output is "Ford"
# without default; returns None
value = car_dictionary.get("brands")
print(value)
# output is None
# with default
value = car_dictionary.get("brands", "not a valid key")
print(value)
# output is "not a valid key"
1.5. Check if the key is in the dictionary
- key in dictionary
- Returns True if the key is among the keys of the dictionary; False if not.
Example:
car_dictionary = {"brand": "Ford", "model": "Mustang", "year": 1965}
# key exists
key_exists = "model" in car_dictionary
print(key_exists)
# key doesn't exists
key_exists = "models" in car_dictionary
print(key_exists)
1.6. Definition to get a dictionary value
1.6.1. Create a dictionary
Tasks
Create a dictionary, rainbow_colors, with the colour name as the key and the hex value as the value.
Create a dictionary, rainbow_colors, with the colour name as the key and the hex value as the value.
# Dictionary for rainbow colors
rainbow_colors = {
"red": "#FF0000",
"orange": "#FFA500",
"yellow": "#FFFF00",
"green": "#008000",
"blue": "#0000FF",
"indigo": "#4B0082",
"violet": "#EE82EE"
}
1.6.2. User input
- input(prompt)
- prompt is a string, representing a default message for the input.Returns a string.
Example:
colour = input("Enter a colour: ")
print(colour)
Tasks
Create a user input that refers to all the possible colors and stores it in the variable, user_color.
Create a user input that refers to all the possible colors and stores it in the variable, user_color
user_color = input("Enter a color from the rainbow (red, orange, yellow, green, blue, indigo, violet): ")
1.6.3. Hex colour function
A scaffold of a simple function to return the hex value of a colour is below.
def hex_color(user_color, ________________):
# Convert the input to lowercase for case-insensitivity
user_color = ______________.lower()
return ________________.get(_________________, "not listed in the dictionary")
Tasks
Complete the function to return the hex colour for a named colour.
Complete the function to return the hex colour for a named colour.
def hex_color(user_color, rainbow_colors):
# Convert the input to lowercase for case-insensitivity
user_color = user_color.lower()
return rainbow_colors.get(user_color, "not listed in the dictionary")
1.6.4. Final code
Exercise
Create a python script that gets user input and prints the hex colour for the color name the user inputs.
Example output: The hexadecimal value for green is #008000.
Create a python script that gets user input and prints the hex colour for the color name the user inputs.
Example output: The hexadecimal value for green is #008000.
# Dictionary with rainbow colors
rainbow_colors = {
"red": "#FF0000",
"orange": "#FFA500",
"yellow": "#FFFF00",
"green": "#008000",
"blue": "#0000FF",
"indigo": "#4B0082",
"violet": "#EE82EE"
}
user_color = input('Enter a rainbow color (red, orange, yellow, green, blue, indigo, violet): ')
def hex_color(user_color, rainbow_colors):
# Convert the input to lowercase for case-insensitivity
user_color = user_color.lower()
return rainbow_colors.get(user_color, "not listed in the dictionary")
hex_val = hex_color(user_color, rainbow_colors)
print(f"The hexadecimal value for {user_color} is {hex_val}")