GoalType single commands into the Python shell, use print and calculations, and explain why Python is case sensitive.
New words
Python shell · the window where you type one command at a time and Python answers at once (interactive mode)print statement · a Python command that shows text or a value on the screencase sensitive · able to tell capital letters from small lettersinterpreter · the part of Python that translates your commands for the computer, one line at a time
Explanation
When you open IDLE you see the Python shell. The symbol >>> is the prompt and it waits for you to type a command. This is called interactive mode. When you press Enter, the interpreter translates the line and Python carries it out at once. The command print("Hello") shows the text between the quotation marks, but a calculation such as 7 * 6 shows its answer without print. The symbol * means multiply and / means divide. Python is case sensitive: print is a command, but Print is not. Lines typed in the shell are not saved.
You type at >>>
The shell shows
print("Hello")
Hello
(4 + 6) * 3
30
12 / 4
3.0
2 + 3 * 4
14
Worked examples
You type at >>>
The shell shows
10 - 2 * 3
4
(10 - 2) * 3
24
50 - 8 - 2
40
Brackets change the order of a calculation. The first two commands use the same numbers but give different answers, because Python works out the brackets first. In the third command both signs are minus, so Python works from left to right: 50 - 8 is 42, and 42 - 2 is 40.
Python
1Print("Good morning")
Output
NameError: name 'Print' is not defined
A capital letter in the command name is a mistake. Python does not know the word Print, so it reports a NameError.
Python
1print("Hello"
Output
SyntaxError: '(' was never closed
Nodira forgets to close a bracket. Python cannot read the line, so it reports a syntax error. Always count your opening and closing brackets.
Class activity
Be the interpreter (no computer): write these four lines on four slips of paper: print("Hi") · Print("Hi") · 6 * 7 · print("Hi). Mix the slips and pick one at a time. Your partner is the Python shell and must answer: what appears, or which mistake there is. Then swap roles and write four new slips with your own commands. Keep a score: one point for each correct answer. If you have a computer or tablet, type the slips into IDLE with an adult and check your answers.
Practice
1
Fill in the shell’s answers (the question marks).
You type at >>>
The shell shows
print("Moon")
?
8 * 5
?
(2 + 4) * 5
?
100 / 8
?
Moon · 40 · 30 · 12.5. In the third row the brackets are worked out first: (2 + 4) is 6, and 6 * 5 is 30. In the last row a division with / gives a decimal answer, 12.5.
2
This program shows Hello and then stops with an error. Why does line 1 work but line 2 does not? How do you fix line 2?
Python
1print("Hello")
2Print("Welcome")
Output
Hello
NameError: name 'Print' is not defined
Line 2 is written with a capital P. Python is case sensitive, so Print is a different word from print, and Python does not know it. Line 1 has already run, so Hello appears first. Fix: print("Welcome").
3
Write the two commands you would type in the shell: one that shows the words Good luck! and one that takes 15 away from 90 and then divides the answer by 5.
print("Good luck!") and (90 - 15) / 5. The brackets make Python subtract first: 90 - 15 is 75. Then 75 / 5 gives 15.0, because / always gives a decimal answer.
4
Why is it correct to write a capital H in print("Hello"), but wrong to write a capital P in Print?
Text inside quotation marks is just text and is shown exactly as typed. The word print is a command, and Python only knows it written in small letters.