Building a workflow

Introduction to Python for Developers

George Boorman

Curriculum Manager, DataCamp

Complex workflows

  • Loops through data structures
    • for, while
  • Evaluate multiple conditions
    • if, elif, else, >, >=, <, <=, ==, !=
  • Update variables
    • +=
  • Return outputs
    • print()
Introduction to Python for Developers

The "in" keyword

  • in = check if a value is in a variable/data structure
products_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
Introduction to Python for Developers

The "not" keyword

  • 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
Introduction to Python for Developers

The "and" keyword

  • 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
Introduction to Python for Developers

The "or" keyword

  • 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
Introduction to Python for Developers

Adding/subtracting from variables

  • Combine keywords with other techniques to build complex workflows
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
  • Other ways to update variables
Introduction to Python for Developers

Appending

  • Store information that meets specific criteria in a list
# 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)
Introduction to Python for Developers

Appending

print(expensive_products)
['HT91', 'PL65', 'KB07', 'TR48']
Introduction to Python for Developers

Let's practice!

Introduction to Python for Developers

Preparing Video For Download...