3.1 Introduction

In this chapter we will check to see if you have Python installed on your computer. If you do not have Python installed then we will step through the process of installing Python. This is necessary because when you run a Python program it looks for the python interpreter that is used to execute your program or script. Without the python interpreter installed on your machine Python programs are just text files!

3.2 Check to See if Python is Installed

The first thing you should do is to see if Python 3 is already installed on your computer. First check to see that you don’t have Python installed. If it is you don’t need to do anything unless it is a very old version of Python 3 such as 3.1 or 3.2.

On a Windows 11 machine you can check the version installed by opening a Command Prompt window (this can be done by searching for Cmd in the ‘Search’ box in Windows).

Once the Command window is open try typing in python. This is illustrated below:

A command prompt window. It is to check whether Python or Python 3 is installed or not.

You may be prompted to look in the Microsoft store, you can ignore that and close the window.

Note in the above where we have tried both python and python3 in case the latest version has been installed using that name. This is not an uncommon thing to do as on some machines you may have both Python 2 and Python 3 available.

On a system such as a Mac you can use the Terminal and do the same thing. You will probably find that at least Python (2) is pre-installed for you. For example, if you type in python on a Mac you will get something like this:

A Terminal window in Mac. It is to check whether Python 2 is installed or not. It indicates that version 2 dot 7 dot 15 is installed.

This indicates that the above user has version 2.7.15 installed (note you may have another 2.x version installed).

However, be careful if you find that you have Python 2 installed on your machine; this book is focused solely on Python 3.

If you have started a Python interpreter then

  • Use quit() or exit() to exit the Python interpreter; exit() is an alias for quit() and is provided to make Python easier to use.

If Python 3 was not available, then the following steps will help you to install it.

If the correct version of Python is already available on your computer then you can skip to the next chapter. An example of what might happen if you type in Python and Python 3 is installed on a Mac is given below:

A Terminal window in Mac. It is to check whether Python 3 is installed or not. It indicates that version 3 dot 11 dot 3 is installed.

Here you can see that this machine has Python 3.11.3 installed.

3.3 Installing Python on a Windows PC

3.3.1 Step 1: Downloading Python

Python is available for a wide range of platforms from Windows, to Mac OS and Linux; you will need to ensure that you download the version for your operating system.

Python can be downloaded from the main Python web site which can be found at http://www.python.org/.

As you will see the ‘Downloads’ link is the second from the left on the large menu bar below the search field. Click on this and you will be taken to the download page; the current Python 3 version at the time of writing is Python 3.11 which is the version we will download (if you find that the version is 3.12 or later then that is fine as well). Click on the ‘Download Python 3.x.x’ link. For example, for a Windows 11 machine, using 3.11.3, you will see:

A web browser page of Python installation for step 1, downloading python. It indicates that version 3 dot 11 dot 3 is available to download.

Click on the ‘Download’ button—this may take you to a page with several different options on it—look for the ‘Recommended’ option and down load that installer.

A web browser page of Python installation. It provides 3 download options for Windows 32-bit, 64-bit, and A R M 64. 64-bit is recommended and A R M 64 is experimental.

Even if a newer version of Python is available (which is likely as the version is updated frequently) the steps should be fundamentally the same.

3.3.2 Step 2: Running the Installer

Once you have download the install open it. You will now be prompted for the location to install Python, for example:

A set up of Python installation, with step 2, running the installer. The version is 3 dot 11 dot 3 for Windows 64-bit. The checkbox for use admin privileges when installing p y dot e x e is ticked.

Note that it is easiest if you click on the ‘Add python.exe to PATH’ option as this will make it available from the command line. If you don’t do this then don’t worry, we can add Python to the PATH later on (the PATH environment variable is used by Windows to find the location of programs such as the Python interpreter).

Next select the ‘Install Now’ option and follow the installation steps.

If everything went as expected you should now see a confirmatory dialog such as:

A set up of Python installation, under step 2, running the installer. The version is 3 dot 11 dot 3 for Windows 64-bit. The setup is successful. It has the close button.

If you are on Windows now close any Command Prompt windows that you have open (the PATH variable is not updated for existing Commands). This can be done by typing in ‘exit’ or closing the window.

3.3.3 Step 3: Set up the PATH (Optional)

If you did not select ‘Add python.exe to PATH’ at the start of the installation you will now need to set up the PATH environment variable. If you did then skip onto the next step.

