Getting to Know the Python Standard REPL
In this lesson, you’ll learn about the CPython standard REPL, which is available in all of the installers of this Python distribution. If you haven’t installed Python yet, then check out Python 3 Installation & Setup Guide to get set up.
00:00 Getting to Know that Python Standard REPL.
00:05 In computer programming, you’ll find two kinds of programming languages: compiled and interpreted. Compiled programming languages such as C and C++ will have a compiler program, which takes care of translating the language’s code into machine code.
00:22 This machine code is typically saved into an executable file. Once you have an executable file, you can run your program on any compatible computer system without needing the compiler or the source code. In contrast, interpreted languages such as Python need an interpreter program.
00:41 This means that you need to have a Python interpreter installed to run Python code on your computer. Some may consider this characteristic a drawback because it can make your code distribution more difficult. However, in Python, having an interpreter offers one significant advantage that comes in handy during your development and testing process.
01:02 The Python interpreter allows for what’s known as an interactive REPL (Read-Eval-Print Loop), or shell, which reads a piece of code, evaluates it, and then prints the result to the console in a loop. In this course, you’ll learn about the CPython standard REPL, which is available in all of the installers of this Python distribution.
01:22 If you don’t have CPython yet, then check out the Python 3 Installation & Setup Guide for detailed instructions.
01:31 The Python interpreter can execute Python code in two modes: script or program mode and interactive or REPL mode.
01:42 In script mode, you use the interpreter to run a source file as an executable program. In this case, Python loads the file content and runs the code line by line, following the script or program’s execution flow.
01:57 By way of contrast, interactive mode is when you launch the interpreter and use it as a platform to run code that you type in directly.
02:06 The name Python is commonly used to denote two different things, the language itself and the interpreter. In this course, you’ll find the explicit term Python interpreter only in situations where ambiguity can arise.
02:22 When you run the Python interpreter in interactive mode, you open an interactive shell, also known as an interactive or REPL session. In this shell, your keyboard is the input source, and your screen is the output destination.
02:35 In this course, you’ll find the terms interactive shell, interactive session, interpreter session, and REPL session are used interchangeably.
02:45 The input consists of Python code, which the interpreter parses and evaluates. After that’s done, the interpreter automatically displays the result on your screen, and the process starts again as a loop.
02:58 So, Python’s REPL is an interactive way to talk to your computer using the Python language. It’s like a live chat. The whole process is known as a REPL because it goes through four steps that run under the hood: reading your input, which consists of Python code as expressions and statements; evaluating your Python code, which generates a result or causes side effects; printing any output so you can check your code’s results and get immediate feedback; and looping back to step one to continue the interaction.
03:32 This feature of Python is a powerful tool that you’ll wind up needing in your Python coding adventure, especially when you’re learning the language or when you’re in the early stages of a development process. That’s because the REPL offers several benefits, which you’ll learn about next.
03:48 As a Python programmer, you’ll spend considerable time in interactive mode. This mode provides a quick way to try out ideas and code snippets, and in a REPL session, you can do some or all of the tasks seen on-screen.
04:05 As a Python developer, you’ll have many reasons to spend a lot of your time in REPL sessions, working interactively with the interpreter. Getting immediate feedback on how your code works is the most relevant benefit of using a REPL session. Interactive mode is one of the best features of Python.
04:22 It allows you to test solutions and experiment with the language in real time. If you want to know how something works, then just try it in an interactive shell.
04:32 You should consider the interactive shell as a powerful learning tool, especially if your previous programming experience has been with compiled languages that don’t provide a REPL.
04:43 While learning Python or exploring new features and concepts, you’ll note that many examples in the Python documentation, online tutorials, manuals, and courses are copied and pasted from an interactive session.
04:55 You’ll recognize them because of the REPL’s characteristic prompts, which you’ll get to know later on in the course.
05:02 With this introduction to interpreters and REPLs under your belt, you are ready to get into action. In the next section of the course, you’ll learn how to start and end a Python interactive session.
Become a Member to join the conversation.