Join us and get access to thousands of tutorials and a community of expert Pythonistas.
This lesson is for members only.Join us and get access to thousands of tutorials and a community of expert Pythonistas.
Running Code in a REPL Session
00:00Running 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 thepython command and someof 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:30Once 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 thepython command.
00:43When the REPL’s primary prompt (>>>) appears on the screen,type the expression seen on-screen, pressing theEnter keyafter each of them. This first expression subtracts the two numbers anddisplays the result.
00:58The second expression is a call to the built-insum() 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 avariable callednumber. 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:36While 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:46Now you’ve seen the REPL cycle in action.The Python interpreter has read your expressions, executed them,and printed their corresponding resultto finally loop back to the primary prompt.
02:00With 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 anindented code block.
02:12Python also has compound statements, such as conditionals, loops, andwithstatements. Compound statements require an indented code block.The Python interpreter has a secondary prompt that lets you enter the code blockof compound statements.
02:28Take 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 pressEnter,you get three dots (...) on your screen.
02:45These dots represent the REPL’s secondary prompt.This prompt on your screen means that you can enter the required indented blocksof your current compound statement.
03:08Note that to break out of the REPL’s secondary prompt, you must pressEnter twice.This action takes you back to the primary prompt.
03:21When it comes to entering indented code blocks,keep in mind that the standard REPL doesn’t support auto-indentation.
03:33In the Python standard REPL,you must provide the appropriate indentation manually for any indented codeblock that you need to enter. Otherwise,you’ll get anIndentationError, as seen on-screen.
03:49Another situation where the REPL’s secondary prompt appears is when you need touse line continuations.A line continuation occurs when you explicitly join multiple physical lines intoa single logical line using the backslash character (\), as seen on-screen.
04:12Thisassert statement performs two checks onnumber. First,it uses the built-inisinstance() function to check if the value is an integer.
04:21Next, it checks if the input value is greater than0.If either of these conditions fails,then the statement raises anAssertionError with the provided message as anargument.
04:36Line continuations also happen when you use several physical lines to write anexpression delimited by a pair of brackets—for example,when you define a list, tuple, or dictionary.
04:52Once you use opening brackets ([]), braces ({}), or parentheses (()) and pressEnter,you get the REPL’s secondary prompt. This is known as implicit line joining.
05:08You can also use implicit line joining in other contexts, such as mathematicaland Boolean expressions, functiondefinitions and calls, list comprehensions, and generator expressions.
05:19In short,implicit line continuation will appear in all those Python constructs thataccept brackets, braces, or parentheses.When you run Python in interactive mode,you’ll note that the interpreter immediately displays the resulting value ofevaluating or executing any expression or statement.
05:39This is true for all the statements and expressions that generate a returnvalue.The interpreter doesn’t display anything for statements that don’t generatereturn values. That’s the case with assignment statements,as you’ve already seen.
05:52The Python interpreter behaves this way because its primary goal is to provideimmediate feedback on how your code works.This behavior makes using the built-inprint() function almost unnecessarywhen you’re working interactively. However,there’s at least one use case forprint() in REPL sessions.
06:10You’ll need to useprint() when you want to display the result of an expression orstatement that can or will returnNone.For example,a common error that some Python beginners make when they start to learn aboutlists is to expect new lists from calls to list methods, such as.append() andsort().
06:34Because 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:48In other words,list methods often modify the underlying list object instead of creating a newone. Because of this, they returnNone,which the REPL automatically ignores. As a result,nothing shows up on your screen.
07:02If you ever need to displayNone in a REPL session,then you must use theprint() function.
07:11Here you useprint() to show the return value of.append(), which isNone.Note that you can always access the content of a given variable by typing thevariable’s name and pressingEnter after it, as you did withnumbers. However,if the variable is currently set toNone, then you won’t get anything on-screen.
07:34You’ll need to useprint(), as you can see here withvalue.
07:43In 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.
Course Contents
- Getting to Know the Python Standard REPL
- Running Code in the Standard REPL
- Doing More With Your Code in the Standard REPL
- Making the Python REPL Your Own

