Getting started with Python
Date: 23/10/2020
Time: 12:30-14:30
Downloading Python and PyCharm
Python
- Go to Python Downloads. It will show Download the latest version of Python according to the OS you are using to access the browser.
- Click on the download button to get the latest version of Python (3.9).
On Windows
In case you have been redirected to another web page. From the spreadsheet press "Windows x86-64 executable installer file" link to start the download.Alternatively, you can navigate to the Python Releases for Windows page and select "Download Windows x86-64 executable installer" from the list of releases under the Python 3.9.0 - Oct. 5, 2020 label.
On Mac
The download should start without any redirection.On Linux
Linux users can skip this part and don't download the installer of Python. Since on the installation phase, we are going to use the Advanced Package Tool (APT) which will download and install the Python repository in one step.PyCharm
- Go to PyCharm home page
- Press the Download button in the up-right corner, which redirects you to the download page, according to the OS you are using to access the browser (Windows, Mac, or Linux).
- Under the label Community, press Download.
Install Python
Check if you have Python already installed
To check if you already have the last version of Python (3.9) installed on your machine (Windows, Mac, or Linux), we are going to use a command-line interpreter - shell. Operating systems have different command-line application/interface. Windows uses Command Prompt, Mac uses Terminal, and Linux uses an application called "Console" or "Terminal" (respectively for the KDE and GNOME environments).
a) Open the Command Line Interface (CLI)
On Windows
- Open the Windows menu
- Type command in the search bar
- Select Command Prompt from the search results
On Mac
- Open the Spotlight search box in the upper right-hand corner.
- Type terminal in the search bar
- Click on Terminal or just hit return if it's the first result.
On Linux
- Open the Dash by clicking the icon of your Linux operating system appearing in the upper-left menu
- Type terminal or console in the search bar
- Select the Terminal/Console application from the results that appear
b) Check Python version
- Type
python --version
(alsopython -V
on Windows) in the command line and press return to see whether you have a default version of Python (2.7) already installed. If you don't have python installed, you should get an error message. Mac comes usually with Python 2.7 installed, hence the shell will show the current installed version. For instance:
python --version
Python 2.7.3
- Type
python3 --version
(alsopython3 -V
on Windows) in the command line and press return to check whether you have Python 3 installed, and eventually which version.
Run the installer
On Windows
- Double-click on the Python installer previously downloaded.
- A dialog box like the one below will appear
- Click on Customize installation
- On the following page you should keep all the checkboxes selected, and move to the next page
- Now you should be in the Advanced Options page. Here you need to keep checked the default options with the addition of these two check-boxes (if not already checked): Install for all users, and Add python to environment varaibles. Make sure the Customize install location has been also updated
- Click on the Install button, and wait until the end of the installation
- Finally, go to the command line and try to type
python --version
(alsopython3 -V
) and press return to check whether you correctly installed Python.


On Mac
- Double-click on the downloaded .pkg file to start the wizard.
- Follow the instructions.
- Finally, go to the command line and try to type
python --version
(alsopython3 --version
in case you have installed also Python 2) and press return to check whether you correctly installed Python.
On Linux
- Open the Command Line Interface (Terminal or Console)
- Type
sudo apt-get update
- Type
sudo apt-get install python3.9 python3-pip
- Finally, go to the command line and try to type
python --version
(alsopython3 --version
in case you have installed also Python 2) and press return to check whether you correctly installed Python.
Play with Python in the shell
Windows, Mac, and Linux users can use the shell as a Python interpreter.
- Type
python
(orpython3
) in the shell. You'll see in the first line which version of python is installed. If it still shows youpython 2.X
, typeexit()
and press return to exit python, and typepython3
to access the latest version of python installed on your computer. - In the second line you will see
>>>
. This means you accessed python in interactive mode and you can now type your commands to be executed by the shell. - Type
print("Hello world")
and press enter.
>>> print("Hello world")
Hello World
- Type
exit()
and press return to exit python interactive mode.
Install Python libraries
Some libraries/modules (e.g. csv
and collections
) are built-ins, meaning you do not need to install them, while others, developed by the larger community, must be downloaded and installed.
We can install python libraries using the pip
command in the shell.
pip
is a package management system used to install and manage software packages written in Python. You will use it whenever you want to install a Python library. In case you also have Python 2 installed on your machine, pip is renamed in pip3
.
Type pip --version
in the shell to see which version is actually installed on your machine.
Note (for Windows): If not installed, follow the instructions here to find where and which version is installed.
Install modules with pip3
We will see how to install networkx and anytree.
- To install
networkx
typepip3 install networkx
in the shell. - To install e.g.
anytree
typepip3 install anytree
in the shell.
Note: you can also type pip3 install -U <module_name>
the -U
option will upgrade all packages to the newest available version.
Whenever you want to install a library, look for the documentation page and the official name of the package to be used in the shell. Here is the list of libraries/packages you'll need to install for the hands-on classes.
Install PyCharm
> Windows
- Unpack the tar.gz folder previously downloaded (see Downloading Python and PyCharm)
- Run the
pycharm-xxxxxx.exe
file that starts the Installation Wizard. - In the window Choose Installation location press Next.
- In the window Installation Options select the checkboxes Create Desktop Shourtcut and Update PATH variable (restart needed) and press Next.
- In the next window press Install.
- Select Rebooth now and press Finish. Your laptop will restart.
- If during the installation you selected to create a shortcut on your desktop, double-click that shortcut. Otherwise, go to the PyCharm bin folder (e.g.
C:\Program Files (x86)\JetBrains\PyCharm 2011\bin
) and runpycharm.exe
,pycharm6.exe
, orpycharm.bat
. - If it prompts to import settings select do not import settings.
- Agree and sign the license, press Continue.
- Install the plugins you may want to have (e.g. Markdown support)
> Mac
- Double-click the downloaded .dmg file (see Downloading Python and PyCharm)
- Drag and drop the icon in
Application
folder. - In the Application folder, double-click the
PyCharm
icon to open the editor. - If it prompts to import settings select do not import settings.
- Agree and sign the license, press Continue.
- Install the plugins you may want to have (e.g. Markdown support)
> Linux
- Unpack the tar.gz folder previously downloaded (see Downloading Python and PyCharm)
- Enter the unpacked folder and go to bin subdirectory
- You need to run the pycharm.sh file. You should access the bin directory from shell using the
cd
command and run it from shell withsh pycharm.sh
command. Alternatively, to run a .sh file form GUI, check the guidelines provided at the following link
Note: you can also check the above installations guidelines on the Pycharm Installation Guide
Create your first project in PyCharm
- Click on Create New Project
- Change the location and the name of the project folder as you wish
- Click on the Dropdown menu Project interpreter: existing interpreter
- Select the checkbox Existing interpreter and
- Double-click on the ... icon on the right to open a new window
- On the left column select System interpreter. In the up right bar you will see the path to your interpreter (i.e. the
..\python.exe
file for windows users) - Press OK, and when you are back in the prior window finally press Create
Note: you can also check the Instructions to configure the interpreter in PyCharm
Create and run a Python script
- Right click on the folder of your project and select File > New > Python file.
- Input
hello_world
as a name of your file. It will create hello_world.py in your project folder (see the left column) - In the editor type your command
print("hello world")
and press Cmd+S (on Mac) or CTRL+s (on Windows) to save changes to the file. - From the menu select Run > Run. In the interactive window select the name of your file
hello_world
. In the bottom part of the editor will appear the result.