Nhập môn Python dành cho Developer
Jasmin Ludolf
Senior Data Science Content Developer
for, whileif, elif, else, >, >=, <, <=, ==, !=+=print()in = kiểm tra giá trị có nằm trong biến/cấu trúc dữ liệu khôngrecipe = {"pasta": 500, "tomatoes": 400, "garlic": 15, "basil": 20}if "pasta" in recipe.keys(): print(True) else: print(False)
True
not = kiểm tra điều kiện không thỏapantry_items = ["flour", "sugar", "olive oil"]# Kiểm tra "salt" KHÔNG có trong tủ bếp if "salt" not in pantry_items: print(True) else: print(False)
True
and = kiểm tra nhiều điều kiện cùng đúngpasta_quantity = 600 olive_oil_quantity = 30# Kiểm tra có đủ CẢ HAI nguyên liệu if pasta_quantity >= 500 and olive_oil_quantity >= 30: print(True) else: print(False)
True
or = kiểm tra chỉ cần một (hoặc nhiều) điều kiện đúngpasta_quantity = 600 olive_oil_quantity = 30# Kiểm tra có đủ MỘT TRONG HAI nguyên liệu 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
+= cộng vào biến, -= trừ khỏi biến# Tạo danh sách rỗng để chứa kết quả shopping_list = []# Lặp qua nguyên liệu công thức for ingredient, qty_needed in recipe.items():# Kiểm tra có cần mua không if ingredient not in pantry:# Thêm vào danh sách mua shopping_list.append(ingredient)
print(shopping_list)
['tomatoes', 'salt']
Nhập môn Python dành cho Developer