Introdução ao Python para desenvolvedores
Jasmin Ludolf
Senior Data Science Content Developer
for, whileif, elif, else, >, >=, <, <=, ==, !=+=print()in = verifica se um valor está em uma variável/estruturarecipe = {"pasta": 500, "tomatoes": 400, "garlic": 15, "basil": 20}if "pasta" in recipe.keys(): print(True) else: print(False)
True
not = verifica se uma condição não é atendidapantry_items = ["flour", "sugar", "olive oil"]# Verifique se "salt" NÃO está na despensa if "salt" not in pantry_items: print(True) else: print(False)
True
and = verifica se várias condições são atendidaspasta_quantity = 600 olive_oil_quantity = 30# Verifique se temos o suficiente de AMBOS os ingredientes if pasta_quantity >= 500 and olive_oil_quantity >= 30: print(True) else: print(False)
True
or = verifica se uma (ou mais) condição é atendidapasta_quantity = 600 olive_oil_quantity = 30# Verifique se temos o suficiente de QUALQUER um dos ingredientes 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
+= soma à variável; -= subtrai# Crie lista vazia para guardar resultados shopping_list = []# Faça loop pelos ingredientes da receita for ingredient, qty_needed in recipe.items():# Veja se precisamos comprar if ingredient not in pantry:# Adicione à lista de compras shopping_list.append(ingredient)
print(shopping_list)
['tomatoes', 'salt']
Introdução ao Python para desenvolvedores