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

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands 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 your subtitle preferences 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 refer to our video player troubleshooting guide for assistance.

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.

00:15 It’s a bit over the top for the little thing that we’re doing, but anyways, I want to show you what’s possible in here. So, okay. Our project, we call it square, and here you see an option.

00:27 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 venv. Okay.

00:45 And this is just what it’s going to create for me now.

00:50 We wait for PyCharm to finish its duty,

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:10 So, let’s see. Nothing’s in here, but we already have a virtual environment. See, everything’s installed. We have Python installed, pip, et cetera.

01:18 And if I open up a terminal, you can see we’re already inside of that virtual environment. So, any packages that I’m going to install are just going to go right into this virtual environment.

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.

01:46 Let’s call it square as well.

01:51 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:26 So, we know we need—what do we need? Command + P. We need “Until where does it go?” We’ve been doing 5, so I’m just going to say for x in range(5):.

02:35 Automatically it indents for us—we’re already used to this. And now we’re using x = x**2 (x squared). Still the indentation, and I can print it out.

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 easier to see.

03:05 I think you can do that with Command + Plus or Command + Minus as well. Whoops. Oh, that’s some folding. We don’t want to do that, actually. But it’s a nice thing to see, actually.

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 it 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.

03:44 So it gives us this yellow thing here that tells us “Something’s a bit wrong here.” Okay. I can read what it is, just to make a new line here, and it disappears.

03:53 And our code is nicely formatted, so that’s great. Cool! And now we want to run this. One way to do that—we have our file sitting over here, so we can open up the terminal.

04:04 Where am I at the moment? All right, I’m in the right folder. I can just say python square, autocomplete, and it runs the file for me. There’s a quicker way to do that also.

04:16 I can simply come here and right-click and I can say Run ‘square’.

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.

04:52 We also have this thing called the Run Configuration that maybe you noticed. Before it said Not Set, but now it tells us that it’s this square.py file that we’re using.

05:01 So starting from now, I can just press this Run and it’s going to always run this file. If I make any changes here, it’s going to apply them. Yeah.

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.

sakthimugan on Feb. 19, 2020

Hi Dan,

how to avoid indentation for while coding in pycharm. is there any shortcut?

Martin Breuss RP Team on Feb. 19, 2020

Hi @sakthimungan, could you explain your question a bit more? I don’t currently understand what you are trying to do.

Are you wondering whether PyCharm can automate your indentation?

If so, PyCharm automaticaly does the indentation for you when e.g. defining a function, or starting a loop etc. There’s no need to press anything additionally.

Martin Breuss RP Team on Feb. 19, 2020

You can change the indentation level of a selected code line or block with the following key presses:

  • Tab: indent one level to the right
  • Shift + Tab: un-indent one level to the left

Become a Member to join the conversation.