Introduzione a Python per sviluppatori
Jasmin Ludolf
Senior Data Science Content Developer
for, whileif, elif, else, >, >=, <, <=, ==, !=+=print()in = verifica se un valore è in una variabile/struttura datirecipe = {"pasta": 500, "tomatoes": 400, "garlic": 15, "basil": 20}if "pasta" in recipe.keys(): print(True) else: print(False)
True
not = verifica che una condizione non sia verapantry_items = ["flour", "sugar", "olive oil"]# Controlla se "salt" NON è in dispensa if "salt" not in pantry_items: print(True) else: print(False)
True
and = verifica se sono vere più condizionipasta_quantity = 600 olive_oil_quantity = 30# Controlla se abbiamo abbastanza ENTRAMBI gli ingredienti if pasta_quantity >= 500 and olive_oil_quantity >= 30: print(True) else: print(False)
True
or = verifica se è vera una (o più) condizionepasta_quantity = 600 olive_oil_quantity = 30# Controlla se abbiamo abbastanza ALMENO di uno dei due ingredienti if pasta_quantity >= 500 or olive_oil_quantity >= 30: print(True) else: print(False)
True
ingredients_checked = 0 for ingredient in recipe_list: # ingredients_checked = ingredients_checked + 1 ingredients_checked += 1items_to_buy = 10 for item in shopping_list: # items_to_buy = items_to_buy - 1 items_to_buy -= 1
+= aggiunge a una variabile, -= sottrae# Crea lista vuota per i risultati shopping_list = []# Cicla gli ingredienti della ricetta for ingredient, qty_needed in recipe.items():# Controlla se serve comprarlo if ingredient not in pantry:# Aggiungi alla lista della spesa shopping_list.append(ingredient)
print(shopping_list)
['tomatoes', 'salt']
Introduzione a Python per sviluppatori