CODE WITH MARTIN

2. Setup


Python Online

When learning to write Python code, it's nice to not have to deal with installing things and dealing with files. You might also not be on a computer that you are able to install things, or even on Windows.

If you prefer, you can write Python code using an online tool such as one located here: Python Online

If that doesn't work, you can also try: Python Online Compiler

You will be able to use this to test out small pieces of Python code which will be fine for the first 12 chapters of this course. Skip to the 'Hello World' section below if you prefer to use this over installing anything on your computer.

If you use the online tool, where I talk about writing code throughout the course, ignore the comments on doing things in Visual Studio Code and use the top space in the online tool to write your Python code. You can also use the 'Run' button just underneath the code panel in the tool to run your code.

If you'd like to get setup with Python on your computer which let's us do more advanced things later on, then follow the installation sections below.

Install Python

Let's download and install Python. This allows us to run the python code that we will start writing throughout the course.

You can find Python from the official site here: Python Download

Important: Before clicking the 'Install Now' button when you launch the setup program, make sure the 'Add Python ... to PATH' check-box at the bottom of the window is checked.

Install Visual Studio Code

Python code can be written in notepad if we really wanted. However, there are tools that will make it so much easier to help us write Python code and run it at the click of a button.

The editor of choice for our journey is called Visual Studio Code, produced by Microsoft, and it's completely free.

You can find the download here: Visual Studio Code Download

Visual Studio Code Setup

Once you have both of these installed, we're almost ready to write Python code! Let's go ahead and set things up to write our first Python program.

Open Visual Studio Code if it's not already open from when you installed it. You'll have the option to select a colour theme when you first open it, choose whichever suits you.

Create a folder somewhere on your computer, this will be the location where we create python code files.

In Visual Studio Code, on the File menu, select the 'Open Folder...' option. Browse in to the folder you just created and hit the 'Select Folder' button.

Visual Studio Code will ask you to trust the folder its going to open - click 'Yes, I trust the authors'.

The left area in Visual Studio Code is your file overview. The right side is where files open for us to write in, which also appears as tabs across the top. You can close the 'Getting Started' tab if this is still showing.

On the File menu, click 'New File'. A window will now display. Type "code.py" here (without quotes), then click the 'File' menu and click 'Save' (Ctrl+S is the shortcut to save). The save dialog will open with the filename set as "code", and the 'Save as type' option set to Python. Just hit save to create the file. The file should get created inside the folder you created earlier.

A small popup in the bottom right of Visual Studio Code will now appear asking you to install extensions for Python. Click the 'Install' button. Give it a minute to complete the installation. When its ready, a new window will display with the tab title 'Get Started' - you can close this tab. You can also now close the 'Extension: Python' tab.

You might notice on the left of Visual Studio Code, its showing you the Python extension. Just click on the file looking icon in the top left to get back to the file explorer view. You should now see your code.py file window, if not, just click the 'code.py' file in the file list.

Hello World

Delete the text "code.py" that we typed earlier and then write the following. If you're using the online tool, this code goes in to the top panel on the web-site:

print("Hello World")

This one line of code is our first ever line of Python code. The word 'print' means we want to write some text to the terminal. We will see what the terminal is shortly.

The text inside the quote marks is the text that we will output to the terminal. This is contained in two brackets to conform to the rules of Python programming. We'll explain more about this later. We are now finally ready to run this Python code.

If you're using Visual Studio Code, save the file (Ctrl+S) - always remember to save your code file before trying to run it. Click on the 'Run' menu, then click 'Run Without Debugging' (or press Ctrl+F5).

If you're using the online tool, you don't need to worry about saving your code. To run your code, you use the 'Run' button located just under the code panel.

For Visual Studio Code, you will now see an area appear at the bottom of your file window with the tab 'Terminal' selected. Among all the text that appears in this terminal, you should see the words "Hello World". This is your Python code running successfully!

If you're using the online tool, the terminal is the section below the code panel. You should also see the words "Hello World" here to show your Python code running successfully.

In Visual Studio Code only, if you'd like to clear the terminal so things look a bit cleaner, add these two new lines before your print line, save, then run it:

import os
os.system('cls')
print("Hello World")

We don't need to understand at this point how these two new lines work, but for now, just know that when they appear at the top of our code file, the terminal window will clear when we run. Don't worry, you will fully understand these lines eventually as we work through the course.

Congratulations if you've made it this far! You're successfully writing Python code and running your first program.

The next chapters in this course will introduce you to the core concepts of the Python language and the basics of programming.