You can set the PATH environment variable using the system environment variables editor.

The easiest way to find this is to type ‘envir’ into the Windows search box, it will then list all applications which match this pattern including the ‘Edit the system environment variables’ editor. If you are logged into your machine as a user with ‘admin’ rights use the first one listed, if you are logged in as a user without admin rights select the ‘Edit environment variables for your account’ option.

A page to add Python path of step 3. It has options to edit the system environment variables and edit environment variables using the control panel.

One the resulting dialog select the ‘Environment Variables …’ button at the bottom of the page:

A system properties dialog box with advanced tab open, in step 3. The Environment Variables button is selected.

One the next dialog select the PATH environment variable and select Edit:

An Environment Variables dialog box. The PATH environment variable is selected. It has new, edit, and delete buttons.

Now add the location in which you installed Python, by default this will be something like.

C:\Users\<username>\AppData\Local\Programs\Python\Python311 C:\Users\<username>\AppData\Local\Programs\Python\Python311\Scripts

Note Python311 should be replaced by the version of Python you are installing if it is different.

The end result should look something like:

An Edit Environment Variable dialog box. The location C, Users, p h o e b, App Data, Local, Programs, Python, Python 3 1 1, Scripts is selected. It has new and edit buttons.

Now click on OK until all the windows are closed.

3.3.4 Step 4: Verify the Installation

Next open a new Command Prompt window and type is python as shown below:

A command prompt window in step 4, verify the installation. It is to check whether Python is installed or not. It displays the installed version as Python 3 dot 11 dot 3.

Congratulations you have installed Python and run the python interpreter!

3.3.5 Step 5: Run Some Python

Now at the ‘ >>> ’ prompt type in

print('Hello World')

Be careful to make sure you use all lowercase letters for the ‘print’ function; Python is very case sensitive which means that as far as Python is concerned print('Hello World') and Print('Hello World') are completely different things.

Also make sure that you have single quotes around the Hello World text this makes it a string. If you get this right then on Windows you should see:

A command prompt window in step 5, run some python. On the installed version of Python 3 dot 11 dot 3, it has the code to produce output as Hello World.

You have now run your first Python program. In this case your program printed out the message ‘Hello World’ (a traditional first program in most programming languages).

3.3.6 Step 6: Exit the Python Interpreter

To exit the Python interpreter use exit() or Ctrl-Z plus Return.

A command prompt window in step 6, exit the python interpreter. On the installed version of Python 3 dot 11 dot 3, it has the code to produce output as Hello World. To exit the Python interpreter, exit parentheses, is used.

3.4 Setting up on a Mac

