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, boolean 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.
Using our dictionary, products_dict, containing product IDs as keys and prices as values, we can check whether the ID "OS31" is included in the dictionary keys, printing True if so, and False if not.
4. The "not" keyword
Next is the not keyword, which is used to confirm that a condition has not been met.
We can combine it with the in keyword to confirm that a value is not in a data structure! Here, we check if "OS31" is not in the dictionary's keys. If not, we print False, otherwise, we print True.
5. The "and" keyword
Another important keyword is and, which allows us to check if multiple conditions are met.
Here, we check if "HT91" is a key in the dictionary and that the minimum price of all products is more than 5 dollars.
If both of these conditions are met, we print True. If either or both of these conditions are not met, we print False.
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.
Modifying our previous example, here, we again check if "HT91" is a key in the dictionary, but now we use or to look at whether the minimum price is less than five dollars. If one or both of these conditions is met then we print True, otherwise, we print False.
As "HT91" is a key in the dictionary, it returns True, despite the minimum price being more than five dollars.
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, such as stock.
However, we might need other ways to update variables, such as a data structure like a list.
8. Appending
Say we want to use a list to store information that meets certain criteria, such as the product IDs for all products that cost at least 20 dollars.
To do this, we first create an empty list, like so.
We then loop through the keys and values in our dictionary.
Inside the loop, we check if the value, or price, is 20 or more.
If so, we the list.append method to append the key, or product ID, to the list.
9. Appending
Outside of the loop, we print the list, which returns the four product IDs that met our price criterion!
10. Let's practice!
Time for you to build your own workflow!