Getting started with Python

Coding with Python

🕑 This lesson will take about 20 minutes

What is Python?

Python is a high-level programming language designed by Guido van Rossum. High-level programming languages use natural language elements and are easier to use and understand than low-level programming languages like machine code. Python is an open-source programming language which means it is free to use, modify, and share

Pythonʼs clear and readable syntax (the rules and structure of words and symbols in a programming language) allows programmers to use fewer lines of code to express concepts in comparison to other languages such as C. Python is also easier to use than several other languages because it includes automated memory management meaning less work for the programmer. Python can run on multiple operating systems including Windows, Mac and Linux.

Python can be used to develop web-based applications, scientific and mathematical programs, machine learning applications, and even games and GUI-based (graphical user interface) desktop applications. Python is used by several large companies such as Google, Spotify, Instagram, NASA, and Netflix.

Why use Python?

Python is a language that focuses on clean, highly readable code that is fast to write. Python can run on multiple platforms including Windows, iOS and Linux, is useful for building powerful web applications, and a popular choice in the fast growing field of data science. Python is now the most popular language used to teach introductory computer science in the United States [source]. Eight out of the top 10 U.S. computer science departments use Python to teach the fundamentals of computer science. Python is a mature and versatile language with a large developer community. It is a powerful language that is also easy to learn compared to other languages. Python is also one of the most high-paying [source] and in-demand programming languages [source]. It is a great language to learn!

Who uses Python?

A range of companies and organisations use Python in their applications, including Google, YouTube, Reddit, Snapchat, Instagram, Spotify, Netflix, Dropbox, Blender, Pinterest, NASA, Quora, BitTorrent, and more.

Installing Python

Python is available to download for free for both Windows and Mac. Before you can start writing and testing Python code, you’ll need to install Python as well as a code editor. Follow these instructions to get started:

  1. Go to the official Python website at https://www.python.org/downloads and choose the latest version of Python to install (eg. Python 3.12.1).

  2. Once downloaded, run the Python installer.

  3. Follow the prompts in the installer and make sure that the box next to "Add Python x.x to PATH" is checked during installation. This is important for easy access to Python from the command line.

To verify that Python has installed successfully, follow these instructions:

  • On Mac, open Terminal and type in the command python --version or python3 --version and press Enter. You should see the version number displayed.

  • On Windows, open Command Prompt (by searching for "cmd" in the Start menu), type in the command python --version or python3 --version and press Enter. You should see the version number displayed.

Having trouble installing Python? You can also write and test Python code at replit.com using the cloud-based code editor.

Installing a code editor

Python does provide a basic code editor called Python IDLE which you should be able to find in your applications after installing Python on Mac or Windows. However, it’s functionality and code testing features are very limited.

It is recommended to install a code editor such as Visual Studio Code which can be download for free (for Windows or Mac) at https://code.visualstudio.com . In this tutorial series, we’ll be using Visual Studio Code to write and test Python code. We’ll also be using the Python extension for Visual Studio Code which provides some useful features for formatting and testing Python code.

To install the Python extension in Visual Studio Code, click the Extensions icon on the left toolbar (or click View > Extensions from the top menu). Then search for “python” and select the “Python extension by Microsoft. Click Install.

Screenshot of the Python extension installation page in Visual Studio Code.

Start writing Python code

Let’s make sure we can write and run Python code in Visual Studio Code. Before starting a project and running Python code, it is best to create a folder/directory to save and run our code from. To do this, click File > Open Folder. Select the folder where you’d like to save your code and then click Open.

Screenshot showing the File menu opened in VS Code, and the Open Folder option selected from the File menu.

If you are still in the Extensions menu, click the Explorer icon on the left toolbar (or click View > Explorer from the top menu). In the Explorer view, you’ll be able to view files and folders within the opened folder as well as files you’re currently editing.