Installing Python on a Mac in similar to installing it on a Windows machine in that you can download the Python installer for the Apple Mac from the Python Software Foundation web site (https://www.python.org). Of course this time you need to make sure you select the macOS version of the download as shown below:

A web browser page of Python installation. It indicates that the latest version 3 dot 11 dot 3 is available to download for Mac O S.

From here you can select the macOS 64-bit installer (make sure you select the appropriate one for your version of the operating system). This will download an Apple package that can be installed (it will have a name similar to python-3.11.3-macos11.pkg although the version number of Python and the macOS operating system may be different). You will need to run this installer.

When you run this installer the Python Installer wizard dialog will open as shown below:

A welcome page to the Python installation. The version is 3 dot 11 dot 3 for Mac O S 10 dot 9 or later. It has installation details with the continue button.

Step through the dialogs presented to you by the installer wizard accepting each option until the installation starts. Once the installation has completed you will be presented with a summary screen confirming the installation was successful. You can now close the installer.

This will have created a new folder in your Applications folder for Python 3.11.

Note that on a Mac which already has Python 2 installed (which is installed by default), Python 3 can be installed along side it and can be accessible via the python3 command (as shown below). You should confirm that Python has been installed successfully by opening a Terminal window and entering the Python 3 REPL:

A Terminal window in Mac. It is to check whether Python 3 is successfully installed or not. It indicates that version 3 dot 11 dot 3 is installed.

Now at the ‘>>>’ prompt type in

print('Hello World')

Be careful to make sure you use all lowercase letters for the ‘print’ function; as on a Windows machine Python is very case sensitive which means that as far as Python is concerned print('Hello World') and Print('Hello World') are completely different things.

The result should be as shown below:

A Terminal window in Mac. On the installed version of Python 3 dot 11 dot 3, it has the code to produce output as Hello World.

You can now exit the REPL using the exit() or quit().

3.5 Setting up an IDE

You can use whatever editor you want along with this book, and most chapters are IDE independent. However, if you wish to use the PyCharm editor to make life easier then this section outlines downloading, installing and getting started with this editor. Almost all other chapters are IDE independent apart from one section in the ‘Python Virtual Environments’ chapter.

3.5.1 Downloading the PyCharm IDE

PyCharm is provided by JetBrains who make tools for a variety of different languages. The PyCharm IDE can be downloaded from their site—see https://www.jetbrains.com/. Look for the menu heading ‘Developer Tools’ and select that. You will see a long list of tools, which should include PyCharm.

A page of Jet Brains. It provides a list of tools under the Developer Tools menu. Includes I D Es, plugins and services, dot net and visual studio, and languages and frameworks. PyCharm I D E is selected.

Select the PyCharm option.

On the next page displayed there should be a large button labelled ‘DOWNLOAD’ select this option:

A download page of PyCharm. The text in the center reads The Python I D E for Professional Developers. It has the Download button. The text at the bottom reads full-fledged professional or free community.

On the next page there are then two download options available: Professional and Community.

  • the Professional version is the charged for option but included several additional elements over the free community version,

  • the Community version is free.

For most of the work I do in Python the Community version is more than adequate, and it is therefore the version we will download and install (note with the Professional version you do get a free trial but will need to either pay for the full version at the end of the trial or reinstall the Community version at that point).

A download page of PyCharm. It provides 3 download options for Windows, Mac O S, and Linux, where Mac O S is selected. It has the Download button separately for professionals with a free 30-day trial with H T M L, J S, and S Q L support, that is selected, and community for free, open source.

Assuming you selected the Community edition the installer will now download, and you will be prompted to run it. Note you can ignore the request to subscribe if you want). Note that the web browser should select the correct operating system for you however, if it does not then you can manually select it above the ‘Professional’ and ‘Community’ headings.

You can now run the installer and follow the instructions provided.

3.5.2 Setting up the IDE

You need to first start the PyCharm IDE. Once started the first dialog shown to you asks if you want to import any settings you may have had for another version of PyCharm. At this point select ‘Do not import settings’.

Step through the next set of dialogs selecting the look and feel (I like the light version of the IDE), whether you want to share data with JetBrains, etc. Once you have completed this click the ‘Start PyCharm’ option.

You should now be presented with the landing screen for PyCharm:

A welcome page of PyCharm. The version is 2023 dot 1. The projects tab is selected in the left panel. The main window has options to create a new project, open, and get from V C S.

We will now create a project for you to work in. A Project in PyCharm is where you write your programs and how you configure what version of Python you are using and any libraries that you might need (such as graphics libraries etc.).

Click on the ‘New Project’ option in the landing dialog.

You will now be asked where you want to create this new project. Again you can use the default location but you will need to give it a name, we will call ours python-intro. This is done by changing the last element of the ‘Location’ field to read ‘python-intro’.

It is also worth at this stage making sure that the Python interpreter you installed has been picked up by the IDE. You can do this by opening the ‘Project Interpreter: New Virtualenv environment’ option and making sure that the Base interpreter field is populated with the installation of Python on your machine (this should be picked up automatically for you). If all is OK then select ‘Create’; if the Base Interpreter is not specified or is incorrect then click on the ‘…’ button to the right of the field and browse to the appropriate location.

The ‘New project’ dialog is shown below on the left for Windows 11 and on the right for an Apple Mac:

Two dialog boxes for the New Project. On the left, it is for Windows 11. On the right, it is for Apple Mac O S. Both have location paths, checkboxes, and radio buttons. New environment using, virtual environment, is selected in both.

Next make sure that the ‘Create a main.py welcome script’ is NOT selected as this will create a sample program for you which contains far more than a simple ‘Hello World’ message.

On opening the PyCharm project you might see a Welcome message; click ‘Close’ and the project will be set up for you.

When you open the project, you will be shown a blank work space. On the left-hand side you should see at the top of the thin bar three icons:

A screenshot of 3 icons. The icon on the top is the project view resembling a folder icon. The second icon is 3 tiny squares in an L shape. The third icon is ellipsis.

Select the top one which looks a bit like a folder—this is the project view. This will look something like:

A dialog box of the project view. It lists 3 options. 1. Path starts as python intro temp, that is selected. 2. External Libraries. 3. Scratches and Consoles.

