AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Typing your first code

Week 1 · Unit 7.1: Block it out · 3 lessons
3

Save it and run it

Computing 7: Unit 7.1, pp. 8–43
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 message
2print("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.
StepWhat you do
1Open IDLE and choose File, then New File
2Type the lines of the program
3Choose File, then Save As, and end the name with .py
4Choose Run, then Run Module (or press F5)
5Read 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 sign
2print("Fresh bread")
3# print("Cakes")
4print("Open daily")  # all week
2
What does this program show? Write each line of output.
Python
1print("Total:")
2print(5 + 4)
3print("5 + 4")
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)
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.