Running Code in a REPL Session
Running Code in a REPL Session. Up to this point, you’ve learned what a Python REPL is and why Python developers love it. You’ve also learned how to start a REPL session using the
python command and some of its command-line options. Also, you’ve learned how to terminate a Python interactive session, jumping back to the operating system shell. In this part of the course, you’ll learn how to enter and execute Python code in an interactive session.
Once you’ve launched a Python interactive session from your command line, you can start entering and executing Python code immediately. To do this, get back to your command-line window and run the
When the REPL’s primary prompt (
>>>) appears on the screen, type the expression seen on-screen, pressing the Enter key after each of them. This first expression subtracts the two numbers and displays the result.
The second expression is a call to the built-in
sum() function, which takes a series of values and returns the total sum. Here you execute a Boolean expression that compares two numbers, and this example uses an assignment statement to define and initialize a variable called
number. Because assignments don’t return any value, the Python interpreter doesn’t display any output on your screen, and instead it falls back to the primary prompt immediately, and this shows how Python displays an error when your code has issues.
02:00 With the examples in a previous section, you’ve executed simple statements in a Python interactive session. These expressions are known as simple statements because they don’t have an indented code block.
Python also has compound statements, such as conditionals, loops, and
with statements. Compound statements require an indented code block. The Python interpreter has a secondary prompt that lets you enter the code block of compound statements.
Take a look at the example seen on-screen here. You first define a variable to hold a number. Next, you start a conditional statement. Once you type the colon character (
:) and press Enter, you get three dots (
...) on your screen.
In the Python standard REPL, you must provide the appropriate indentation manually for any indented code block that you need to enter. Otherwise, you’ll get an
IndentationError, as seen on-screen.
Another situation where the REPL’s secondary prompt appears is when you need to use line continuations. A line continuation occurs when you explicitly join multiple physical lines into a single logical line using the backslash character (
\), as seen on-screen.
05:19 In short, implicit line continuation will appear in all those Python constructs that accept brackets, braces, or parentheses. When you run Python in interactive mode, you’ll note that the interpreter immediately displays the resulting value of evaluating or executing any expression or statement.
05:39 This is true for all the statements and expressions that generate a return value. The interpreter doesn’t display anything for statements that don’t generate return values. That’s the case with assignment statements, as you’ve already seen.
The Python interpreter behaves this way because its primary goal is to provide immediate feedback on how your code works. This behavior makes using the built-in
print() function almost unnecessary when you’re working interactively. However, there’s at least one use case for
print() in REPL sessions.
You’ll need to use
print() when you want to display the result of an expression or statement that can or will return
None. For example, a common error that some Python beginners make when they start to learn about lists is to expect new lists from calls to list methods, such as
06:34 Because these method calls don’t issue any output to the screen, it may seem that they didn’t perform any actions. However, they did. Most list methods run their intended transformation or computation in place.
In other words, list methods often modify the underlying list object instead of creating a new one. Because of this, they return
None, which the REPL automatically ignores. As a result, nothing shows up on your screen.
Here you use
print() to show the return value of
.append(), which is
None. Note that you can always access the content of a given variable by typing the variable’s name and pressing Enter after it, as you did with
numbers. However, if the variable is currently set to
None, then you won’t get anything on-screen.
Become a Member to join the conversation.