Introducere în Python pentru dezvoltatori
Jasmin Ludolf
Senior Data Science Content Developer
Conțin date unice
Imuabile
Ideale pentru identificarea și eliminarea duplicatelor
Căutare rapidă (față de alte structuri de date, precum listele)
{}: = Dicționar: = Set# Create a set of ingredients
ingredients = {"pasta", "tomatoes", "pasta",
"basil", "garlic", "olive oil", "salt"}
print(ingredients)
{'pasta', 'tomatoes', 'garlic', 'basil', 'olive oil', 'salt'}
# Existing list variable ingredients_list = ["pasta", "tomatoes", "garlic", "basil" "olive oil", "pasta", "salt"]# Convert to a set unique_ingredients = set(ingredients_list)# Check the data type type(unique_ingredients)
set
print(unique_ingredients)
{'pasta', 'tomatoes', 'garlic', 'basil', 'olive oil'}
[]# Trying to subset a set
print(unique_ingredients[0])
TypeError: 'set' object is not subscriptable
ingredients = {"pasta", "tomatoes", "garlic", "basil", "olive oil", "salt"}# Sorting a set print(sorted(ingredients))
['basil', 'garlic', 'olive oil', 'pasta', 'salt', 'tomatoes']
sorted() returnează o listă
[0]

# Creating a tuple serving_sizes = (1, 2, 4, 6, 8)# Convert another data structure to a tuple ingredients_tuple = tuple(ingredients_list)
# A tuple
serving_sizes = (1, 2, 4, 6, 8)
# Access the second element
print(serving_size[1])
2
Introducere în Python pentru dezvoltatori