Skip to content

Commit

Permalink
Improve the repl tag (#2736)
Browse files Browse the repository at this point in the history
* Rewrite the repl tag

* Fix typo: "command prompt" to "command line"

* Apply suggestions from code review

Co-authored-by: wookie184 <wookie1840@gmail.com>

* Remove trailing whitespace

* Apply suggestions from code review

---------

Co-authored-by: wookie184 <wookie1840@gmail.com>
  • Loading branch information
n0Oo0Oo0b and wookie184 committed Aug 31, 2023
1 parent 0e2fd19 commit 03069d0
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions bot/resources/tags/repl.md
Expand Up @@ -2,14 +2,19 @@
embed:
title: "Read-Eval-Print Loop (REPL)"
---
A REPL is an interactive language shell environment. It first **reads** one or more expressions entered by the user, **evaluates** it, yields the result, and **prints** it out to the user. It will then **loop** back to the **read** step.
A REPL is an interactive shell where you can execute individual lines of code one at a time, like so:
```python-repl
>>> x = 5
>>> x + 2
7
>>> for i in range(3):
... print(i)
...
0
1
2
>>>
```
To enter the REPL, run `python` (`py` on Windows) in the command line without any arguments. The `>>>` or `...` at the start of some lines are prompts to enter code, and indicate that you are in the Python REPL. Any other lines show the output of the code.

To use Python's REPL, execute the interpreter with no arguments. This will drop you into the interactive interpreter shell, print out some relevant information, and then prompt you with the primary prompt `>>>`. At this point it is waiting for your input.

Firstly you can start typing in some valid Python expressions, pressing <return> to either bring you to the **eval** step, or prompting you with the secondary prompt `...` (or no prompt at all depending on your environment), meaning your expression isn't yet terminated and it's waiting for more input. This is useful for code that requires multiple lines like loops, functions, and classes. If you reach the secondary prompt in a clause that can have an arbitrary amount of expressions, you can terminate it by pressing <return> on a blank line. In other words, for the last expression you write in the clause, <return> must be pressed twice in a row.

Alternatively, you can make use of the builtin `help()` function. `help(thing)` to get help on some `thing` object, or `help()` to start an interactive help session. This mode is extremely powerful, read the instructions when first entering the session to learn how to use it.

Lastly you can run your code with the `-i` flag to execute your code normally, but be dropped into the REPL once execution is finished, giving you access to all your global variables/functions in the REPL.

To **exit** either a help session, or normal REPL prompt, you must send an EOF signal to the prompt. In *nix systems, this is done with `ctrl + D`, and in windows systems it is `ctrl + Z`. You can also exit the normal REPL prompt with the dedicated functions `exit()` or `quit()`.
Trying to execute commands for the command-line (such as `pip install xyz`) in the REPL will throw an error. To run these commands, exit the REPL first by running `exit()` and then run the original command.

0 comments on commit 03069d0

Please sign in to comment.