Loading video player…

Running Python in Interactive Mode

00:00 One of the quickest ways to start interacting with Python is in the REPL, which means starting up the Python interpreter and typing commands directly to the interpreter.

00:10 REPL stands for Read-Eval-Print Loop. When you interact with Python this way, the interpreter will read the command you enter, evaluate and execute the command, print the output to the console if there is any output, and then loop back and repeat the process.

00:28 Read, evaluate, print, loop. In short, REPL. An interactive session like this continues until you instruct the interpreter to stop. Using Python like this is a great way to test short snippets of Python code and get more familiar with the language.

00:44 So it’s perfect if you want to try things out.

00:48 So how do you get to this REPL? On Windows, you can open your Command Prompt or PowerShell application and type the py or python command to launch the Python REPL.

01:00 On Linux or macOS, open your terminal application and type python3 to launch it from the command line. Depending on your configuration, it can also work to type python instead of python3.

01:13 And once you hit Enter, you’re in the

01:16 REPL. And in the REPL, everything works exactly the same no matter which operating system you’re using. Okay, let’s explore the REPL together.

01:25 So here I’m in my terminal. Since I’m on the Mac, I can type python, and once I hit Enter, I’m entering the Python REPL. There is quite a bit of output here once you’re in the REPL, but the two most important parts are the Python version, which is 3.13.0 for me, but it could be a different one for you, and the prompt. The prompt is those three greater signs that you can see on the left of my blue block, which is my current cursor position.

01:57 These three greater signs indicate that you are talking to the Python interpreter and Python is waiting for your input. We’re good to go. Now to interact with Python, let’s type the command print( "Hello, World!", and don’t forget the closing parentheses because we’re using a print() function call to output the “Hello, World!” message.

02:21 And once you hit Enter, you can see the interpreter’s response on the next line, Hello, World!. Okay, that’s amazing. And you can tell that it’s console output because the three greater signs are not in front of the Hello, World! output.

02:37 So Python reads your input, evaluates it, then prints your output, and then there is this L for looping, and that means you’re seeing those three greater signs at the end again, Python is waiting for any input that you have for it.

02:51 Feel free to play around a little bit in the Python REPL. After all, that’s exactly the purpose of the REPL. Try things out and see how things go. And once you’re done, you can type exit() with opening and closing parentheses and hit Enter.

03:07 That way, you finish the REPL session and you’re back in the terminal.

03:14 Entering commands into the Python interpreter interactively is great when you want to do some quick testing or try out new features or explore some functionality. As you just saw, you can jump into the REPL, type some code, try things out, see how it goes, and then use exit() to jump out of the REPL again.

03:33 That’s quite cool. The disadvantage is that the code you wrote during the REPL session is basically lost if you don’t copy paste it out of the REPL. So when you want to develop longer programs that you want to edit and run repeatedly, you don’t want to retype the code into the interpreter every time or be scared that you lose all of the code.

03:55 In these situations, it’s a better idea to create Python files and run them directly. And you can do both things in the terminal. So let’s explore that next.

Become a Member to join the conversation.