Introduction to Python for Developers
George Boorman
Curriculum Manager, DataCamp
for
, while
if
, elif
, else
, >
, >=
, <
, <=
, ==
, !=
+=
print()
in
= check if a value is in a variable/data structureproducts_dict = {"AG32": 10, "HT91": 20, "PL65": 30, "OS31": 15, "KB07": 25, "TR48": 35}
# Check if "OS31" is a key in products_dict if "OS31" in products_dict.keys(): print(True) else: print(False)
True
not
= check if a condition is not met# Check if "OS31" is not a key in products_dict
if "OS31" not in products_dict.keys():
print(False)
else:
print(True)
True
and
= check if multiple conditions are met# Check if "HT91" is a key and the minimum price of all products is > 5 if "HT91" in products_dict.keys() and min(products_dict.values()) > 5: print(True)
else: print(False)
True
or
= check if one (or more) condition is met# Check if "HT91" is a key or that the minimum price of all products is < 5 if "HT91" in products_dict.keys() or min(products_dict.values()) < 5: print(True)
else: print(False)
True
sales_count = 0 for sale in range(1, 10): # sales_count = sales_count + 1 sales_count += 1
stock = 10 for sale in range(1, 10): # sales_count = sales_count - 1 stock -= 1
# Create an empty list expensive_products = []
# Loop through the dictionary for key, val in products_dict.items():
# Check if price is 20 dollars or more if val >= 20:
# Append the product ID to the list expensive_products.append(key)
print(expensive_products)
['HT91', 'PL65', 'KB07', 'TR48']
Introduction to Python for Developers