HTML Diff
0 added 0 removed
Original 2026-01-01
Modified 2026-02-21
1 <p>Python includes a built-in<strong>REPL</strong>- an interactive Python interpreter. It is a program that runs as a shell and executes code in Python.</p>
1 <p>Python includes a built-in<strong>REPL</strong>- an interactive Python interpreter. It is a program that runs as a shell and executes code in Python.</p>
2 <p>The acronym REPL stands for:</p>
2 <p>The acronym REPL stands for:</p>
3 <ul><li><strong>Read</strong>- read input from the user</li>
3 <ul><li><strong>Read</strong>- read input from the user</li>
4 <li><strong>Eval</strong>- execute the entered code</li>
4 <li><strong>Eval</strong>- execute the entered code</li>
5 <li><strong>Print</strong>- print the result on the screen</li>
5 <li><strong>Print</strong>- print the result on the screen</li>
6 <li><strong>Loop</strong>- enter standby mode again</li>
6 <li><strong>Loop</strong>- enter standby mode again</li>
7 </ul><p>To start the REPL, we type python3. You can then execute the Python code and see the result. REPL displays the operation result directly on the screen and enters command entry standby mode again.</p>
7 </ul><p>To start the REPL, we type python3. You can then execute the Python code and see the result. REPL displays the operation result directly on the screen and enters command entry standby mode again.</p>
8 <p>This way of working is good for debugging, simple computations, and quick tests of hypotheses along the lines of "How does this thing work?"</p>
8 <p>This way of working is good for debugging, simple computations, and quick tests of hypotheses along the lines of "How does this thing work?"</p>
9 <p>Let's look at an example of working with the REPL:</p>
9 <p>Let's look at an example of working with the REPL:</p>
10 <p>To exit the REPL, we press Ctrl + D.</p>
10 <p>To exit the REPL, we press Ctrl + D.</p>
11 <h2>Built-in documentation</h2>
11 <h2>Built-in documentation</h2>
12 <p>Python allows you to add documentation to the code. It supports this feature at the syntax level. The essential documentation tool is<strong>documentation strings</strong>or<strong>docstrings</strong>.</p>
12 <p>Python allows you to add documentation to the code. It supports this feature at the syntax level. The essential documentation tool is<strong>documentation strings</strong>or<strong>docstrings</strong>.</p>
13 <p>The documented function looks like this:</p>
13 <p>The documented function looks like this:</p>
14 <p>We generate online documentation from such strings. And this documentation is also available for viewing directly in the REPL. To view the documentation, use the help function.</p>
14 <p>We generate online documentation from such strings. And this documentation is also available for viewing directly in the REPL. To view the documentation, use the help function.</p>
15 <p>Let's declare the add function in REPL, try to call it, and then look at the definition of our function:</p>
15 <p>Let's declare the add function in REPL, try to call it, and then look at the definition of our function:</p>
16 <p>The help function also works in interactive mode, which we can do using the help() command without arguments. In this case, you will see the welcome page, and the prompt on the input line will change to help&gt;. The prompt page shows which command you can enter. To exit the help mode, quit or press Ctrl+D.</p>
16 <p>The help function also works in interactive mode, which we can do using the help() command without arguments. In this case, you will see the welcome page, and the prompt on the input line will change to help&gt;. The prompt page shows which command you can enter. To exit the help mode, quit or press Ctrl+D.</p>
17 <p>The topics command is helpful for beginners. It displays a list of articles you can read in the REPL help mode.</p>
17 <p>The topics command is helpful for beginners. It displays a list of articles you can read in the REPL help mode.</p>
18 <p>We'll enter help mode and display a list of topics. Then we'll open an article that talks about Python operators:</p>
18 <p>We'll enter help mode and display a list of topics. Then we'll open an article that talks about Python operators:</p>
19 <h2>REPL and code examples in sources</h2>
19 <h2>REPL and code examples in sources</h2>
20 <p>REPL is so widespread that you can find examples depicting a piece of dialog between a programmer and REPL in many sources. It looks something like this:</p>
20 <p>REPL is so widespread that you can find examples depicting a piece of dialog between a programmer and REPL in many sources. It looks something like this:</p>
21 <p>Notice how the lines begin differently:</p>
21 <p>Notice how the lines begin differently:</p>
22 <ul><li>Lines with &gt;&gt;&gt; have code entered by the programmer. The &gt;&gt;&gt; symbol itself is called<strong>an invitation</strong></li>
22 <ul><li>Lines with &gt;&gt;&gt; have code entered by the programmer. The &gt;&gt;&gt; symbol itself is called<strong>an invitation</strong></li>
23 <li>Lines without &gt;&gt;&gt; are the result of the code you entered</li>
23 <li>Lines without &gt;&gt;&gt; are the result of the code you entered</li>
24 </ul><p>If you decide to try it yourself, don't enter the invitation. Instead, enter the code that follows the &gt;&gt;&gt; characters.</p>
24 </ul><p>If you decide to try it yourself, don't enter the invitation. Instead, enter the code that follows the &gt;&gt;&gt; characters.</p>
25 <h2>Canonical representation and printout</h2>
25 <h2>Canonical representation and printout</h2>
26 <p>In the examples above, you can see that when you type, the string appears in the interpreter's output with quotation marks and special characters. Numbers are output as numbers. The same approach applies to any other values.</p>
26 <p>In the examples above, you can see that when you type, the string appears in the interpreter's output with quotation marks and special characters. Numbers are output as numbers. The same approach applies to any other values.</p>
27 <p>Most values are output as<strong>a canonical string representation</strong>. We can copy the values presented in this way to the prompt line and execute them again. The canonical representation is usually considered correct code in Python.</p>
27 <p>Most values are output as<strong>a canonical string representation</strong>. We can copy the values presented in this way to the prompt line and execute them again. The canonical representation is usually considered correct code in Python.</p>
28 <p>You can get a canonical representation of any value using the repr function. Let's see how it works:</p>
28 <p>You can get a canonical representation of any value using the repr function. Let's see how it works:</p>
29 <p>Note that the result of the print function call doesn't contain quotation marks or special characters. It is because we can see printing a string.</p>
29 <p>Note that the result of the print function call doesn't contain quotation marks or special characters. It is because we can see printing a string.</p>
30 <h2>REPL and None</h2>
30 <h2>REPL and None</h2>
31 <p>You should also know how REPL maps the return from the function to None.</p>
31 <p>You should also know how REPL maps the return from the function to None.</p>
32 <p>The correct answer is that it doesn't. We did it on purpose. If the function doesn't return a result explicitly, it returns None. And to avoid cluttering up the REPL logs with infinite Nones, the interpreter's authors decided to suppress the output of this value.</p>
32 <p>The correct answer is that it doesn't. We did it on purpose. If the function doesn't return a result explicitly, it returns None. And to avoid cluttering up the REPL logs with infinite Nones, the interpreter's authors decided to suppress the output of this value.</p>
33 <p>It is why, in the REPL example above, we didn't see that the print function returned None.</p>
33 <p>It is why, in the REPL example above, we didn't see that the print function returned None.</p>
34 <p>But we can still see None in the REPL if needed. To do this, you have to wrap the print call in another print call:</p>
34 <p>But we can still see None in the REPL if needed. To do this, you have to wrap the print call in another print call:</p>
35 <p>Now we see this value because the second print receives None as the input and the results and prints the line already.</p>
35 <p>Now we see this value because the second print receives None as the input and the results and prints the line already.</p>
36 <p>In your work, you may encounter a situation where a function in the REPL is called but doesn't return anything. There's nothing wrong with that. Maybe, your function returns None.</p>
36 <p>In your work, you may encounter a situation where a function in the REPL is called but doesn't return anything. There's nothing wrong with that. Maybe, your function returns None.</p>
37 <h2>Entering multi-line code</h2>
37 <h2>Entering multi-line code</h2>
38 <p>You can also enter multi-line code in the Python REPL. You can't edit lines you've already entered. But it's convenient to enter small functions in this way.</p>
38 <p>You can also enter multi-line code in the Python REPL. You can't edit lines you've already entered. But it's convenient to enter small functions in this way.</p>
39 <p>Imagine you enter a line that isn't complete in terms of meaning. In this case, REPL changes the prompt to ... and waits for a new line in addition to the one already entered. This way, we can enter a whole function definition along with docstring and logic.</p>
39 <p>Imagine you enter a line that isn't complete in terms of meaning. In this case, REPL changes the prompt to ... and waits for a new line in addition to the one already entered. This way, we can enter a whole function definition along with docstring and logic.</p>
40 <p>The end of all multi-line code is an empty line, so you can't use it in your code.</p>
40 <p>The end of all multi-line code is an empty line, so you can't use it in your code.</p>
41 <p>Examples with multi-line code would look like this:</p>
41 <p>Examples with multi-line code would look like this:</p>
42 <h2>Connecting modules in the REPL</h2>
42 <h2>Connecting modules in the REPL</h2>
43 <p>You may need functions from modules built into Python. To use them, you need to import the necessary module or function:</p>
43 <p>You may need functions from modules built into Python. To use them, you need to import the necessary module or function:</p>
44 <h2>The REPL is a handy calculator</h2>
44 <h2>The REPL is a handy calculator</h2>
45 <p>The Python REPL is easy to use as a regular calculator. The only difference is that REPL doesn't remember results between calculations. To fix this, we can use a variable to store intermediate results:</p>
45 <p>The Python REPL is easy to use as a regular calculator. The only difference is that REPL doesn't remember results between calculations. To fix this, we can use a variable to store intermediate results:</p>
46 <p>Note that the assignment doesn't output any values. Built-in statements never return values. It is one reason why they can't be part of expressions.</p>
46 <p>Note that the assignment doesn't output any values. Built-in statements never return values. It is one reason why they can't be part of expressions.</p>
47 <p>Using variables helps if we need intermediate results later. But if you need the result only in the following expression, you can use the variable _.</p>
47 <p>Using variables helps if we need intermediate results later. But if you need the result only in the following expression, you can use the variable _.</p>
48 <p>It stores the result of the previous command:</p>
48 <p>It stores the result of the previous command:</p>
49 <p>Now it's more like a calculator you're used to. Intermediate results are saved and displayed after each action.</p>
49 <p>Now it's more like a calculator you're used to. Intermediate results are saved and displayed after each action.</p>
50 <p>Moreover, the _ variable stores<strong>the last successfully obtained result</strong>. If an error occurs during the execution of a line of code, the previous result won't be lost. Also, the result won't be lost if you enter statements.</p>
50 <p>Moreover, the _ variable stores<strong>the last successfully obtained result</strong>. If an error occurs during the execution of a line of code, the previous result won't be lost. Also, the result won't be lost if you enter statements.</p>
51 <p>It helps if you want to store the current value in a variable. Here's an example demonstrating error tolerance and preservation of results during execution:</p>
51 <p>It helps if you want to store the current value in a variable. Here's an example demonstrating error tolerance and preservation of results during execution:</p>
52 <p>The _ variable is only available in the REPL. We cannot declare this variable in the code, but it'll work like any other variable. Keep this in mind when transferring code from the REPL to modules.</p>
52 <p>The _ variable is only available in the REPL. We cannot declare this variable in the code, but it'll work like any other variable. Keep this in mind when transferring code from the REPL to modules.</p>