Learn the input-process-output pattern and run a first Python program.
Your first Python program
Learning objectives
- Describe a problem using input, process, and output.
- Run a Python code cell.
- Use
print()to display a result. - Turn a plain-language algorithm into code.
The pattern
Suppose we want to calculate the total minutes in a school day:
| Part | Example |
|---|---|
| Input | Number of class periods and minutes per period |
| Process | Multiply the two values |
| Output | Total minutes |
Write the algorithm before the code:
read the number of periods
read the minutes per period
multiply the two values
display the total
Code cell
periods = 6
minutes_per_period = 50
total_minutes = periods * minutes_per_period
print(f"A school day has {total_minutes} class minutes.")
periods and minutes_per_period store input values. The multiplication is the process. The formatted string is the output.
Guided practice
Change the program to calculate:
- the number of minutes in five periods;
- the total minutes for a different schedule; and
- the number of hours represented by the result.
periods = 5
minutes_per_period = 55
total_minutes = periods * minutes_per_period
hours = total_minutes / 60
print(total_minutes)
print(hours)
Checkpoint
Explain why total_minutes is a better name than x. Then write an input-process-output plan for a program that calculates the cost of three notebooks.
Mini-project
Build a unit converter that converts kilometers to miles. State the conversion assumption in a comment and test the program with zero and a larger value.