To create a new Python code file, click File > New Text File from the top menu (you can also use keyboard shortcuts (eg. Command + N on Mac). Then click File > Save As to give this new file a name and save it in your folder. All Python files must end with the .py extension (eg. app.py). In this example, we’ll call the file test.py . Give the file a name ending in .py and click Save.

To get started with some basic Python code, we’ll use the print() function to display a simple message in the terminal. Write the following line of code:

print("Hello world")

Save your file by clicking File > Save (or by using the keyboard shortcut Ctrl + S on Windows or Command + S on Mac).

There are a few different ways you can test your code:

  • run the code using Terminal on Mac or Command Prompt on Windows

  • run the code using the built-in Terminal in Visual Studio Code (without debugging tools)

  • run the code in Visual Studio Code (with debugging tools)

You can try out each of these methods by following the instructions below.

Run the code using Terminal or Command Prompt

If you’re using a Mac, open the Terminal app. If you’re using a PC, open the Command Prompt app. Terminal and Command Prompt can be used to run different text-based instructions called commands and to display output. In this example, we’ll run the code in the “test.py” Python file created earlier. Let’s say this code is stored in a folder/directory called “mycode” on the Desktop. We’ll need to tell Terminal or Command Prompt to navigate to this folder (Desktop/mycode) first and then to run the code in the “test.py” file.

To navigate to a folder/directory in Terminal or Command Prompt, you can use the cd (change directory) command followed by the path to the folder/directory you wish to open. After typing a command, press the Return/Enter key to run the command. For example:

cd Desktop/mycode

Once you have navigated to the folder/directory containing the code, you can use the python or python3 command (depending on which version of Python you have installed), followed by the name of the file (eg. test.py) to run the code. For example:

python3 test.py

After running this command, the output from the print function (eg. “Hello world”) should be displayed on the next line in the terminal. The screenshot below shows the two commands followed by the output displayed in the Mac Terminal.

Screenshot showing a Terminal window on Mac. The cd Desktop/mycode command has been run followed by the python3 test.py command on separate lines. On the third line in the terminal, the output "Hello world" is displayed.

Run the code using Visual Studio Code’s terminal (without debugging tools)

Visual Studio Code (or VS Code) also has a built-in Terminal you can use to run and test your code within VS Code without having to switch windows. You can run the code with or without debugging tools that can help you diagnose any bugs/errors in your code. Make sure you have opened the folder containing your code and the Python file in VS Code. To run the code without using these debugging tools, click Run > Run Without Debugging from the top menu (or you can use a keyboard shortcut eg. F5 on Mac).

Screenshot showing the Run > Run Without Debugging menu option selected in Visual Studio Code on MacOS.

The code will automatically run without having to enter any commands, and the output will be displayed in the Terminal in VS Code. You can also click Terminal > New Terminal if you’d like to manually run commands yourself (eg. python3 test.py).

The output "Hello world" is displayed in the terminal at the bottom of the VS Code window

Run the code in Visual Studio Code’s terminal (with debugging tools)

If you’d like to test the code using the debugging tools, just click Run > Start Debugging (you might also need to choose a configuration - just select Python file which is usually the first option in the list). The Terminal will open and the code will execute. If there are any errors, then error messages will be displayed in the terminal and you can click on the Problems tab to view a list of problems detected in the code. You can also take advantage of debugging tools that can help find and remove bugs in the code.

An example of a debugging tool you can use is breakpoints. A breakpoint is like a marker in your code that tells the terminal to halt the execution of the code when it reaches the breakpoint and wait until you tell the terminal to continue executing the code (like playing and pausing the code). This allows you to stop the code during execution to see what is happening and narrow down where an error might occur.

To add a breakpoint to a line of code, click on the empty space to the left side of the line of code. A red dot will display (click again to remove the breakpoint if you wish). Now, when running the code with debugging tools in VS Code, the terminal will pause execution of the code when it reaches that breakpoint and wait for you to click Continue.

Screenshot showing the debugging tools in VS Code. A breakpoint is on line 2 and the terminal has paused execution of the code, waiting for the programmer to click Continue.

In the screenshot above, a breakpoint has been added on line 2 of the code. The code has been executed using debugging tools and the breakpoint has been reached, pausing execution of the code at the start of line 2. This means that only the first line of code has run (which is why there is only one line of output displayed in the terminal). At the top of the screen, there are different options allowing the programmer to either continue running the code or run each line step by step (single line stepping).

Formatting strings in Python

In the example code we have used so far, we have contained the string "Hello, world" between double-quotation marks. You can use single-quotation marks or double-quotation marks depending on the string you’re working with (eg. if the text contains an actual quote or an apostrophe) and you can also use triple quotation marks to contain text that occupies multiple lines.

For example, if you are using double-quotation marks to contain a string and then use double-quotation marks to place a quote within that string, then that will cause a syntax error as the string would prematurely end. Instead, you could use single-quotation marks to contain the string. Likewise, if you use single-quotation marks to contain a string, but the string has an apostrophe somewhere in it, then that would also cause a syntax error as the string would prematurely end. In this situation, you could use double-quotation marks to contain the string instead.

Here are some examples:

print('Joe says, "hello!"')

print('Joe says, "hello!"')

print("Joe's car is green.")

print(''' Joe says, "hello!". ''')

You can also use the \ escape character before quotation marks. The \ escape character allows you to include quotation marks inside a string without prematurely ending the string, for example:

print("Joe says, \"hello!\"")

What if you need to include an actual backslash (\) inside a string though? Well, you can use a double-backslash! eg. (\\).

Summary

With this code, we have used the print function to output a string value (a string is basically just a sequence of characters such as letters, numbers, punctuation marks, symbols, etc). You must use parentheses to contain the string of text you would like to output, otherwise, your code will generate a syntax error - this is a type of error that occurs when the code you have written does not follow the rules (or syntax) of the programming language. Syntax errors can occur when you forget to use parentheses or brackets when required, when you don’t use matching quotation marks or brackets, if you make a spelling mistake when referring to function names, etc.

Sample code

Review questions

  1. What is the difference between high-level and low-level programming languages? Give one example of each type of programming language.

  2. What is an open-source programming language?

  3. What happens if you type print Hello, world without the brackets and quotation marks?

  4. What does SyntaxError mean?

Next lesson: Using comments in code