- Understanding Computer Programming
- Setting Things Up
- Writing Your First Python Program
- Summary
Writing Your First Python Program
We’re going to write a really simple program just to make sure everything is working properly. To do that we’re going to create a work folder and then write some code.
Selecting Your Work Folder
As you know, programmers organize their code in folders, which is why you just created one. Now we need to tell VS Code to use your new work folder:
Let’s go back to the buttons on the top left of the VS Code window:
The top button opens and closes the Explorer panel, which is where you can see all your files. Click that top button to show the Explorer (if it is not already open).
As you’ve not yet told VS Code where your work folder is, you’ll see a NO FOLDER OPENED display like this:
Click the Open Folder button, and you’ll see your regular (Windows or Mac) file folder screen.
Navigate to the Python folder that you just created and then click the Select Folder button.
You’ll now have an open folder with nothing in it:
VS Code now has a work folder, and we’re good to start coding.
It’s Coding Time!
Now let’s create a file and write some code. Remember the steps used to create new files, you’ll be doing this over and over (starting right in the next chapter):
Move your mouse over the PYTHON box in the Explorer on the left. See those four icons to the right of the word PYTHON? The first one creates a new file. Click it and name your file Hello.py. (The .py extension is super important. Every Python file you create must have a .py extension.)
Press Enter, and the file will be saved. You already know where the Explorer panel is. The next part of the IDE screen you need to know about is the most important one; it is the big box on the upper right, that’s the editor, where you type your code. Your newly created file should also automatically be open, ready for you to start coding. If it isn’t, just double-click on it in the Explorer panel to open it.
Now to start coding, type the following exactly as shown here in the editor part of the screen:
Let’s not worry about the code itself yet. Instead, notice that the file name is shown above the editor. This is really important when you have lots of files open—it’s how you can tell them apart.
The last important thing to look at is the green arrow above the code, over on the top right. That is used to run your code. (If you hover your mouse over the green arrow, the screen will say Run Python File in Terminal. You can hover your mouse anywhere in VS Code to see what buttons will do.)
Click the green Run button, and Python will run your code. So when you run your code, where do you see the results? The Terminal window, which is right below the editor window. You told Python to print (that means display) some text, and it does just that in the Terminal window, like this:
If this text is displayed in the Terminal window, then it means that Python is installed and working properly, VS Code is installed and can talk to your Python installation, and you are ready to code. Congratulations!