Skip to content

Informatics Practices · Ch 3 — Brief Overview of Python

Working with Python

3.1.1

Working with Python

Writing a Python program is only half the job — something must read those statements and make the computer carry them out. That something is the Python interpreter. To write and run (execute) a Python program, a Python interpreter must be installed on the computer; alternatively, an online Python interpreter can be used through the internet, with nothing installed locally. Either way, the interpreter is the engine that takes each Python statement and executes it.

The interpreter is also called the Python shell. When it is opened, it presents a screen of its own (a sample appears in Figure 3.1) whose most important element is a small symbol:

  • >>> — this is the Python prompt. Its presence means the interpreter is ready to receive instructions. You type a command or statement at this prompt, and the interpreter executes it.

The prompt is worth internalising as a signal: whenever >>> is showing, the shell is waiting for your next instruction; while a statement is being executed, it is not.

Working directly at the prompt has one serious limitation: statements typed there are not saved for future use. Once the session is over, everything typed is gone, and running the same statements again means retyping them from scratch.

Saving work — script mode

The remedy is script mode: instead of typing statements live at the prompt, the whole program is written in a file, saved, and then the interpreter is asked to execute the program from that file. Such program files carry the .py extension and are also known as scripts. Beginners usually start in the interactive shell, but for any program longer than a few lines the code should always be saved in a file — that way it can be run again, corrected and extended at any time.

A Python script can be created in any editor. Python itself ships with a built-in editor called IDLE — short for Integrated Development and Learning Environment. Creating a program in IDLE follows a simple routine:

  1. Open IDLE.
  2. Choose File > New File to open a fresh editor window.
  3. Type the program into that window.
  4. Save it with a name of your choice. By default, Python scripts are saved in the Python installation folder.

Figure 3.3 shows a small program, prog3-1.py, written in the IDLE editor this way.

Running a script

Executing a saved program in script mode takes three steps:

  1. Open the program in an editor — for example, open prog3-1.py in IDLE (as in Figure 3.3). …
Figure 3.1Python Interpreter or Shell

The figure is a screenshot of the Python interpreter as it appears the moment it is opened on a computer — specifically the Python 3.7.0 Shell window running through IDLE. The title bar at the top reads "Python 3.7.0 Shell", and directly beneath it sits a menu bar with the entries File, Edit, Shell, Debug, Options, Window and Help, the standard menus through which files are opened, text is edited and the shell is controlled.

The main area of the window opens with the interpreter's version banner: the line "Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)] on win32". This single line identifies exactly which interpreter is running — the Python version (3.7.0), the build identifier and build date, the compiler it was built with, and the platform (a 32-bit build on Windows). Below the banner is the customary line inviting the user to type "copyright", "credits" or "license" for more information. …