Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Running Code in a REPL Session

00:00 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.

00:30 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 python command.

00:43 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.

00:58 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.

01:36 While running these examples, note how after executing each expression, the interpreter loops back to the primary prompt, which lets you introduce a new expression.

01:46 Now you’ve seen the REPL cycle in action. The Python interpreter has read your expressions, executed them, and printed their corresponding result to finally loop back to the primary prompt.

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.

02:12 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.

02:28 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.

02:45 These dots represent the REPL’s secondary prompt. This prompt on your screen means that you can enter the required indented blocks of your current compound statement.

03:08 Note that to break out of the REPL’s secondary prompt, you must press Enter twice. This action takes you back to the primary prompt.

03:21 When it comes to entering indented code blocks, keep in mind that the standard REPL doesn’t support auto-indentation.

03:33 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.

03:49 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.

04:12 This assert statement performs two checks on number. First, it uses the built-in isinstance() function to check if the value is an integer.

04:21 Next, it checks if the input value is greater than 0. If either of these conditions fails, then the statement raises an AssertionError with the provided message as an argument.

04:36 Line continuations also happen when you use several physical lines to write an expression delimited by a pair of brackets—for example, when you define a list, tuple, or dictionary.

04:52 Once you use opening brackets ([]), braces ({}), or parentheses (()) and press Enter, you get the REPL’s secondary prompt. This is known as implicit line joining.

05:08 You can also use implicit line joining in other contexts, such as mathematical and Boolean expressions, function definitions and calls, list comprehensions, and generator expressions.

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.

05:52 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.

06:10 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 .append() and sort().

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.

06:48 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.

07:02 If you ever need to display None in a REPL session, then you must use the print() function.

07:11 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.

07:34 You’ll need to use print(), as you can see here with value.

07:43 In the next section of the course, you’ll deepen your understanding of running code in the REPL, looking at errors, special variables, and how to reload imported modules.

Become a Member to join the conversation.