Slovníky

Introduction to Python for Developers

Jasmin Ludolf

Senior Data Science Content Developer

Seznam ingrediencí

ingredients = ["pasta", "tomatoes", "garlic", "basil", "olive oil", "salt"]

# Ingredient quantities (in grams) quantities = [500, 400, 15, 20, 30, 5]
Introduction to Python for Developers

Slovník

  • Slovník = páry klíč–hodnota

Slovník s vyznačeným slovem (klíč) a definicí (hodnota)

1 https://unsplash.com/@sandym10
Introduction to Python for Developers

Proč používat slovníky?

  • username
  • email
  • preferences

 

  • ip_address
  • location

$$

  • Škálování receptu: propojení ingrediencí s množstvím

Informace o uživateli

Počítačová síť

1 User image by Bilobaba Vladimir, Aviavlad; https://unsplash.com/@jjying
Introduction to Python for Developers

Vytvoření slovníku

# Creating a dictionary
recipe = 
Introduction to Python for Developers

Vytvoření slovníku

# Creating a dictionary
recipe = {
Introduction to Python for Developers

Vytvoření slovníku

# Creating a dictionary
recipe = {"pasta"
Introduction to Python for Developers

Vytvoření slovníku

# Creating a dictionary
recipe = {"pasta":
Introduction to Python for Developers

Vytvoření slovníku

# Creating a dictionary
recipe = {"pasta": 500
Introduction to Python for Developers

Vytvoření slovníku

# Creating a dictionary
recipe = {"pasta": 500,
Introduction to Python for Developers

Vytvoření slovníku

# Creating a dictionary
recipe = {"pasta": 500,
          "tomatoes": 400,
Introduction to Python for Developers

Vytvoření slovníku

# Creating a dictionary
recipe = {"pasta": 500,
          "tomatoes": 400, 
          "garlic": 15,
          "basil": 20, 
          "olive oil": 30,
          "salt": 5
Introduction to Python for Developers

Vytvoření slovníku

# Creating a dictionary
recipe = {"pasta": 500,
          "tomatoes": 400, 
          "garlic": 15,
          "basil": 20, 
          "olive oil": 30,
          "salt": 5}
Introduction to Python for Developers

Přístup k hodnotě podle klíče

  • Slovníky jsou seřazené

    • Hodnoty jsou přístupné indexováním pomocí klíče
  • Kolik těstovin potřebujeme?

# Find the ingredient's quantity
print(recipe["pasta"])
500
Introduction to Python for Developers

Přístup ke všem hodnotám

# Get all values from a dictionary
print(recipe.values())
dict_values([500, 400, 15, 20, 30, 5])
Introduction to Python for Developers

Přístup ke všem klíčům

# Retrieve all keys in a dictionary
print(recipe.keys())
dict_keys(['pasta', 'tomatoes', 'garlic', 'basil', 'olive oil', 'salt'])
Introduction to Python for Developers

Zobrazení celého slovníku

# 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)])
Introduction to Python for Developers

Přidání páru klíč–hodnota

# 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}
Introduction to Python for Developers

Aktualizace hodnoty

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}
Introduction to Python for Developers

Duplicitní klíče

# 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 to Python for Developers

Lass uns üben!

Introduction to Python for Developers

Preparing Video For Download...