Python 入門(開發者向)
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}
字典是有序的
需要多少 pasta?
# 找出該食材的數量
print(recipe["pasta"])
500
# 取得字典中所有值
print(recipe.values())
dict_values([500, 400, 15, 20, 30, 5])
# 取得字典中所有鍵
print(recipe.keys())
dict_keys(['pasta', 'tomatoes', 'garlic', 'basil', 'olive oil', 'salt'])
# 列印整個字典
print(recipe)
{'pasta': 500, 'tomatoes': 400, 'garlic': 15, 'basil': 20, 'olive oil': 30,
'salt': 5}
# 取得所有項目(鍵值配對)
print(recipe.items())
dict_items([('pasta', 500), ('tomatoes', 400), ('garlic', 15), ('basil', 20),
('olive oil', 30), ('salt', 5)])
# 新增一組鍵值
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}
# 更新既有鍵對應的值
recipe["pasta"] = 1000
print(recipe)
{'pasta': 1000, 'tomatoes': 400, 'garlic': 15, 'basil': 20, 'olive oil': 30,
'salt': 5, 'parmesan': 50}
# 使用重複鍵建立字典 recipe = {"pasta": 500, "garlic": 5, "garlic": 15, "basil": 20, "olive oil": 30, "salt": 5}# 列印重複鍵的值 print(recipe["garlic"])
15
Python 入門(開發者向)