Python Data Types - Dictionary

 

# Lone Star Development Training - Dictionary Data Type
# DICTIONARY
# Dictionaries are one of the data types that you will see often in Python. Dictionaries are used
# to store data values in key value pairs. A dictionary is a
# collection which is ordered, changeable and does not allow duplicates. Values
# in a dictionary can be of any data type.

# As of Python 3.7 dictionaries are ordered. In Python 3.6 and earlier they are unordered.


# DICTIONARY METHODS
# clear() - removes all elements from the dictionary
# copy() - returns a copy of the dictionary
# get() - returns value of specified key
# items() - returns a list containing a tuple for each key value pair (KVP)
# keys() - returns a list containing dict keys
# pop() - remove element with specified key
# popitem() - removes the last inserted KVP
# update() - updates dict with specified KVP's
# values() - returns list of all the values of dictionary


# Create dictionary with int keys
print("Language Dictionary")
languages_dict = {1: "Python", 2: "C#", 3: "Java", 4: "TypeScript", 5: "JavaScript", 6: "VB",
                  7: "ActionScript", 8: "Objective C"}
print(languages_dict)

# Create dictionary with the dict method
print("Car Dictionary")
cars_dict = dict({1: "Corvette", 2: "Countach", 3: "911", 4: "F50", 5: "Aventador", 6: "Cayman"})
print(cars_dict)

# Create empty dictionary and add items to it
print("Empty Dictionary")
empty_dict = {}
empty_dict[1] = "Setting 1"
empty_dict[2] = "Setting 2"
empty_dict[3] = "Setting 3"
print(empty_dict)

# Create dictionary with string KVP
print("Color Dictionary")
color_dict = {"red": "#AA4A44", "blue": "#0000FF", "black": "#000000", "white": "#FFFFFF", "purple": "#800080"}
print(color_dict)

# Example for dict update method - update with specified KVP
print("Color Dictionary with Red Updated")
color_dict.update({"red": "#EE4B2B"})
print(color_dict)

# Example for dict get method - return value for passed in key
print("Get Method Examples")
red_hex = color_dict.get("red")
print(red_hex)
red_hex1 = color_dict["red"]
print(red_hex1)
dream_car = cars_dict[2]
print(dream_car)

# Example for dict items method - ruturn list containing a tuple for each KVP
print("Items Method Example")
print(color_dict.items())

# Example for dict copy method - return copy of dict
print("Copy Method Example")
cars_dict_copy = cars_dict.copy()
print(cars_dict_copy)

# Example for dict clear method - clear dict
print("Clear Method Example")
cars_dict_copy.clear()
print(cars_dict_copy)

# Example for dict keys method
print("Keys Method Example")
print(cars_dict.keys())

# Example for dict values method
print("Values Method Example")
print(cars_dict.values())

# Example for dict pop method
print("Pop Method Example")
print(cars_dict.pop(3))
print(cars_dict)

# Example for dict popitem method
print("Pop Item  Method Example")
print(cars_dict.popitem())
print(cars_dict)



# EXERCISES
# Exercise 1
# Take the cars_dict from above and add two of your favorite cars to the dictionary to the #3 & #6 keys
# that were removed in the examples up above
print("Exercise 1")



# Exercise 2
# Take the cars_dict from above and try to add another car and assign to key #3. What happened?
print("Exercise 2")



# Exercise 3
# Create your own dictionary with an int for the key and then values should be first and
# last names of everyone in your family, including pets. Then print dictionary.
print("Exercise 3")



# Exercise 4
# 1. Create a new dictionary my_states
# 2. Add at least 5 states and use the state abbreviation for the key and the
#    name of the state for the value.
# 3. Pick your favorite state in the dictionary and search for it in the values and return the key
# 4. Use that key to bring back that state name and assign to a variable named favorite_state
# 5. Create and print a sting that says "My favorite state is <favorite_state>!"
# 6. BONUS - create a function to handle some of the work outlined above.
print("Exercise 4")



# Exercise 5
# 1. Create a new dictionary looping_states
# 2. loop through a list of 10 states and add them to the dictionary values and use LS1-LS10 for the
#    keys for those values and the 1-10 should be generated by an incremental counter and appended to
#    the 'LS' for the key through the use of a function. Then return the values of the dictionary. So you should
#    have a loop that generates the number and end up starting with Key = LS1 and Value = <One of my
#    favorite states> and continue on to the next state with LS2.
print("Exercise 5")

Comments

Popular posts from this blog

Upgrading to .NET8 from desktop versions 4.8.X

GHL Chat Bots for Webpage

GHL > Set website so shorter URL address