Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Creating and Running a Code Snippet

Give Feedback

00:00 In this second video, we’ll create and run our little code snippet in Visual Studio Code. Once you open up a new project with VS Code, this is kind of the screen that you’re going to be greeted with.

00:11 We can see some helpful information here. I don’t want to deal with this right now, but obviously you can read over it. If I close it, what is also nice, I think—there’s a couple of other helpful features that are just shortcuts that are shown here. And we’ll look at those… let’s do it right now!

00:27 So let’s take a look at some of the most common ones. Show All Commands, Command + Shift + P—press this combination. I’m going to create a new file in here, so I say “new file” and right away, it gives me—I can make a new file here with this command, or it also shows me what’s a keyboard shortcut that I can use for that.

00:48 Press Enter here, and it creates an Untitled file here. It doesn’t really know what type of file this should be. See if I can save, yeah.

00:56 So if I just click Command + S, it allows me now to save it. I’ll save this as test.py, and we’re inside of our project folder. So we can Save, and we have our Python file here.

01:09 You can see that there’s also a symbol, here, on the left side. It shows us that it’s a Python file. This is all customizable, so if you don’t want to have these specific symbols there, you can change them, et cetera. But yeah, I’m happy with them. So let’s go ahead, maybe increase the font size a bit. I just press Command + Plus, and that will make it much easier to see…

01:31 because all we’re writing is just a little script. So for x in range(), we’re already pretty used to that. And as you see popping up here, we have code completion, that’s also called IntelliSence. It’s a mix of both code completion, which allows me to just Tab complete something—so range, you see here, I can select multiple ones and I want range, and then I open up these brackets and it gives me the documentation for this function. So I can read about it: What is it about? What do I want to do? This works both for inbuilt functions and also stuff that you’re going to write yourself. So, very useful.

02:06 Just stop in here, start is going to be implicit. Again, I just press Enter and it does automatically the indentation for me.

02:16 I can say x = x**2 (x squared).

02:21 We can see a nice syntax highlighting here. But what I also like is that if I hover over this function, it brings back up this description of what’s going on.

02:30 It might be sometimes a little bit annoying, but you can customize all of this stuff. All right. And print out our x. Here we are. And now I want to run this file. So I can save it with Command + S and you see that this circle up here changes to an X. That means I know that it’s saved.

02:50 And now if I want to run it, I can do this—let’s try to do it from the terminal first. So, I want to have a terminal, right?

02:58 I can simply go down here and pull up—and here it is, a nice little terminal.

03:06 And we are inside of the folder. I have test.py, the file I just created. I can go ahead and run it,

03:14 and I get my output right here in the terminal. Another way to do this is I can also right-click and Run Python File in Terminal, and the same thing happens.

03:27 So, these are some easy ways to just create the script and run it. We can see, we have all the nice features that we want from a modern text editor. We have syntax highlighting, we have code completion, we have inspection of the documentation of what’s going on in the functions, et cetera. So it’s an easy tool to get started, everything is relatively straightforward.

03:48 And let’s take a look at what happens if we are encountering some errors and if we want to debug this, because VS Code is actually a pretty nice tool for doing exactly that.

03:59 I’ll see you in the next video, where we’ll look into debugging.

Become a Member to join the conversation.