This illustrates the structure of your project. You should ignore ‘External Libraries’ and ‘Scratches and Consoles’ and open the top node. If you called your project something other than python-intro you will find that this node has the name you entered rather than python-intro but that is fine.

When you open the top node you will see that it currently contains a directory called venv—ignore this one as well and close this node so that you should now see:

A dialog box of the project view. It lists 3 options. 1. Path starts as python intro temp. 2. External Libraries. 3. Scratches and Consoles. Under option 1, folder v e n v is selected.

We are now ready to get started.

3.6 Writing Python Programs

In this section we will use PyCharm to write our first Python program. If you are using a different IDE then the basic ideas will be the same—create file with a.py extension and run it—however the menu options and buttons may be (will be) different.

The first step will be to create a new file within PyCharm to store our ‘Hello World’ program.

First make sure that the selected node in the project tree is the top level node with the name of your project.

Next select the PyCharm File->New menu option. This will display a ‘New’ menu with the option to create a new ‘Python File’. Make sure that this is the option you select, do not select the plain File option. If you don’t select the Python File option PyCharm will not realize that you want to run the associated file as a Python program. For example:

A menu of PyCharm. The title is New and lists 7 options. 1. File. 2. New scratch file. 3. Directory. 4. Python package. 5. Python file. 6. Jupyter notebook. 7. H T M L file. PyCharm File is selected.

When you select the ‘Python File’ option PyCharm will display a new dialog allowing you to enter the name of the file you want to create. All Python files must end with a.py extension thus you could enter the name ‘main.py’ into this dialog, however the ‘.py’ is optional here as PyCharm will add that extension for you, thus you can merely enter ‘main’ if you wish:

A New Python file dialog box. It has 4 options. 1. Main. 2. Python file. 3. Python unit test. 4. Python stub. Python file is selected.

Again make sure you have selected the ‘Python file’ option and neither of the other options as they will not work with the simple Hello World program we are creating. Now press enter.

Once you have entered a filename for your program, the enter will create the file for you, for example called main.py, and display it on the right-hand side of your editor as an empty file. Note that the editor is divided into two areas, one is the project tree on the left and one is the editor window on the right, for example:

A Python file dialog box. The left panel has 3 options. The file name is main dot p y under option 1. It includes an editor window for writing programs.

Within the editor window enter the code to print out ‘Hello World’, for example:

print('Hello World')

Now save the file. For example,

A Python file dialog box. The file name is main dot p y. The code on line 1 reads print, Hello World in quotes and within parentheses.

You can now run the file. To do this look above the name of the file in the editor and check that the field next to the little green arrow head say ‘Current File’. This indicates that the IDE will run the current file when you click on the green arrow, for example:

A screenshot with a dark rectangular bar. It has a drop-down list for current file, an arrowhead, a lock-like icon, and 3 vertical dots.

Now click on the green arrow head and your program should now run. Below the left hand screen shot is from running PyCharm on a Windows 11 machine and the right-hand one is from an Apple Mac. As you can see they are very similar:

Two similar Python file dialog boxes on Windows 11 and Mac O S. The file name is main dot p y. The code on line 1 reads print, Hello World. The output is Hello World. The output on the bottom reads, process finished with exit code 0.

The output from the program is displayed in a ‘Run’ window at the bottom of the IDE. This output shows you what was run (Python and the main.py file), the output from the program ‘Hello World’ and that the process (that ran the program) exited successfully with an exit code of ‘0’.

You have now run your first Python program!

3.7 A Note on Filenames

One point to note is that file names in Python need a little care.

Firstly, you should always name your files using all lowercase letters. For example the filename utils.py is fine but the file Utils.py should be avoided (by convention).

Secondly, you should try to avoid splitting file names up into constituent words, such as my_utils.py, this is a style thing mostly, and one that not everyone (including myself) adheres to. However, if you do need to split a filename up to make it more readable always use an underbar ‘_’, never use a hyphen ‘-‘. If you do use a hyphen this will not cause a problem right now, but at some point in the future it may come back to bite you. This is because if you ever need to reference one Python file from another the hyphen ‘-‘ will break such an operation as the ‘import’ command parses the ‘-‘ as a minus sign and thus will try to subtract part of the file name from another part resulting in an error instead of just being the file name.

3.8 Online Resources

See the Python Standard Library documentation for: