← Back to course
Code Crew G6+ / Week 01 / Wednesday
3/6
Week 01 Β· Hello, Mission Control

Wednesday

Saving a program in a file
// Your first real Python programs
⏱ about 30 min

Wednesday: Saving a program in a file

Rocket has typed his welcome banner into the shell five times today, once for every crew member who arrived. "There has to be a faster way," he groans, stretching his fingers.

Raven flips back through her notebook. "What do you notice about the shell?" she asks. "Every time you press Enter, the line runs once and then it is gone. Nothing gets kept."

Nova projects a small picture of a folder. "Would you like a hint?" she asks. "Programmers write their lines in a file first. Then one command runs the whole file, as many times as you like."

Rocket opens a fresh window and decides to write the banner just once, then save it forever.

The shell and the editor

IDLE has two kinds of windows. The shell runs one line the moment you press Enter. It is great for quick experiments.

The editor is for programs. You type many lines, save them as a file ending in .py, and then run the whole file at once. Nothing runs while you type.

ShellEditor
How it startsOpens with IDLEFile, then New File
What you seeThe >>> promptAn empty page
When lines runEach time you press EnterWhen you choose Run Module
Good forQuick testsPrograms you keep

Your first program file

print("Mission Control online")
print("Crew ready:", 3)
print("=" * 20)
print("Let's build some tools.")

Python runs a file from the top line to the bottom line, in order. When you choose Run Module, the shell restarts and shows a RESTART line with your file name. Your output appears below it:

Mission Control online
Crew ready: 3
====================
Let's build some tools.
How many lines of output does hello_mission.py show (not counting the RESTART line)?
WHY THIS EXERCISEEach print call shows one line, and Python runs every line of the file once, from top to bottom.
PUT THE STEPS FOR MAKING AND RUNNING A PROGRAM IN ORDER
  • ?Choose File, then New File
  • ?Read the output in the shell
  • ?Choose File, then Save, and name it hello_mission.py
  • ?Type the program in the editor
  • ?Choose Run, then Run Module
WHY THIS EXERCISEIDLE runs the saved file, so the program must be typed and saved before Run Module.
SHELL OR EDITOR?
  • Read the question.
  • Tap your answer.
You want to check what 15 * 4 is, just once. Which is quicker?
You want a banner you can show every day this week. Which do you use?
At your computer
1. In IDLE, choose File, then New File. An empty editor window opens.
2. Type the four lines of hello_mission.py exactly as shown.
3. Choose File, then Save. Open your mission folder and name the file hello_mission.py.
4. Choose Run, then Run Module. The F5 key does the same thing.
5. Look in the shell for the RESTART line and your four lines of output.
6. Change the crew number to 4, save, and press F5 again.
If IDLE says the source must be saved, click OK. It saves the file and then runs it.

Trace a program, line by line

Tracing means reading a program the way Python does: one line at a time, from the top. You write down what each line shows before you run it.

An empty print() shows a blank line. It is useful for spacing out a report.

print("CREW ROLL CALL")
print("-" * 14)
print("Rocket", "Raven", "Nova")
print()
print("Total crew:", 1 + 1 + 1)
TRACE CREW_ROLL.PY
  • Read the question.
  • Tap your answer.
What does the second line show?
What does the fourth line, print(), show?
If you swap lines 1 and 2, what appears first?
What does the last line show after Total crew: ?
How many lines of output, counting the blank one?
PUT THE OUTPUT OF CREW_ROLL.PY IN ORDER
  • ?Total crew: 3
  • ?Rocket Raven Nova
  • ?--------------
  • ?CREW ROLL CALL
WHY THIS EXERCISEOutput always comes out in the same order as the print lines in the file.

You wrote, saved and ran a real Python program. Tomorrow you will learn to leave notes inside your programs for other people, and for your future self.

← Tuesday