Docsity

slide programming for data science, Slide di Programmazione Avanzata

Anteprima parziale del testo

Scarica slide programming for data science e più Slide in PDF di Programmazione Avanzata solo su Docsity!

Python basics I: variables,

expressions, data structures,

if, loops, functions

Python is an interpreted, object-oriented, high-level, general purpose, programming language. Conceived in the late 80s and released in 1991 by Guido Van Rossum at the Centrum Wiskunde & Informatica in the Netherlands. Managed by the nonprofit organization Python Software Foundation ( PSF ) as an open source project. Python was named after the British comedy group Monty Python's Flying Circus!

Python

1991 – first version of Python released. 2000 – Python 2. 2008 – Python 3. Not backwards compatible with Python 2 Two main branches of Python survived: Python 2.7 and Python 3.x. Currently Python has reached version 3.13.0 (release date 7/10/24). Support to Python 2 has stopped in 2020. We are going to learn Python 3!

Python Roadmap

In most cases Python 2 and Python 3 do not differ that much. Often, adding a few parentheses will convert your Python 2 code to a perfectly fine Python 3 version. When you install Python, a tool called 2to3 will be also installed. It is a Python program that reads Python 2.x source code and applies a series of fixers to transform it into valid Python 3.x code. We will see the main differences between Python 2 and Python 3 as soon as we start coding!

Help! I found some useful Python 2 code!

What can I do?

import this – The Zen of Python

Windows

  • To install Python 3 go to https://www.python.org/downloads/ and download the executable for the latest version
  • Run the installer, just be sure to add Python to the system environment variables

Getting started - Installation

Linux

  • Python is probably already installed!
  • Check it using a terminal by executing: python --version (also python2 --version and python3 --version ) If it is not installed you can do so like this (Ubuntu): sudo apt-get update sudo apt install software-properties-common sudo add-apt-repository ppa:deadsnakes/ppa sudo apt install python3.

Getting started - Installation

By installing Python, we are installing a Python Interpreter , a program that executes other (Python) programs. The interpreter reads the program and carries out the instructions it contains. The interpreter acts as a layer between the software you write and the hardware on your machine. There are different interpreters, and can be written in different programming languages. The most common interpreter is written in C!

Getting started - Interpreter

Throughout this course we will use several Integrated Development Enviroments (IDE) : PyCharm Visual Studio Code Spyder Thonny Jupyter Notebooks/Google Colab IDLE

Getting started - IDE

VSCode Probably the most usable Python IDE. You can download it from https://code.visualstudio.com/

Getting started - IDE

Thonny A minimal yet functional Python IDE for beginners. https://thonny.org/ LabTecInf 2018/2019 - F. Becattini 18

Getting started - IDE

IDLE

IDLE ( I ntegrated D evelopment and L earning E nvironment) is an interactive interpreter where we can execute python code, it is installed with Python. 20

Getting started - IDE

LabTecInf 2018/2019 - F. Becattini

Python is an interpreted language (unlike C). This means that the code is not directly compiled into instructions for the specific hardware of your machine. Instead, the code is compiled into an intermediate language called Byte Code. This translation progenerates a platform-independent source code stored in a *.pyc file (‘’compiled py’’). To execute the code, the Byte Code is then translated at runtime by the Python Virtual Machine ( PVM )

Python under the hood