Intermediate Python REPL & Running Python in VS Code
Transcript from the "REPL & Running Python in VS Code" Lesson
>> Nina: Using the REPL. For Introduction to Python, we worked exclusively in the REPL. For Intermediate Python, we're gonna be working in both the REPL and in Python files. REPL stands for Read Evaluate Print Loop. It's the interactive Python interpreter that lets us type commands in and quickly see the output.
>> Nina: Unlike running your Python code on a file, you also don't necessarily need to display any output in a print statement. You can just type in the name of the variable and the REPL will show you its value. This is really great for quick prototyping, debugging. As a python developer, I've worked in python for six years, I still have REPL open daily so that I can experiment with my code, look at documentation very quickly etc.
[00:00:53] So in the VS Code to open the REPL, we're gonna go to VS Code, use the keyword shortcut for our command power for Mac, that's command Shift P and we're gonna select Start REPL.
>> Nina: And that should open REPL here at the bottom.
>> Nina: Now I think that you know the best way to learn a language, the best way to learn anything is just typing along Muscle memory.
[00:01:25] So I recommend that as I'm coding, and as I'm showing concepts, and as I'm experimenting that all of the students follow along in the REPL live. That's how you'll get the most value out of this class. Just a quick tour of the REPL. These three arrows here means it's a prompt, it's ready for input.
[00:01:46] So I can type stuff in here. If I just type the name of a variable I'll see the output. If I start a if statement for example I'll see these three dots here, and that means that the REPL is awaiting more input. So here I would have to provide a level of indentation, and say the rest of my statements.
[00:02:11] As you notice in an if statement, the first time I press enter when I'm done with my code block I still see these three dots, still waiting for input. So in the REPL we'll have to press Enter just one more time to make sure that this command is executed successfully, great.
[00:02:31] Throughout the course, you will see code that looks like this. If you see these right arrows here on this label that says, PYTHON REPL, that means that the file was run in the REPL. Otherwise you'll see Python file, and that means run this code from a Python file.
[00:02:47] You can copy and paste my Python REPL code, maybe occasionally you'll see a little bit of a blip. And then you can paste it right into your REPL. You'll just have to hit Enter to run the last command, that's how I make sure that you're awake.
>> Nina: Okay, these three most important commands for the REPL are type(), dir(), and help().
[00:03:12] So if I'm not sure about the type of something I can call the built-in type class and pass in my variable, it'll let me know that a was of class int.
>> Nina: Let's say I have a string here. If I want to know all of the methods available on string cuz we're not Google.
[00:03:36] As programmers, we don't have to memorize these things. I can use the built in dir function here in pass in a class. So very quickly if I look for the type of name Type of name oops. I'm so sorry I cannot type this morning. So I'm pressing Control+C to kind of exit the statement if I make a mistake.
[00:04:07] So type of name is str. I can do dir on str. That's the class right here. And that will show me all of the methods available on the string class. The ones that we care about are the ones that don't start with the underscores or double double underscores.
[00:04:27] You can kind of consider those to be private methods. And let's say I don't remember what upper does. This is where help comes in so I can pass in either a whole type to help and it'll show me all of the help for all of the methods or if I just want to zoom in on a particular function here I could do str, and then I would just type the name of the method that I'm interested in.
[00:04:57] No additional parentheses here or anything, and this will bring up help. A little bit of documentation, it will show me what arguments are required, if any, and usually a helpful message, sometimes not so much. To exit out of the screen just hit Q. Okay, so that's the tour of the REPL, I recommend everybody has one open.
>> Nina: So, running code we'll be working with a mix of The REPL and Python files in day two. New Python files have a .py extension. To create them, just hit Command and or Control+N, depending on your operating system. And then you'll have to save that file as a .py file in order for VS Code to recognize that it's working with Python.
[00:05:47] Once you do that, you should see syntax highlighting, you should see Linting if you installed your Linter. You'll have autocomplete, a lot of kinda really nice helpful things there. So you don't have to follow along here but if I'm make a new file, and I print out paste in this from the class website well it's all white, right?
[00:06:14] And I'm not seeing any syntax highlighting. So what I have to do is save that file as hello.py, I see all my syntax highlighting. To run this file very quickly and easily using the correct interpreter You would have to open your command palette, Cmd+Shift+P, python. Run Python File in Terminal.
>> Nina: Easy peasy.
>> Nina: Lastly, the VS Code terminal window, it'll automatically pop up if you start the REPL from the Python file from the command palette. To quickly open and close the REPL on Mac it's Control and and back tick.
>> Nina: As we see here on Windows I believe it's Control and single quote.
[00:07:13] That will open and close the REPL for you. An important thing to know about the REPL if you click here in this drop down box, these are all your different if important thing about the terminal. If you click here in this drop down box you'll see tabs for all your different terminology.
[00:07:30] You'll have your REPL here your, Python, maybe a shell and this is how you can switch between them.
>> Nina: Okay that's the whirlwind tour of VS Code.