Getting started with Python

Date: 23/10/2020
Time: 12:30-14:30

Downloading Python and PyCharm


Python

  1. Go to Python Downloads. It will show Download the latest version of Python according to the OS you are using to access the browser.
  2. 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

  1. Go to PyCharm home page
  2. 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).
  3. 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).

In computing, a shell is a user interface for accessing services of an operating system. It can be a command-line interface (CLI) or a graphical user interface (GUI). It enables a user to perform several operations, such as file management, installations, run processes, monitor and configure OS. We'll use it to install packages and as an interpreter for python.

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.
Note: Alternatively, go to the folder Applications/Utility and open Terminal application.
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 (also python -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 (also python3 -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 (also python3 -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 (also python3 --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 (also python3 --version in case you have installed also Python 2) and press return to check whether you correctly installed Python.
Note: in case you find troubles in the process, check the following instructions according to your specific Linux distribution

Play with Python in the shell

Windows, Mac, and Linux users can use the shell as a Python interpreter.

The interpreter is the program you’ll need to run Python code and scripts. Technically, the interpreter is a layer of software that works between your program and your computer hardware to get your code running.
  • Type python (or python3) in the shell. You'll see in the first line which version of python is installed. If it still shows you python 2.X, type exit() and press return to exit python, and type python3 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


A text file containing Python code that is intended to be directly executed by the user is called script. A file that contains Python code that is designed to be imported and used from another Python file, is called module/library.

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 type pip3 install networkx in the shell.
  • To install e.g. anytree type pip3 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


PyCharm is a lightweight IDE (Integrated Development Environment) for Python. It includes a rich text editor, an interpreter for python, a way to access the shell, and allows you to save your scripts (as .py files).
> 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 run pycharm.exe, pycharm6.exe, or pycharm.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 with sh 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

  1. Click on Create New Project
  2. Change the location and the name of the project folder as you wish
  3. Click on the Dropdown menu Project interpreter: existing interpreter
  4. Select the checkbox Existing interpreter and
  5. Double-click on the ... icon on the right to open a new window
  6. 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)
  7. 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

  1. Right click on the folder of your project and select File > New > Python file.
  2. Input hello_world as a name of your file. It will create hello_world.py in your project folder (see the left column)
  3. 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.
  4. 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.