GoalWrite a program in script mode, save it, run it and use comments to explain it.
New words
script mode · the way of using IDLE in which you type many lines in an editor, save them and run them togetherprogram file · a saved program; a Python file ends with .pycomment · a note for people that starts with # and is ignored by Pythonrun (execute) · to make the computer carry out a program
Explanation
The shell forgets your lines, so to keep a program you use script mode. Choose File, then New File, and a text editor opens. Type your lines there and save the file with a name that ends in .py. Then choose Run (or press F5), and Python carries out the lines in order from line 1. The output appears in the shell window. To change a program, open the file, edit it and save it again. A comment starts with the # symbol and Python ignores everything after # on that line. Comments tell other people what the program does and why.
Python
1# Zoo opening message2print("Welcome to the zoo")
3print("We open at nine") # opening time
Output
Welcome to the zoo
We open at nine
Worked examples
Python
1print("One")
2# print("Two")3print("Three")
Output
One
Three
Python ignores a line that starts with #, so the second print line does not run. Programmers often ‘comment out’ a line to test a program without deleting the line.
Python
1print(9 + 4)
2print("9 + 4")
Output
13
9 + 4
Quotation marks decide what Python does. Without quotes, 9 + 4 is a calculation and Python shows the answer. With quotes it is just text, so Python shows it exactly as written.
Step
What you do
1
Open IDLE and choose File, then New File
2
Type the lines of the program
3
Choose File, then Save As, and end the name with .py
4
Choose Run, then Run Module (or press F5)
5
Read the output in the shell window; fix any errors and run again
These are the steps for making your first program. Each step has its own menu or key.
Class activity
Paper IDLE (no computer): fold a sheet in two. On the left write a four-line program for ‘My day’: line 1 is a comment with the program’s title, then three print lines about breakfast, school and a hobby. Swap sheets with a partner. On the right-hand side the partner writes exactly what the screen shows when the program runs, and circles any line that would not appear. Then add a comment to every line to explain it. If you have a computer or tablet, type your program into IDLE with an adult, save it and run it.
Practice
1
What does this program show on the screen?
Python
1# Bakery sign2print("Fresh bread")
3# print("Cakes")4print("Open daily") # all week
Two lines: Fresh bread, then Open daily. Line 3 starts with #, so it is a comment and is ignored. The notes after # on the other lines do not appear either.
2
What does this program show? Write each line of output.
Python
1print("Total:")
2print(5 + 4)
3print("5 + 4")
Three lines: Total:, then 9, then 5 + 4. The first print shows text. The second adds 5 and 4 because there are no quotation marks. The third has quotation marks, so it shows the sum as text.
3
This program will not run. Python reports the error on line 2. Find the mistake and write the correct line.
Python
1print("Library rules")
2print(Be quiet")
3print("Return books on time")
Output
SyntaxError: unterminated string literal (detected at line 2)
On line 2 the opening quotation mark is missing before Be quiet. The correct line is print("Be quiet").
4
Write a program of three lines. Line 1 is a comment that says what the program does. The other lines show your favourite food and the result of 12 * 4.
Example: # Shows my favourite food and a sum, then print("My favourite food is rice"), then print(12 * 4). The program shows the sentence and then 48.