Check out a free preview of the full Practical Guide to Python course

The "REPL & Running Python" Lesson is part of the full, Practical Guide to Python course featured in this preview video. Here's what you'd learn in this lesson:

Nina demonstrates how to use the Python REPL to run programs line-by-line. Using the REPL can be faster than using a Python file since it eliminates the hassle of running a full program. The output is instantaneous inside the REPL as each line is run.

Preview
Close

Transcript from the "REPL & Running Python" Lesson

[00:00:00]
>> There's one other thing that I want to talk about and that is the python REPL. What is a REPL? REPL stands for read evaluate print loop. This is an interactive Python prompt where you can type in Python programs line by line and see results right away. There are lots and lots of benefits to using a REPL.

[00:00:24]
By seeing this instantaneous output, you get instantaneous feedback, which is exactly what you need when you're learning. You want to fail fast and fail often make mistakes experiment. There's really not much that you can break. So, take full advantage of the REPL for the first few chapters. We're going to be working exclusively in the REPL and I asked that you type along with me.

[00:00:52]
Now, the downside to the REPL is you eventually will want to store your Python code for reuse. And that's what Python files are for and Python programs, you can consider the REPL more of a scratch area. So it's where you can experiment and play and try out Python code.

[00:01:14]
If you want to save the output of your REPL, you can just copy and paste that into a file, which I recommend doing if you want to go back and look at what you were working on. The REPL isn't just a tool for beginners. As a Python programmer.

[00:01:31]
I use it all the time. If I can't remember what a method does or what the return value of something is. I pop up a REPL and I can quickly answer that question for myself without having to Google or redox or whatever. So it's not just something that beginners used.

[00:01:48]
It's going to be a tool that you'll have in your toolkit for the rest of your Python career. Now you'll know that you're in a REPL. When you see three arrow lines, those are gonna indicate that a line of input was given at the prompt. If you see three arrows in the example code throughout the course, don't copy them into your own REPL.

[00:02:11]
He built in copy function on this course website should take care of that for you. Now, comments can start with the pound sign, and comments will be ignored in the REPL, both if they are on a new line, or if they follow a line of code. So you might see me add in some comments for some context, as I type.

[00:02:36]
And then lines that don't start with either of those things or, a dot means that it was output produced by running input from the prompt. So let's try this out. If you want to open the ripple. On your terminal without VS code, as long as your virtual environment is activated, you can type Python and it will bring it up for you.

[00:03:03]
And you should see these three arrows and a blinking prompt letting you know that Python is ready for input. On Mac and Linux systems, you can exit out of this by hitting Ctrl D on Windows. I think it's slightly different. I don't remember the command offhand, but I'm pretty sure the ruffle prints it out.

[00:03:26]
Either way on Linux or Windows systems if you don't remember how to. With this REPL you can just type exit as a function, and that will exit it for you. If you don't want to remember the keyboard shortcuts. Now, in VS code, we can just pull up the REPL from the command palette and have it in our integrated terminal.

[00:03:48]
So to do that Ctrl Shift p for Windows Command Shift p for Mac. Then Python, start REPL and that will bring up the REPL right in VS code for me. Now we can start typing some Python code and seeing what happens. We can type a number and see that the output is just printed out.

[00:04:18]
We can assign a variable. So in this case, I'm gonna assign the number five to X. You'll see here that nothing was printed out. That's because an assignment was made and. Nothing was returned from that assignment. So in the ripple, if you want to see the value of what's an X now, you can just type X by itself and you'll see that output again.

[00:04:43]
Once we move to files, and we don't have these handy features of the ripple, you're going to need to use the print. Function in order to see any output. We're going to talk a lot more about printing later. But for the time being, you can just pass a variable to the print function, or you can pass in a string that is delineated with quotes.

[00:05:07]
And in the REPL, that'll do the same thing as just typing the variable So now you should be all set up to work on the rest of the exercises in this course. There's a bit of a checklist. If you're missing a step, I recommend that you go back in the break and make sure that you're all set up.

[00:05:31]
You need to have Python 3.9 installed. You need to have the vs code extensionist installed. You need to have the Python extension for vs code installed. You need to have made a PI workshop directory in your home folder and navigated to it. Created a virtual environment. Activated that virtual environment and open VS code in your project directory.

[00:06:00]
If you have that, if you have code on your path, you can just navigate to a directory where you have a virtual environment opened and type code dot. That will open VS code in this project directory. It should pick up the virtual environment automatically but you'll still want to double check.

[00:06:19]
If you don't have that installed on your path. That's okay. You can just launch VS code and navigate to the project directory. It's not a prerequisite for the course, it's just a handy shortcut. And there are some steps where you can go ahead and check that you have everything.

[00:06:41]
Once you're done with setup, go ahead and open the ripple either in VS code or, on the terminal and that will set you up for the next chapter. And somebody mentioned that in Windows to exit the REPL Ctrl Z works. So yes, that is the keyboard shortcut. Somebody's in chats that they keep getting Python 3.8 point six.

[00:07:08]
That means that they might have an older version of Python installed. If that is the case, and they went through the steps to install Python 3.9. They might be able to create their virtual environment or check with the version with another more specific command. So here in my project folder, I'm going to type deactivate and that will deactivate the virtual environment that I had activated.

[00:07:37]
I am instead of typing this, I don't want any of you to deactivate your virtual environments. I'm just going to open a new window. And you'll see here that in my new window, I don't have a virtual environment. So if you have an older version of Python three.

[00:07:55]
You should have a Python 3.9 command. I believe in Windows, it's pi 3.9. And that will let you target the more specific version of Python three on your system.
>> There was one other suggestion of potentially using a REPL. If all else fails. Was that a good option or not, like got online Python owner?

[00:08:21]
>> No, because if you want to run Python programs on your system, you wanna make sure that you're all set up. For the person in chat who's on Python 3.8, that's okay, it's a modern enough version of Python 3 that, they shouldn't be too far behind. And I'm not going to cover any Python 3.9 specific features in class today, but they do want to get their environments dialed in.

[00:08:48]
Unfortunately, I'll chat about this in a little bit. Versioning and Python is something that has. There's some history there. And it's not the easiest thing to do, I imagine that that's where the history of virtual environments came from originally is to solve some of these problems. So it is possible to have multiple versions of Python on your machine.

[00:09:12]
And you wanna make sure that you're using the right one for your project.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now