Creating and Running a Code Snippet
00:00 All right, let’s get started writing some code. Okay, in this section, we’re going to recreate the little code snippet that we’ve been working with. Let’s hop right over into PyCharm. If I open up PyCharm, I get this option to create a new project, and I’ll do this now.
It’s going to make a new virtual environment for me. That’s just happening by default. You don’t have to worry about it. You can just see it happens. You can extend it here if you want to tweak it a bit, maybe use a different base interpreter or whatever. Or give it a different name, let’s call this one
00:55 and here we are. We’re presented with this left section that shows the document structure, and the right section—there’s nothing in there currently, but this is going to be where we have our files and where we can write our code.
01:27 I don’t have to worry about polluting the rest of my operating system. So that’s a really nice feature. Okay, so, we need a file! Let’s go ahead and create one. I’m just going to right-click here. Or also see if I can make a new file there. I right-click here, say new Python File.
And here we are. We have our canvas, we can start typing.
for x in range()—you can see all the autocomplete that is happening here. So I just type something and it shows me already what this would probably autocomplete to. I can just press Enter and it creates that for me.
02:11 There’s this command when I open a function, for example, it shows me right away what are the different arguments that I can put in there. When you lose them, like this, you can press Command + P and it’s going to show them to you again.
02:50 All right, there we go. That’s our little snippet. So, this is pretty small here. One thing you can do is you can zoom in by just using the mouse pad or your mouse scroll wheel, and make it a bit bigger. It’s easy to see.
03:14 You can press these little arrows here on the side, and there’s also this shortcut that I just discovered. You can press at that, and if you have a function definition, or if you maybe have some long docstring, you can just fold it away and continue writing your code.
03:30 All right, so we created this—oh, sorry. Actually, let’s look at this. You see this little squiggly line here? We have a linter included, so that tells us that according to PEP8, there should be a newline at the end of a Python file, and currently we don’t have one.
04:23 Or there’s a shortcut, Control + Shift + R. Clicking this runs it in a terminal. I get to see the output down here right away. So, those are some of the advantages of using an integrated development environment, where you just have everything that you need in this one program.
04:38 You don’t need to switch around between your terminal and the editor. Everything is in this one thing. This is a simple use of PyCharm for writing our simple script that we’re using. You can see it’s easy to run it.
05:10 Or you can just use Control + R. The first time you run it, this might be confusing—if you don’t have a run configuration set up here, you have to do this Control + Shift + R or simply right-click and say Run here. But the first time it needs to set up this run configuration, so you need to do this the first time. Otherwise, when you run it, just press the play or Control + R.
05:32 What I really like about this is that it makes it easy to write your code because of all the autocomplete features—there’s the help that PyCharm gives you. And it also is very easy to run it and see the output right away, so that makes development pretty simple and straightforward. Okay!
05:51 So we wrote our script, we ran it. Let’s move on and figure out what happens if something is wrong in there and how PyCharm can help us to resolve a problem like that. See you in the next video.
Become a Member to join the conversation.