Fun with Algorithms: Cooking Recipes
Read at your own pace. Code execution, quizzes, progress tracking, and the AI tutor are available in the interactive view with JavaScript enabled.
From Data To Recipes
Welcome back, little data chef!
In Building Blocks of Data Structures, you learned that data needs homes:
- Lists keep things in order
- Dictionaries use labels
- Sets avoid repeats
Now we are ready for the next idea: algorithms.
An algorithm is a clear set of steps for doing something. A cooking recipe is a friendly algorithm:
- Gather ingredients
- Follow steps in order
- Make something new
In data science, AI, and machine learning, algorithms are the recipes computers follow to turn data into answers.

What Is Algorithm
Algorithm = recipe for action
A good recipe tells you exactly what to do.
Example: making a sandwich.
Input: bread, cheese, tomato
Steps:
1. Put bread on plate
2. Add cheese
3. Add tomato
4. Put bread on top
Output: sandwich
That same shape appears in computing:
Input data → Steps → Output answer
The steps matter. If you put the plate on the bread instead of the bread on the plate, things get silly!

Python Follows Steps
A recipe can become code
Python runs instructions from top to bottom, just like reading a recipe card.
Here we use a list, the ordered container you met in Building Blocks of Data Structures, to store recipe steps.
```python
steps = [
'Get two slices of bread',
'Add cheese',
'Add tomato',
'Put the second slice on top'
]
print('Sandwich Algorithm:')
for number, step in enumerate(steps, start=1):
print(number, step)
print('Output: sandwich ready!')
```
Spot The Algorithm
Which one is the best example of an algorithm?
Order Really Matters
Same steps, wrong order?
Algorithms are not just bags of actions. They are ordered actions.
Imagine brushing teeth:
Good order:
1. Put toothpaste on brush
2. Brush teeth
3. Rinse mouth
Now the silly version:
Wrong order:
1. Rinse mouth
2. Brush teeth
3. Put toothpaste on brush
The same actions appear, but the result is not as good.
This is why sorting from Discover Patterns with Sorting was powerful: once things are in a useful order, patterns and answers become easier to find.

Recipe Check
True or false?
If an algorithm has the right steps but in a confusing order, the output can be wrong.
Repeat With Loops
Recipes often repeat
Some recipe steps happen more than once:
- Stir 5 times
- Add 3 spoonfuls
- Wash every apple
In algorithms, repeated steps are often handled with a loop.
A loop says: do this step again for each thing or do this step many times.
```python
apples = ['red apple', 'green apple', 'yellow apple']
print('Washing apples:')
for apple in apples:
print('Wash the', apple)
print('All apples are clean!')
```
Name The Starter
In the recipe-style picture of an algorithm:
_____ → steps → output
The things we start with are called what?
Choices In Recipes
Algorithms can make tiny choices
Recipes sometimes say:
If the soup is cold, heat it.
Otherwise, serve it.
That is a condition: a rule that chooses between paths.
AI and machine learning use many smart rules later, but the tiny idea starts here: an algorithm can look at information and decide what step comes next.
```python
soup_temperature = 'cold'
print('Checking soup...')
if soup_temperature == 'cold':
print('Heat the soup')
else:
print('Serve the soup')
print('Dinner step complete!')
```
Algorithm Chef Toolbox
What you now know
Today you learned that an algorithm is like a cooking recipe:
- Inputs are the starting ingredients or data
- Steps are clear instructions
- Order matters
- Loops repeat steps
- Conditions choose what happens next
- Output is the result
This connects your earlier lessons into one bigger idea:
- Numbers count things
- Shapes, colors, and patterns describe things
- Data structures hold things
- Algorithms do useful work with those things
Next, we will gently move from recipe-style algorithms toward using computers to solve small problems.
