Building a workflow
1. Building a workflow
Let's round out our knowledge by examining some additional techniques to use when building workflows.2. Complex workflows
When developing software we might need to implement extensive custom logic. For example, we may need to loop through multiple data structures, evaluate them against multiple conditions, update variables based on these checks, and display various outputs depending on the results. We have the majority of the knowledge required for this complex workflow, given our ability to use for and while loops, operators, and if, elif, and else statements.3. The "in" keyword
Let's examine some more conditional keywords and techniques to help us design complex workflows. First, there's in, which is used to check if a value exists in a variable or data structure. For our recipe scaler, let's check if a specific ingredient is in our recipe dictionary. We check if "pasta" is one of our recipe keys, and return True if it is, or False if it isn't. This is faster than looping through every key to find a match.4. The "not" keyword
Next is the not keyword, which is used to confirm that a condition has not been met. Here we check if "salt" is not in our pantry items. If salt is missing, this returns True, and we might add it to our shopping list.5. The "and" keyword
Another important keyword is and, which allows us to check if multiple conditions are met, like checking if a user is logged in AND has the right permissions, or if a form is complete AND all fields are valid. For our recipe scaler, let's check if we have enough of two key ingredients. We check if we have at least 500g of pasta, and 30ml of olive oil. Both conditions must be True for this check to return True.6. The "or" keyword
The last keyword for us to discuss is or. This allows us to provide multiple conditions and evaluate if at least one of them is true. For our recipe scaler, let's check our pasta and oil stocks again using or. This time if either condition is True, we can proceed.7. Adding/subtracting from variables
We can combine keywords with other techniques to build custom workflows. One example is updating variables based on loops or conditions. Recall that we can increment the value of a variable using a for loop, like so. We can also swap the plus-equals for minus-equals to subtract one from the value of a variable. However, we might need other ways to update variables, such as a data structure like a list.8. Appending
Let's build a shopping list of ingredients that we need to buy. First, we create an empty list called shopping_list. This will hold our results. Then we loop through the keys and values in our recipe dictionary using .items(). Each key is an ingredient name, and each value is the quantity we need. Inside the loop, we check a condition: is the required quantity more than what we currently have in our pantry? If so, we use the .append() method to add that ingredient to the our shopping list.9. Appending
After the loop completes, we print the shopping_list, which now contains only the ingredients we need to buy!10. Let's practice!
Time for you to build your own workflow!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.