Python Scripts, the Shell and Editors
What you will learn
- How to run a script (a
.pyfile) from a terminal - How to use the interactive shell (the
>>>prompt), and leave it - How to run one line with
python -c, and a script then the prompt withpython -i - What to look for in a code editor
1. Three ways to run code
You met two ways in lesson 1.1. Here they are again with a third, small one.
| Way | Command | Best for |
|---|---|---|
| Script | python hello.py |
real programs you keep and run again |
| Interactive shell | python |
trying one idea at a time |
| One command | python -c "print(2 + 3)" |
a quick one-line check |
Quick check: which way would you use to test what `10 / 4` gives?
The interactive shell (or python -c). It answers at once and you do not need to save a file.
2. Running a script
A script is a text file of Python instructions whose name ends in .py. Open a terminal in the folder that holds the file and run:
python hello.py
If your system uses python3, type that instead. Python reads the file from top to bottom, runs it and returns you to the terminal.
Two things to check when it does not work:
- Are you in the right folder? The terminal looks for
hello.pyin its current folder. Usecdto move to the folder first. - Is the name exact?
hello.py, nothello.py.txt.
3. The interactive shell
Type python with no file name. You see a few lines of information and then a prompt:
>>>
The prompt waits for one Python instruction. Type it and press Enter. Python runs it and shows the answer straight away. This loop is also called the REPL (read, evaluate, print, loop).
>>> 2 + 3
5
>>> print("Hello")
Hello
>>> x = 4
>>> x
4
Notice:
- You do not need
printto see the value of an expression:2 + 3shows5. - A value stored with
=shows nothing (x = 4), because it is an instruction, not a value to display. Typing the namexshows what it holds. - The shell remembers:
xis still4on the next line. - A line that starts a block (such as
for) shows...while you type the indented lines. Press Enter on an empty line to finish.
>>> for i in range(2):
... print(i)
...
0
1
The underscore
The shell keeps the last result in a name called _:
>>> 2 + 3
5
>>> _ * 10
50
This works in the interactive shell only, not inside scripts.
Getting help and leaving
| You type | What happens |
|---|---|
help(len) |
shows the documentation for len (press q to leave a long page) |
exit() |
leaves the shell |
You can also leave with Ctrl+Z then Enter on Windows, or Ctrl+D on macOS and Linux. exit() works everywhere. Everything you typed in the shell is forgotten when you leave, so keep real programs in files.
4. One command with -c
python -c "..." runs the Python code inside the quotation marks and stops:
python -c "print(2 + 3)"
Output:
5
It is handy for a quick check without opening a file or the shell. Use double quotes around the whole command and single quotes inside it, so that it works in both Windows and macOS or Linux terminals: python -c "print('hi')".
5. A script, then the shell: -i
The -i option runs your script and then stays in the shell, with all the script's names still available. It is excellent for testing what a script created.
Suppose t1.py holds:
print("Hello")
x = 7
Running it with -i:
python -i t1.py
Hello
>>> x
7
The script ran first, and then you could look at x yourself.
6. Choosing an editor
Any plain text editor can write a .py file. A word processor such as Word cannot, because it adds formatting. A good code editor helps you more:
| Feature | Why it helps |
|---|---|
| Syntax colouring | different colours for text, numbers and keywords make typos visible |
| Indentation help | Python depends on spaces at the start of a line; an editor keeps them tidy |
| A Run button or built-in terminal | run your file without leaving the editor |
| Error highlighting | shows many mistakes before you run the program |
Common choices include IDLE (a simple editor that normally comes with Python from python.org), Visual Studio Code and PyCharm. Pick any one and stay with it while you learn; the language is the same in all of them.
Worked example: three ways to see the same answer
Goal: find the value of 7 * 6 + 1.
| Way | What you do | Result |
|---|---|---|
| Shell | type python, then 7 * 6 + 1 |
43 |
| One command | python -c "print(7 * 6 + 1)" |
43 |
| Script | save print(7 * 6 + 1) as calc.py, run python calc.py |
43 |
All three give 43. The shell and -c are for quick tests; the script is what you keep.
Common mistakes
- Typing the prompt. Writing
>>> print("hi")inside a script: the>>>is shown by the shell, not part of the code. - Typing Python at the terminal's own prompt. Typing
print("hi")there, outside Python, gives an error, because the terminal does not understand Python. Start Python first, or usepython -c. - Expecting the shell to remember. Names and results are lost when you leave it.
- Using
_in a script. The last-result name works only in the interactive shell. - Running the script from the wrong folder. Move to its folder with
cdfirst.
Key takeaways
- Use a script (
python file.py) for programs you keep. - Use the shell (
python) to try ideas; leave it withexit(). python -c "..."runs one line, andpython -i file.pyruns a script then keeps the shell open.- In the shell, an expression shows its value and
_holds the last result. - Use a code editor with colouring and indentation help; the choice matters less than using one.
Practice
1. What does the shell show if you type 10 - 4? Do you need print?
Show answer
It shows 6. You do not need print, because the shell displays the value of an expression.
2. You type y = 9 in the shell and nothing appears. Is that an error? How do you see the value?
Show answer
It is not an error: an assignment shows nothing. Type y on the next line to see 9.
3. In the shell you type 4 * 5 and then _ + 1. What does the second line show?
Show answer
21. The first line gives 20, and _ holds that last result.
4. Write the command that runs one line, print("ok"), without creating a file.
Show answer
python -c "print('ok')" (or with the quotation marks the other way round, if your terminal allows it).
5. A script data.py makes a name total. How can you run the script and then check total yourself?
Show answer
Run python -i data.py. The script runs first, and then you can type total at the >>> prompt.
6. Why is a word processor a poor choice for writing Python files?
Show answer
It adds formatting and can change straight quotation marks into curly ones, which Python rejects. Use a plain text editor or a code editor.