Building a workflow

Introduction to Python for Developers

Jasmin Ludolf

Senior Data Science Content Developer

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
recipe = {"pasta": 500, "tomatoes": 400, 
          "garlic": 15, "basil": 20}

if "pasta" in recipe.keys(): print(True) else: print(False)
True
  • Faster than looping through every key
Introduction to Python for Developers

The "not" keyword

  • not = check if a condition is not met
  • Useful for validating that something is missing
pantry_items = ["flour", "sugar", "olive oil"]

# Check if "salt" is NOT in our pantry if "salt" not in pantry_items: print(True) else: print(False)
True
Introduction to Python for Developers

The "and" keyword

  • and = check if multiple conditions are met
  • Use when multiple requirements must be met
pasta_quantity = 600
olive_oil_quantity = 30

# Check if we have enough of BOTH ingredients if pasta_quantity >= 500 and olive_oil_quantity >= 30: print(True) else: print(False)
True
Introduction to Python for Developers

The "or" keyword

  • or = check if one (or more) condition is met
  • Use when any of several options are acceptable
pasta_quantity = 600
olive_oil_quantity = 30

# Check if we have enough of EITHER ingredient if pasta_quantity >= 500 or olive_oil_quantity >= 30: print(True) else: print(False)
True
Introduction to Python for Developers

Adding/subtracting from variables

  • Combine keywords with other techniques to build complex workflows
ingredients_checked = 0
for ingredient in recipe_list:
    # ingredients_checked = ingredients_checked + 1
    ingredients_checked += 1

items_to_buy = 10 for item in shopping_list: # items_to_buy = items_to_buy - 1 items_to_buy -= 1
  • += adds to a variable, -= subtracts from it
  • Other ways to update variables
Introduction to Python for Developers

Appending

  • Store information that meets specific criteria in a list
# Create empty list to hold results
shopping_list = []

# Loop through recipe ingredients for ingredient, qty_needed in recipe.items():
# Check if we need to buy it if ingredient not in pantry:
# Add to shopping list shopping_list.append(ingredient)
Introduction to Python for Developers

Appending

print(shopping_list)
['tomatoes', 'salt']
Introduction to Python for Developers

Let's practice!

Introduction to Python for Developers

Preparing Video For Download...