Introduction à Python pour les développeurs
Jasmin Ludolf
Senior Data Science Content Developer
ingredients = ["pasta", "tomatoes", "garlic", "basil", "olive oil", "salt"]# Ingredient quantities (in grams) quantities = [500, 400, 15, 20, 30, 5]

usernameemailpreferences
ip_addresslocation$$


# Creating a dictionary
recipe =
# Creating a dictionary
recipe = {
# Creating a dictionary
recipe = {"pasta"
# Creating a dictionary
recipe = {"pasta":
# Creating a dictionary
recipe = {"pasta": 500
# Creating a dictionary
recipe = {"pasta": 500,
# Creating a dictionary
recipe = {"pasta": 500,
"tomatoes": 400,
# Creating a dictionary
recipe = {"pasta": 500,
"tomatoes": 400,
"garlic": 15,
"basil": 20,
"olive oil": 30,
"salt": 5
# Creating a dictionary
recipe = {"pasta": 500,
"tomatoes": 400,
"garlic": 15,
"basil": 20,
"olive oil": 30,
"salt": 5}
Les dictionnaires sont organisés
Quelle quantité de pâtes est nécessaire ?
# Find the ingredient's quantity
print(recipe["pasta"])
500
# Get all values from a dictionary
print(recipe.values())
dict_values([500, 400, 15, 20, 30, 5])
# Retrieve all keys in a dictionary
print(recipe.keys())
dict_keys(['pasta', 'tomatoes', 'garlic', 'basil', 'olive oil', 'salt'])
# Print the dictionary
print(recipe)
{'pasta': 500, 'tomatoes': 400, 'garlic': 15, 'basil': 20, 'olive oil': 30,
'salt': 5}
# Get all items (key-value pairs)
print(recipe.items())
dict_items([('pasta', 500), ('tomatoes', 400), ('garlic', 15), ('basil', 20),
('olive oil', 30), ('salt', 5)])
# Add a new key-value pair
recipe["parmesan"] = 50
print(recipe)
{'pasta': 500, 'tomatoes': 400, 'garlic': 15, 'basil': 20, 'olive oil': 30,
'salt': 5, 'parmesan': 50}
print(recipe)
{'pasta': 500, 'tomatoes': 400, 'garlic': 15, 'basil': 20, 'olive oil': 30,
'salt': 5, 'parmesan': 50}
# Updating a value associated with an existing key
recipe["pasta"] = 1000
print(recipe)
{'pasta': 1000, 'tomatoes': 400, 'garlic': 15, 'basil': 20, 'olive oil': 30,
'salt': 5, 'parmesan': 50}
# Creating a dictionary with a duplicate key recipe = {"pasta": 500, "garlic": 5, "garlic": 15, "basil": 20, "olive oil": 30, "salt": 5}# Print the duplicate key's value print(recipe["garlic"])
15
Introduction à Python pour les développeurs