Lesson 2, Topic 5
In Progress

Python Shell (Python REPL)

Lesson Progress
0% Complete

Starting The Python Shell

Either of the following methods can be used to start the Python shell:

  • Navigate to the folder where the Python distribution is installed and double-click the python.exe executable file:
  • In the Windows Start menu, scroll down to the Python 3.9 folder, expand it and click on Python 3.9 (64-bit) or Python 3.9 (32-bit), depending on the version you have installed:
  • In Command Prompt or PowerShell, type py or python and press the Enter key. The py command starts the Python launcher for Windows, which in turn starts the Python shell, whereas the python command starts the Python shell directly:

The first two methods will start the Python shell in a new window, whereas the third method will start the Python shell in a Command Prompt or PowerShell window:

Python Shell Window
Python shell in a new window
Python Shell Inside Command Prompt
Python shell in a Command Prompt window
Python Shell Inside PowerShell
Python shell in a PowerShell window

Note 1: We recommend pinning the Python shell to the Windows taskbar because you will use it extensively in this course. To pin it, right-click the python.exe file and click on Pin to taskbar, or drag and drop the file to the taskbar:

Note 2: There are some websites, like Python’s official website, that provide an online Python shell. Go to python.org and click on the yellow button >_:

You can also go directly to the Python shell page:

What is The Python Shell

The Python interactive shell, or simply the Python shell, is a computer program that enables the user to interact with the Python interpreter. It has the following characteristics:

  • It starts up the python interpreter.
  • It is a text-based interface, also called a command-line interface (CLI).
  • It acts as an interface between the user and the Python interpreter.
  • It allows the user to submit statements and expressions, one by one, to the Python interpreter. This latter will execute them and return the result/output immediately.
  • As long as the Python shell interface is opened, the Python interpreter will stay running.

In summary, the Python shell allows the user to communicate with the Python interpreter in an interactive way, hence the name Python interactive shell.

The Python shell is where most beginners start learning and exploring the Python language. In fact, its simple and clean interface helps them get familiar with new Python concepts and experiment with Python code.

Note 1: A distinction must be made between a Python shell and an operating system’s shell like for example Command Prompt or PowerShell. They look similar but the first one allows to interact with the Python interpreter whereas the second allows to interact with the operating system of your computer.

Note 2: A distinction must also be made between the Python shell and the Python interpreter. Some Python documentation erroneously use the two terms interchangeably, but in reality they refer to different computer programs.

Note 3: There are several Python shells with varying features in the Internet. The standard Python distribution comes with two Python shells, which are the default shell described earlier and IDLE.

Note 4: The Python shell is not mandatory for executing Python code. Later in this course, we will see that Python code can also be executed by double-clicking a Python script, or by using the script mode of the Python interpreter.

Working With The Python Shell

Welcome Message

When you start the python shell, the first textual output that you will receive from the Python interpreter is the welcome message:

In the screenshot above, the welcome message consists of the following:

GitHub is an online platform used by developers to create coding projects, collaborate, and control changes to their code. Whenever the project’s source code is changed, GitHub generates a value, called commit ID, that references the new source code. The commit ID is a SHA-1 hash value represented as a 40-character hexadecimal number (SHA-1 stands for Secure Hash Algorithm 1). Python’s source code is available in GitHub and each time the code changes, a commit ID is generated:

The problem with commit IDs is that they are hard to remember, that’s why we use tags, which are more human-readable, to reference the source code. A tag is typically used to mark, or indicate, a release version of the source code, and it generally has the same name as this release.

  • 0a7dcbd: the first characters of the commit ID. They are also called short SHA-1 hash.
  • May 3 2021, 17:27:52: the date and time at which the commit 0a7dcbd was performed. In GitHub lingo, the term “commit” means saving changes to the source code.
  • MSC v.1928: the name and version of the compiler used to build the Python interpreter. MSC stands for Microsoft C compiler. Compilation will be defined later in this course.
  • 64 bit (AMD64): the Python interpreter was built with a 64-bit compiler.
  • on win32: the platform on which the compilation took place. Win32 means Windows, whether it is a 32 or 64-bit version.
  • Type “help”, “copyright”, “credits” or “license” for more information: the commands that you can type in the Python shell to get help, copyright, credits, and license information.

When starting the Python shell from Command Prompt or PowerShell, you can bypass the welcome message by adding the -q option to the python command (q stands for quiet):

Primary Prompt (>>>)

The other textual output that you will receive from the Python interpreter is the primary prompt >>> , which consists of three greater-than signs, also called chevrons, plus a space character:

These chevrons indicate that the Python interpreter is ready to receive your statements and expressions, and that you can start interacting with it.

Note 1: Linguistically, the verb “prompt” means to urge someone to do something.

Note 2: According to Python’s inventor, Guido Van Rossum, the primary prompt >>> was taken from the ABC language:

The >>> prompt was taken from ABC. I have no idea where they got it from (Guido Van Rossum, Personal History – part 1, CWI)

Now, you are ready to run your first Python code. You will call the print() built-in function to print the “Hello Python!” message to the screen. This message is enclosed in quotes because it is a string. The quotes indicate to the Python interpreter when the string starts and when it ends. Copy and paste the below code into your Python shell. Evidently, you mustn’t copy the primary prompt >>> because it is not part of the code:

>>> print("Hello Python!")

If you got the following output, then congratulations! You have successfully run your first Python code:

>>> print("Hello Python!")
Hello Python!

The text after the primary prompt >>> is called the shell input, and the text in the next line is called the shell output. The shell output is the response that you receive from the Python interpreter after executing your code. Notice that the shell output does not begin with the primary prompt >>>.

