Introduction to Python for Developers
George Boorman
Curriculum Manager, DataCamp
prices = [10, 20, 30, 15, 25, 35]
# Product IDs product_ids = ["AG32", "HT91", "PL65", "OS31", "KB07", "TR48"]
user_id
order_number
date
value
payment_method
ip_address
location
# Creating a dictionary
products_dict =
# Creating a dictionary
products_dict = {
# Creating a dictionary
products_dict = {"AG32"
# Creating a dictionary
products_dict = {"AG32":
# Creating a dictionary
products_dict = {"AG32": 10
# Creating a dictionary
products_dict = {"AG32": 10,
# Creating a dictionary
products_dict = {"AG32": 10, "HT91": 20,
# Creating a dictionary
products_dict = {"AG32": 10, "HT91": 20,
"PL65": 30, "OS31": 15,
"KB07": 25, "TR48": 35
# Creating a dictionary
products_dict = {"AG32": 10, "HT91": 20,
"PL65": 30, "OS31": 15,
"KB07": 25, "TR48": 35}
Dictionaries are ordered
What is the price of product ID "AG32"
?
# Find the product's price
products_dict["AG32"]
10
# Get all values from a dictionary
products_dict.values()
dict_values([10, 20, 30, 15, 25, 35])
# Retrieve all keys in a dictionary
products_dict.keys()
dict_keys(['AG32', 'HT91', 'PL65', 'OS31', 'KB07', 'TR48'])
# Print the dictionary
print(products_dict)
{'AG32': 10, 'HT91': 20, 'PL65': 30, 'OS31': 15, 'KB07': 25, 'TR48': 35}
# Get all items (key-value pairs)
products_dict.items()
dict_items([('AG32', 10), ('HT91', 20), ('PL65', 30), ('OS31', 15), ('KB07', 25),
('TR48', 35)])
.items()
is useful when iterating or looping# Add a new key-value pair
products_dict["UI56"] = 40
# Updating a value associated with an existing key
products_dict["HT91"] = 12
# Creating a dictionary with a duplicate key products_dict = {"AG32": 10, "AG32": 20, "PL65": 30, "OS31": 15, "KB07": 25, "TR48": 35}
# Print the duplicate key's value print(products_dict["AG32"])
20
Introduction to Python for Developers