First programs: crash course
about 6 min to listen5 lessons0 questions to tryUnit 1 of 6
Short view. Each lesson's rule, its steps and its trap. Switch to Full for a worked example and a question to try in every lesson.Full view. Each lesson's rule, its steps, one worked example with Watch it solved, the trap, and one question to try.
Starts here no skill from another unit needed first
1 Run your first Python program
The rule
Lines you want to keep go in the editor window.
A line you want to try once goes after >>> in the Shell. Save the file, choose Run and then Run Module, and read the result in the Shell below the newest RESTART line. For print("Lantern") the printed line is Lantern. The quote marks belong to your instruction, and the >>> belongs to the Shell, so neither is part of the printed line.
How
- Open IDLE from your Python 3.13 installation. IDLE is an optional part of Python. If it is missing, look for instructions from whoever provided your Python. Depending on its settings and how you start it, IDLE may open an editor window when it starts. If you do not see the Shell, choose Run, then Python Shell, to open it.
- In the Shell, after the
>>>prompt, typeprint("Lantern")and press Return. The Shell shows Lantern. - Choose File, then New File, to open an empty editor window.
- In the editor window, type
print("Lantern")on line 1. Type the instruction only, without>>>. - Choose File, then Save. A new file has no name yet, so IDLE asks you for one with Save As. Type lantern.py. While a window has unsaved changes, its title has a * at each end. The title bar also shows the Python version, so check that it says 3.13.
- Choose Run, then Run Module. The IDLE documentation names F5 as the key for it. If you forgot to save, IDLE either asks you to save or saves for you, depending on its settings.
- Look at the Shell. You see a line containing RESTART, then Lantern, then the prompt.
- In the editor, change Lantern to Cove so the line reads
print("Cove"). Save, then run again. Cove appears below a newer RESTART line. Lantern stays higher up, because the Shell keeps earlier output.
Worked example
Run the saved file
print("Lantern")
Standard input
Empty
Standard output
Lantern
Save the line in lantern.py. Choose Run Module in the editor, then look in the Shell for the printed line.
Edit and run again
print("Cove")
Standard input
Empty
Standard output
Cove
Change the text in the editor, save and run the file again. The Shell shows Cove on its own line.
The trap
The usual mistake is writing the expected output as >>> Lantern, or as Lantern inside quote marks. The >>> is the Shell's prompt. The Shell shows it after the run to say it is ready for your next line, so it is not part of what your program printed. The quote marks belong to your instruction, and the printed line is Lantern alone. A related slip is copying >>> into the editor window. The file holds only your instruction. If nothing new appears, check two things. Look in the Shell, because the result never appears in the editor. Then look at the window title, because a * at each end means your latest change is not saved yet.
2 Tell text from numbers
The rule
Use addition for two integers and joining for two strings.
Check for quote marks before deciding which operation this example uses.
How
- Find the
+and the value on each side of it. In this lesson both values always have the same type. - Look at the edges of each value for a pair of matching quote marks.
- If both values have quotes, they are strings: write the characters of the left value, then the characters of the right value.
- If neither value has quotes, they are integers: add them as you would on paper.
- Write what
printshows: the result with no quote marks, on a line of its own.
Worked example
Predict both lines
print(7 + 2)
print("7" + "2")
Standard input
Empty
Standard output
9
72
On paper, count seven objects and two more. Then put a tile marked 7 next to one marked 2. The total is 9 and the two tiles read 72.
The trap
A natural mistake is to assume that anything made of digits will add. You see a 7 and a 2 in both 7 + 2 and "7" + "2", so it feels natural to expect 9 from both. The quote marks change the type: inside them each digit is a character tile, and + sets the tiles side by side to make 72. Before you predict a +, look at the edges of each value, not at the digits.
3 Give a value a name
The rule
An assignment has a name to the left of = and a value or calculation to the right.
Python works out the whole right-hand side first, then ties the left-hand name to the result. If that name already had a value, it now names the new one instead. No other name changes.
How
- Draw a state table with a column for the line, a column for what the right-hand side works out to, one column for each name and a column for output.
- Take the next line of the program. If it is an assignment, cover the left side with your finger and work out the right side, using the values in the row above.
- Write that result in the column of the name on the left. Carry every other name's value down unchanged from the row above.
- If the line is a
print, write what it displays in the output column: the values in the order given, with one space between them. - Repeat until the last line. The output column is your prediction. Compare it with the real output if you have Python 3.13, or with the printed answer if you are working on paper.
Worked example
Trace boxes and spare
boxes = 4
spare = boxes
boxes = boxes + 1
print(spare, boxes)
Standard input
Empty
Standard output
4 5
Follow the rows below. For a check in the Shell, enter the three assignments and then enter each name on its own.
The trap
The usual mistake is to predict 5 5. It comes from reading spare = boxes as a lasting link, as if spare will always match boxes. That line ran once, at step 2, and tied spare to 4. Step 3 moved only the boxes tag, because boxes was the only name on the left.
A second mistake is reading = as the equals sign from arithmetic. Read that way, boxes = boxes + 1 looks like nonsense, since no number equals itself plus one. In Python, = means "tie this name to this value". The comparison "is equal to" is written with two signs, ==, and a later unit teaches it.
Avoid saying that spare = boxes "makes a copy". The Python tutorial states that a simple assignment never copies data. It puts a second name on the value that is already there. This lesson keeps to whole numbers on purpose.
4 Read a reply from the keyboard
The rule
Keep the reply as text for a badge code, where every character matters.
For a count written in digits, convert the reply with int(reply) before adding to it.
How
- Type the Python source, the lines of code shown below, in your editor. Keep the blank line after
reply = input()and save the file. - Run the saved file with Python 3.13. The
input()call has empty parentheses, so it waits for your reply without displaying a message asking for it. Type the two digits04where the program is running and press Enter. - Compare the two lines the program prints with the expected output. The Shell can show your typed reply above those lines. If you’re working on paper, follow the code one line at a time and write down what each
print()displays.
Worked example
One reply for a badge and a count
reply = input()
print(reply)
print(int(reply) + 1)
Prompt
Type 04 in the Shell and press Enter.Standard input
04
Standard output
04
5
The first line of output keeps the two badge characters. To check the second line on paper, draw four strokes and add one.
The trap
A reply can look like a number and still be text. Writing reply + 1 skips the conversion. Use int(reply) + 1 for the count. For this version, enter a whole number written in digits, such as 04 or 8. The word four is outside the form that int() can convert here.
5 Use an error message to fix a program
The rule
Start at the reported line or the spot IDLE marks.
The mark gives you an approximate place to look. The fix may be nearby. For the misspelled name, compare the name inside the parentheses with the one beside =. For the unclosed call, look for the missing ). Make one change and run the file again so you can see what that change did.
How
- In IDLE for Python 3.13, use File, then New File to open an Editor window, where you type and save source. Save the file under the example’s name. In the Editor’s Run menu, choose Run Module. Read the program’s output in the Shell window. For these examples, type the source in the Editor and leave the Shell for reading.
- For the misspelled name, find the file name and line number in the Shell report. Open that file in the Editor if needed. Go to the indicated line, counting from 1 and including blank lines. Read the error class on the report’s last line. For the unclosed call, look for the spot IDLE marks in the Editor during the syntax check.
- Make one change in the Editor and save the file.
- Choose Run Module again and compare what appears in the Shell with your predicted output.
Worked example
Find the missing letter
seats = 3
print(seat)
Standard input
Empty
Standard output
Empty
Exception NameError
Read the error class on the last line and use the file and line number to find the misspelled name.
Run the repaired file
seats = 3
print(seats)
Standard input
Empty
Standard output
3
Write seats next to 3 on paper. The second line looks up that name and prints the value.
Read a syntax error
print("Gate open")
print("Gate closed"
Standard input
Empty
Standard output
Empty
Exception SyntaxError
The file has an unclosed call. Python reports a syntax error during parsing, before either line prints.
Close the parentheses
print("Gate open")
print("Gate closed")
Standard input
Empty
Standard output
Gate open
Gate closed
Save the corrected file. Read the two lines in order to predict its output.
The trap
Check the whole name when the error says NameError. If a call has an open parenthesis, find its closing partner before running the file. An unclosed call can stop the whole file during parsing.