1
Introduction to the seminar
Sokratis Sofianopoulos
Seminar objectives
• Provide a general introduction to programming using Python
• We will be seeing
• Python Syntax
• Data types
• Variables, assignment and arithmetic operations
• Functions and program flow
• Data Structures
• Conditional operators
• Classes and Objects
2
What is Python
• A programming language is simply a tool utilized to write a series of instructions
for the computer to perform in order to solve a certain problem
• Algorithm: a formal description of how to do something
• Programming: implementation of an algorithm in some programming language
• Python is a powerful language and it is known for its simplicity and ease of use
• Also, the core philosophy of Python is summarized in the "The Zen of Python":
https://www.python.org/dev/peps/pep-0020/
• For writing Python programs you need to have python installed
https://www.python.org/downloads/
3
Conda
• An open source package management system and environment
management system that runs on Windows, macOS, and Linux
• With conda you can
• install, run and update packages and their dependencies
• Create, save, load, and switch between environments on your local
computer
• It was created for Python programs, but it can package and distribute
software for any language
• Python has its own package manager called Pip
4
The Anaconda Distribution
• Anaconda is a Python (and R) distribution that includes
• The core Python interpreter (Python version 3.7 as well as 2.7)
• 100+ python "packages" (libraries)
• Pre-installed Python libraries essential for data science
• You can download Anaconda from https://www.anaconda.com/download/
for your platform
• There is a release for all major platforms (Windows, mac and Linux)
• The installation is straightforward
• The online installation guidelines (for windows) can be found at
http://docs.anaconda.com/anaconda/install/windows/
5
Miniconda
• Miniconda is a free minimal installer for conda
• It is a small, bootstrap version of Anaconda that includes only conda,
Python, the packages they depend on, and a small number of other useful
packages, including pip, zlib and a few others
• Use the conda install command to install 720+ additional conda packages
from the Anaconda repository
6
Python IDE
• You can use your favorite IDE for writing python:
• Spyder
• IDE specifically designed for working with scientific Python development and the libraries
commonly associated with this type of work
• Spyder comes preinstalled with Anaconda!
• PyCharm (this is our IDE of choice)
• The python IDE from JetBrains. It is a commercial product, but also has a free version
• If you are a university student (or have access to a university email account) you can download the
Professional edition with a one-year subscription (can later be renewed)
• Visual Studio Code
• Microsoft's lightweight IDE and fast open source code editor
7
Pip: Properly Installing Python
8
What is pip
• Pip is an installer for Python packages
• It can install packages, list installed packages, upgrade packages, and
uninstall packages
• Think of packages like new applications for your python installation
• To install a package (let's say pandas) we enter the command
pip install pandas
• The package is now installed and we can use it in our programs (by
importing it)
• If the package depends on other packages to work, pip will also install them
9
Installing a specific version of a package
• You can also give pip a version specifier for a package using one or more of
==, >=, >, <, <=:
pip install 'pandas<2.0'
• This will install the latest version of pandas lower than 2.0 (e.g. 1.9)
• You can also combine version specifiers with a comma:
pip install 'pandas>2.0,<2.0.3'
• You can use the list command to see the packages installed in your
environment: pip list
10
Upgrading a package
• Packages are maintained and updated by their developers
• You should always upgrade the packages to their latest versions
• To upgrade a specific package that you have already installed you enter:
pip install -U pandas OR pip install --upgrade pandas
• You can also upgrade pip to it’s latest version: pip install --upgrade pip
11
Uninstalling a package
• To uninstall a package simply run
pip uninstall pandas
• There are also more functionality in pip (like getting multiple packages from
a requirements file)
• You can always find more information about pip by opening the help screen:
pip help
12
Python virtual environments
13
Python virtual environments
• Python, like most other modern programming languages, has its own
unique way of downloading and storing packages (3rd party libraries)
• Every Python project on your system will use these same directories to
store and retrieve these packages
• At first glance, this may not seem like a big deal, and it isn't really, for
system packages (packages that are part of the standard Python library), but
it does matter for site packages
14
A worst-case scenario
• Consider the following scenario where you have two projects: ProjectA and
ProjectB, both of which have a dependency on the same library, ProjectC
• The problem becomes apparent when we start requiring different versions
of ProjectC
• Maybe ProjectA needs v1.0.0, while ProjectB requires the newer v2.0.0
• This is a real problem for Python since it can’t differentiate between
versions in the site-packages directory
• So both versions would reside in the same directory with the same name
15
What Is a Virtual Environment?
• The purpose of virtual environments is to create an isolated environment
for a project
• This means that each project can have its own dependencies, regardless of
what dependencies every other project has
• The great thing about this is that there are no limits to the number of
environments you can have since they’re just directories containing a few
scripts
16
Creating a virtual environment
• There are various tools to create virtual environments in python
• The most well-known is virtualenv (pip install virtualenv)
• We will be using virtualenv to create a virtual environment:
• Open a command prompt and run: virtualenv seminarenv
• It will use the same version of Python that you are currently using
• The command will create a folder with the name seminarenv
• To activate the environment navigate to the Scripts folder of the new virtual
environment folder and run activate
• To deactivate an active environment run deactivate
• You can also freeze the environment with pip freeze > requirements.txt
17
The PyCharm IDE
What is PyCharm
• PyCharm is an integrated development environment (IDE) used in computer
programming, specifically for the Python language
• It is developed by the Czech company JetBrains
• It provides code analysis, a graphical debugger, an integrated unit tester,
integration with version control systems (VCSes), and supports web
development with Django as well as Data Science with Anaconda
• PyCharm is cross-platform, with Windows, macOS and Linux versions
• The Community Edition is released under the Apache License
• There is also a Professional Edition released under a proprietary license
19
Creating a new project with PyCharm
• Once you have run PyCharm you need to select Create a new Project
• On the next screen select Pure Python
• Select the name and location of your project
• Then we need to setup the environment used by the project. We can:
• Create a new environment
• Use an existing Python interpreter (with the base environment)
• Use an existing environment
20
Select an existing environment
• Environments created with conda live by default in the envs folder of your
Conda directory, whose path will look something like:
• % /Users/<username>/miniconda3/envs
• C:\Users\<username>\AppData\Local\Continuum\anaconda3\envs
• We can use the wizard to navigate to the specific environment and select
the seminarenv environment we just created
• Once we are ready click on Create and the new project will be created
• The above can all be found in the excellent online guide at
https://www.jetbrains.com/help/pycharm/quick-start-guide.html
21
Install, uninstall, and upgrade packages
• PyCharm provides methods for installing, uninstalling, and upgrading
Python packages for a particular Python interpreter
• By default, PyCharm uses pip to manage project packages. For Conda
environments you can use the conda package manager
• To manage Python packages for the project interpreter, select the Project
Interpreter page in the project Settings (File>Settings>Project:…)
22
Overview of the user interface
23
Let's start coding
24