Let’s continue with the print() function to get familiar with it. For example, you can print your full name, your year of birth, your country, et cetera. For the year of birth, it should normally be an integer so you don’t need to enclose it in quotes as with strings. In fact, strings can contain multiple words and thus they require quotes to delimit them. On the other hand, integers always consist of a single word and thus they don’t require quotes:

>>> print("Guido van Rossum")
Guido van Rossum

>>> print(1956)
1956

>>> print("Netherlands")
Netherlands

You can also print the above values using a single print() call, just separate them with commas inside the parentheses:

>>> print("Guido van Rossum", 1956, "Netherlands")
Guido van Rossum 1956 Netherlands

Note 1: In this course, whenever you see the primary prompt >>>, this means that the next code is meant to be executed in the Python Shell.

Note 2: Indentation is significant in Python code and leading spaces are not allowed in statements, except in compound statements. Therefore, you mustn’t leave space(s) between the primary prompt >>> and the print function because you will get an indentation error. Besides, you don’t need to leave a space between the print function and the primary prompt >>>  because this latter already contains a space after the chevrons:

>>>  print("Hello Python!")
  File "<stdin>", line 1
    print("Hello Python!")
IndentationError: unexpected indent

The above error message means that there is an error in the file named “<stdin>”, which stands for standard input, and precisely in the line number 1 of this file. The standard input here is the statement that you just entered in the Python shell. After that, the Python interpreter displays the line of code that caused the error, the error name (IndentationError) and a brief description of it.

Note 3: If you started the Python shell from Command Prompt or PowerShell, you can alternate between the C:\ prompt and Python’s primary prompt >>> using these commands:

Python As A Calculator

The Python interpreter can be used as a calculator to perform basic arithmetic operations such as addition, subtraction, multiplication, and division. You can enter an arithmetic expression in the shell and you will receive its result immediately. The operators (+, -, *, /) work as in mathematics:

Addition:
>>> 100 + 200
300

Subtraction:
>>> 500 - 350
150

Multiplication:
>>> 40 * 80
3200

Division:
>>> 1000 / 8
125.0

Parentheses () can be used for grouping:

>>> 1200 + (600*3)
3000

Exponentiation, i.e. a raised to the power of b (ab), is written as a ** b:

>>> 30 ** 3
27000

The floor division of two integers a and b consists of two operations; a division operation and a floor operation:

  • First, a is divided by b.
  • The result of the division is floored with the floor function, denoted floor(x), which is the function that takes as input a real number x, and returns the greatest integer less than or equal to x.

In brief, the floor division of a and b is equal to floor(a/b). The floor division operator is //:

>>> 18 // 5
3
>>> 85 // 9
9
>>> 50 // -20
-3
>>> -41 // 6
-7

The modulo operation returns the remainder of the division of two integers a and b. It is written as a % b, and read as a modulo b. For example, 10 modulo 3 returns 1 because 10 divided by 3 has a quotient of 3 and a remainder of 1. 100 modulo 20 returns 0 because 100 divided by 20 has a quotient of 5 and a remainder of 0:

>>> 10 % 3
1
>>> 100 % 20
0

For all integers a and b, if (a // b = c) and (a % b = d), then (a = b*c + d):

>>> 115 // 9
12
>>> 115 % 9
7
>>> 9*12 + 7
115
>>> 50 // -20
-3
>>> 50 % -20
-10
>>> -20*-3 + -10
50

Here is a summary of the operations mentioned above:

* Spaces are generally encouraged between numbers and operators because they enhance readability. Therefore, a + b is better than a+b, a – b is better than a-b, and so on.

The mathematical operations that we executed so far are all Python expressions. When you enter a Python expression in the shell, the Python interpreter will evaluate it and return the result, which is always a value. The simplest Python expressions are literals, and a literal always evaluates to itself:

>>> 10
10
>>> 20.5
20.5
>>> "Hello Python!"
'Hello Python!'
>>> True
True

The result of the last-last evaluated expression is always stored by the Python interpreter, and you can display it using the underscore character _:

>>> 100 + 200 + 300
600
>>> _
600

This is useful when you want to continue calculations:

>>> _ * 5
3000
>>> _ % 400
200

You can use the up and down arrow keys of your keyboard to browse the last 50 commands entered in the Python shell. For example, if you enter the following commands:

>>> print("Hello Python!")
Hello Python!
>>> 100 * 5
500

Then, you can press the up arrow key one time to repeat the last command:

>>> 100 * 5

You can also press the up arrow key two times to repeat the second to last command:

>>> print("Hello Python!")

This is useful when you want to execute a command multiple times, or when you want to edit a previous command:

>>> print("Learning Python is cool!")

If you span a Python expression over multiple lines, the primary prompt >>> will change in the second line to a secondary prompt , which consists of three dots plus a space character:

>>> (1 + 2 + 4 + 8 + 16
... + 32 + 64 + 128 + 256
... + 512 + 1024 + 2048)
4095

The primary and secondary prompts are strings, and you can display them using these commands (which will be explained later in this course):

>>> import sys
>>> sys.ps1
'>>> '
>>> sys.ps2
'... '

You can also change them using these commands:

import sys
sys.ps1 = "string_1"
sys.ps2 = "string_2"

For example:

>>> import sys
>>> sys.ps1 = "$ "
$ sys.ps2 = "-> "
$ (100 + 200 +
-> 300 + 400)
1000

Note: The Python shell is also known as Python REPL because the following loop will be repeated each time you run an expression:

  • The Python interpreter reads the expression.
  • The Python interpreter evaluates the expression.
  • The Python interpreter prints the result of the expression.

This loop is called read-evaluate-print loop. It is abbreviated as REPL and pronounced “repple”.