lezione 3 - introduzione alla programmazione con...

143
Lezione 3 Introduzione alla programmazione con Python Mauro Ceccanti and Alberto Paoluzzi Dip. Informatica e Automazione – Università “Roma Tre” Dip. Medicina Clinica – Università “La Sapienza”

Upload: others

Post on 17-Jun-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Lezione 3Introduzione alla programmazione con Python

Mauro Ceccanti‡ and Alberto Paoluzzi†

†Dip. Informatica e Automazione – Università “Roma Tre”‡Dip. Medicina Clinica – Università “La Sapienza”

Page 2: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Contents

Quick introduction to Python and BiopythonPython: a great language for scienceBioPython, NumPython, SciPython, and more

Basic elements of programmingExpressions and typesVariables and assignmentStrings, escape chars and multiline stringsUser input and formatted printing

Page 3: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Contents

Quick introduction to Python and BiopythonPython: a great language for scienceBioPython, NumPython, SciPython, and more

Basic elements of programmingExpressions and typesVariables and assignmentStrings, escape chars and multiline stringsUser input and formatted printing

Page 4: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Reference sources

Main references

I Campbell et al. [2009]

I Schuerer et al. [2008]

I Schuerer and Letondal [2008]

Useful readings

I Chapman [2003]

I van Rossum [2002]

I van Rossum [1997]

Page 5: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Reference sources

Main references

I Campbell et al. [2009]

I Schuerer et al. [2008]

I Schuerer and Letondal [2008]

Useful readings

I Chapman [2003]

I van Rossum [2002]

I van Rossum [1997]

Page 6: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Reference sources

Main references

I Campbell et al. [2009]

I Schuerer et al. [2008]

I Schuerer and Letondal [2008]

Useful readings

I Chapman [2003]

I van Rossum [2002]

I van Rossum [1997]

Page 7: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Reference sources

Main references

I Campbell et al. [2009]

I Schuerer et al. [2008]

I Schuerer and Letondal [2008]

Useful readings

I Chapman [2003]

I van Rossum [2002]

I van Rossum [1997]

Page 8: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Reference sources

Main references

I Campbell et al. [2009]

I Schuerer et al. [2008]

I Schuerer and Letondal [2008]

Useful readings

I Chapman [2003]

I van Rossum [2002]

I van Rossum [1997]

Page 9: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Reference sources

Main references

I Campbell et al. [2009]

I Schuerer et al. [2008]

I Schuerer and Letondal [2008]

Useful readings

I Chapman [2003]

I van Rossum [2002]

I van Rossum [1997]

Page 10: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Reference sources

Main references

I Campbell et al. [2009]

I Schuerer et al. [2008]

I Schuerer and Letondal [2008]

Useful readings

I Chapman [2003]

I van Rossum [2002]

I van Rossum [1997]

Page 11: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Contents

Quick introduction to Python and BiopythonPython: a great language for scienceBioPython, NumPython, SciPython, and more

Basic elements of programmingExpressions and typesVariables and assignmentStrings, escape chars and multiline stringsUser input and formatted printing

Page 12: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Why Python ?

I It is free and well documented

I It runs everywhere

I It has a clean syntax

I It is relevant. Thousands of companies and academicresearch groups use it every day;

I It is well supported by tools

Page 13: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Why Python ?

I It is free and well documented

I It runs everywhere

I It has a clean syntax

I It is relevant. Thousands of companies and academicresearch groups use it every day;

I It is well supported by tools

Page 14: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Why Python ?

I It is free and well documented

I It runs everywhere

I It has a clean syntax

I It is relevant. Thousands of companies and academicresearch groups use it every day;

I It is well supported by tools

Page 15: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Why Python ?

I It is free and well documented

I It runs everywhere

I It has a clean syntax

I It is relevant. Thousands of companies and academicresearch groups use it every day;

I It is well supported by tools

Page 16: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Why Python ?

I It is free and well documented

I It runs everywhere

I It has a clean syntax

I It is relevant. Thousands of companies and academicresearch groups use it every day;

I It is well supported by tools

Page 17: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Why Python ?

I It is free and well documented

I It runs everywhere

I It has a clean syntax

I It is relevant. Thousands of companies and academicresearch groups use it every day;

I It is well supported by tools

Page 18: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

What is Python? Executive SummaryExtracted from [van Rossum, 2002]

Python is an interpreted, object-oriented, high-levelprogramming language with dynamic semantics

I high-level data structures, with dynamic typing, make itvery attractive for Rapid Application Development

I simple, easy to learn syntax emphasizes readability

I supports modules and packages, which encouragesprogram modularity and code reuse

I available free for all major platforms

Page 19: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

What is Python? Executive SummaryExtracted from [van Rossum, 2002]

Python is an interpreted, object-oriented, high-levelprogramming language with dynamic semantics

I high-level data structures, with dynamic typing, make itvery attractive for Rapid Application Development

I simple, easy to learn syntax emphasizes readability

I supports modules and packages, which encouragesprogram modularity and code reuse

I available free for all major platforms

Page 20: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

What is Python? Executive SummaryExtracted from [van Rossum, 2002]

Python is an interpreted, object-oriented, high-levelprogramming language with dynamic semantics

I high-level data structures, with dynamic typing, make itvery attractive for Rapid Application Development

I simple, easy to learn syntax emphasizes readability

I supports modules and packages, which encouragesprogram modularity and code reuse

I available free for all major platforms

Page 21: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

What is Python? Executive SummaryExtracted from [van Rossum, 2002]

Python is an interpreted, object-oriented, high-levelprogramming language with dynamic semantics

I high-level data structures, with dynamic typing, make itvery attractive for Rapid Application Development

I simple, easy to learn syntax emphasizes readability

I supports modules and packages, which encouragesprogram modularity and code reuse

I available free for all major platforms

Page 22: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

What is Python? Executive SummaryExtracted from [van Rossum, 2002]

Python is an interpreted, object-oriented, high-levelprogramming language with dynamic semantics

I high-level data structures, with dynamic typing, make itvery attractive for Rapid Application Development

I simple, easy to learn syntax emphasizes readability

I supports modules and packages, which encouragesprogram modularity and code reuse

I available free for all major platforms

Page 23: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

What is Python? increased productivityExtracted from [van Rossum, 2002]

I Since there is no compilation step, the edit-test-debug cycle isincredibly fast

I Debugging Python programs is easy: a bug or bad input will nevercause a segmentation fault

I Instead, when the interpreter discovers an error, it raises an exception

I When the program doesn’t catch the exception, the interpreter prints astack trace

I A source level debugger allows inspection of local and global variables,evaluation of arbitrary expressions, setting breakpoints, steppingthrough the code a line at a time, and so on

I The debugger is written in Python itself, testifying to Python’sintrospective power

I On the other hand, often the quickest way to debug a program is to adda few print statements to the source: the fast edit-test-debug cyclemakes this simple approach very effective

Page 24: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Comparing Python to Other LanguagesExtracted from [van Rossum, 1997]

I [Campbell et al., 2009]

I see Campbell et al. [2009]

Page 25: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Comparing Python to Other LanguagesExtracted from [van Rossum, 1997]

I [Campbell et al., 2009]

I see Campbell et al. [2009]

Page 26: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Comparing Python to Other LanguagesExtracted from [van Rossum, 1997]

I [Campbell et al., 2009]

I see Campbell et al. [2009]

Page 27: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Installingon Mac OS X and Windows

I The suggested book [Campbell et al., 2009] on Pythonprogramming is

Practical Programming:An Introduction to Computer Science Using Python

I Basic install(Python + NumPy + Wing IDE 101)

http://www.cdf.toronto.edu/∼csc108h/fall/python.shtml

Page 28: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Installingon Mac OS X and Windows

I The suggested book [Campbell et al., 2009] on Pythonprogramming is

Practical Programming:An Introduction to Computer Science Using Python

I Basic install(Python + NumPy + Wing IDE 101)

http://www.cdf.toronto.edu/∼csc108h/fall/python.shtml

Page 29: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Installingon Mac OS X and Windows

I The suggested book [Campbell et al., 2009] on Pythonprogramming is

Practical Programming:An Introduction to Computer Science Using Python

I Basic install(Python + NumPy + Wing IDE 101)

http://www.cdf.toronto.edu/∼csc108h/fall/python.shtml

Page 30: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Contents

Quick introduction to Python and BiopythonPython: a great language for scienceBioPython, NumPython, SciPython, and more

Basic elements of programmingExpressions and typesVariables and assignmentStrings, escape chars and multiline stringsUser input and formatted printing

Page 31: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Numerical PythonNumPy is the fundamental package needed for scientific computing with Python

It contains:

I a powerful N-dimensional array object

I sophisticated broadcasting functions

I basic linear algebra functions

I basic Fourier transforms

I sophisticated random number capabilities

I tools for integrating Fortran code.

I tools for integrating C/C++ code.

NumPy can also be used as an efficient multi-dimensional containerof generic data. Arbitrary data-types can be defined.This allows NumPy to seamlessly and speedily integrate with a widevariety of databases.

Page 32: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Scientific PythonSciPy: Scientific Library for Python

I open-source software for mathematics, science, andengineering

I It is also the name of a popular conference on scientificprogramming with Python

I The SciPy library depends on NumPy

I The SciPy library provides many user-friendly and efficientnumerical routines

Page 33: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Scientific PythonSciPy: Scientific Library for Python

I open-source software for mathematics, science, andengineering

I It is also the name of a popular conference on scientificprogramming with Python

I The SciPy library depends on NumPy

I The SciPy library provides many user-friendly and efficientnumerical routines

Page 34: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Scientific PythonSciPy: Scientific Library for Python

I open-source software for mathematics, science, andengineering

I It is also the name of a popular conference on scientificprogramming with Python

I The SciPy library depends on NumPy

I The SciPy library provides many user-friendly and efficientnumerical routines

Page 35: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Scientific PythonSciPy: Scientific Library for Python

I open-source software for mathematics, science, andengineering

I It is also the name of a popular conference on scientificprogramming with Python

I The SciPy library depends on NumPy

I The SciPy library provides many user-friendly and efficientnumerical routines

Page 36: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Scientific PythonSciPy: Scientific Library for Python

I open-source software for mathematics, science, andengineering

I It is also the name of a popular conference on scientificprogramming with Python

I The SciPy library depends on NumPy

I The SciPy library provides many user-friendly and efficientnumerical routines

Page 37: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

SciPy – DownloadScientific Library for Python

I Official source and binary releases of NumPy and SciPy

I A better alternative: SciPy Superpack for Python

I Biology packages

I Cookbook: this page hosts "recipes", or worked examplesof commonly-done tasks.

Page 38: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

SciPy – DownloadScientific Library for Python

I Official source and binary releases of NumPy and SciPy

I A better alternative: SciPy Superpack for Python

I Biology packages

I Cookbook: this page hosts "recipes", or worked examplesof commonly-done tasks.

Page 39: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

SciPy – DownloadScientific Library for Python

I Official source and binary releases of NumPy and SciPy

I A better alternative: SciPy Superpack for Python

I Biology packages

I Cookbook: this page hosts "recipes", or worked examplesof commonly-done tasks.

Page 40: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

SciPy – DownloadScientific Library for Python

I Official source and binary releases of NumPy and SciPy

I A better alternative: SciPy Superpack for Python

I Biology packages

I Cookbook: this page hosts "recipes", or worked examplesof commonly-done tasks.

Page 41: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

SciPy – DownloadScientific Library for Python

I Official source and binary releases of NumPy and SciPy

I A better alternative: SciPy Superpack for Python

I Biology packages

I Cookbook: this page hosts "recipes", or worked examplesof commonly-done tasks.

Page 42: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

BioPythonPython tools for computational molecular biology

I Biopython is a set of freely available tools for biologicalcomputation written in Python

I It is a distributed collaborative effort to develop Pythonlibraries and applications

I Biopython aims to address the needs of current and futurework in bioinformatics

Useful step-by-step instructions are in Biopython Installation

Page 43: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

BioPythonPython tools for computational molecular biology

I Biopython is a set of freely available tools for biologicalcomputation written in Python

I It is a distributed collaborative effort to develop Pythonlibraries and applications

I Biopython aims to address the needs of current and futurework in bioinformatics

Useful step-by-step instructions are in Biopython Installation

Page 44: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

BioPythonPython tools for computational molecular biology

I Biopython is a set of freely available tools for biologicalcomputation written in Python

I It is a distributed collaborative effort to develop Pythonlibraries and applications

I Biopython aims to address the needs of current and futurework in bioinformatics

Useful step-by-step instructions are in Biopython Installation

Page 45: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

BioPythonPython tools for computational molecular biology

I Biopython is a set of freely available tools for biologicalcomputation written in Python

I It is a distributed collaborative effort to develop Pythonlibraries and applications

I Biopython aims to address the needs of current and futurework in bioinformatics

Useful step-by-step instructions are in Biopython Installation

Page 46: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Contents

Quick introduction to Python and BiopythonPython: a great language for scienceBioPython, NumPython, SciPython, and more

Basic elements of programmingExpressions and typesVariables and assignmentStrings, escape chars and multiline stringsUser input and formatted printing

Page 47: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Python comments

Comments are to clarify code and are not interpreted by Python

I Comments start with the hash character, #, and extend tothe end of the line

I A comment may appear at the start of a line or followingwhitespace or code, but not within a string literal1

# this is the first commentSPAM = 1 # and this is the second comment

# ... and now a third!STRING = "# This is not a comment."

1Literal ≡ according with the letter of the scriptures;expression that returns itself by evaluation.

Page 48: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Using Python as a calculatorincluding comments

>>> 2+24>>> # This is a comment... 2+24>>> 2+2 # and a comment on the same line as code4>>> (50-5*6)/45>>> # Integer division returns the floor:... 7/32>>> 7/-3-3

Page 49: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Contents

Quick introduction to Python and BiopythonPython: a great language for scienceBioPython, NumPython, SciPython, and more

Basic elements of programmingExpressions and typesVariables and assignmentStrings, escape chars and multiline stringsUser input and formatted printing

Page 50: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Variables and assignment

I 3.4. Declaring variables2

2from: "DIVE INTO PYTHON – Python from novice to pro",http://www.diveintopython.org/index.html

Page 51: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Variables and assignment

I 3.4. Declaring variables2

2from: "DIVE INTO PYTHON – Python from novice to pro",http://www.diveintopython.org/index.html

Page 52: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Using Python as a CalculatorNumbers

I The interpreter acts as a simple calculator: you can type anexpression at it and it will write the value

I Expression syntax is straightforward: the operators +, -, * and /work just like in most other languages

I parentheses can be used for grouping

Page 53: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Using Python as a CalculatorNumbers

I The interpreter acts as a simple calculator: you can type anexpression at it and it will write the value

I Expression syntax is straightforward: the operators +, -, * and /work just like in most other languages

I parentheses can be used for grouping

Page 54: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Using Python as a CalculatorNumbers

I The interpreter acts as a simple calculator: you can type anexpression at it and it will write the value

I Expression syntax is straightforward: the operators +, -, * and /work just like in most other languages

I parentheses can be used for grouping

Page 55: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Using Python as a CalculatorNumbers

I The interpreter acts as a simple calculator: you can type anexpression at it and it will write the value

I Expression syntax is straightforward: the operators +, -, * and /work just like in most other languages

I parentheses can be used for grouping

Page 56: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Using Python as a CalculatorNumbers

I The equal sign (’=’) is used to assign a value to a variable

I Afterwards, no result is displayed before the nextinteractive prompt:

Page 57: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Using Python as a CalculatorNumbers

I A value can be assigned to several variablessimultaneously:

Page 58: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Using Python as a CalculatorNumbers

I Variables must be “defined” (assigned a value) before theycan be used, or an error will occur:

Page 59: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Using Python as a CalculatorNumbers

I There is full support for floating point

I operators with mixed type operands convert the integeroperand to floating point

Page 60: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Using Python as a CalculatorNumbers

I There is full support for floating point

I operators with mixed type operands convert the integeroperand to floating point

Page 61: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Using Python as a CalculatorNumbers

I There is full support for floating point

I operators with mixed type operands convert the integeroperand to floating point

Page 62: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Using Python as a CalculatorNumbers

I Complex numbers are also supported

I imaginary numbers are written with a suffix of j or J

I Complex numbers with a nonzero real component arewritten as (real+imagj), or can be created with thecomplex(real, imag) function.

Page 63: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Using Python as a CalculatorNumbers

I Complex numbers are also supported

I imaginary numbers are written with a suffix of j or J

I Complex numbers with a nonzero real component arewritten as (real+imagj), or can be created with thecomplex(real, imag) function.

Page 64: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Using Python as a CalculatorNumbers

I Complex numbers are also supported

I imaginary numbers are written with a suffix of j or J

I Complex numbers with a nonzero real component arewritten as (real+imagj), or can be created with thecomplex(real, imag) function.

Page 65: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Using Python as a CalculatorNumbers

I Complex numbers are also supported

I imaginary numbers are written with a suffix of j or J

I Complex numbers with a nonzero real component arewritten as (real+imagj), or can be created with thecomplex(real, imag) function.

Page 66: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Using Python as a CalculatorNumbers

I Complex numbers are always represented as two floatingpoint numbers, the real and imaginary part

I To extract these parts from a complex number z, use z.realand z.imag.

Page 67: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Using Python as a CalculatorNumbers

I Complex numbers are always represented as two floatingpoint numbers, the real and imaginary part

I To extract these parts from a complex number z, use z.realand z.imag.

Page 68: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Using Python as a CalculatorNumbers

I Complex numbers are always represented as two floatingpoint numbers, the real and imaginary part

I To extract these parts from a complex number z, use z.realand z.imag.

Page 69: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Using Python as a CalculatorNumbers

I The conversion functions to floating point and integer(float(), int() and long()) don‚Ät work for complex numbers

I there is no one correct way to convert a complex numberto a real number

I Use abs(z) to get its magnitude (as a float) or z.real to getits real part.

Page 70: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Using Python as a CalculatorNumbers

I The conversion functions to floating point and integer(float(), int() and long()) don‚Ät work for complex numbers

I there is no one correct way to convert a complex numberto a real number

I Use abs(z) to get its magnitude (as a float) or z.real to getits real part.

Page 71: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Using Python as a CalculatorNumbers

I The conversion functions to floating point and integer(float(), int() and long()) don‚Ät work for complex numbers

I there is no one correct way to convert a complex numberto a real number

I Use abs(z) to get its magnitude (as a float) or z.real to getits real part.

Page 72: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Using Python as a CalculatorNumbers

I In interactive mode, the last printed expression is assignedto the variable _

I This means that when you are using Python as a deskcalculator, it is somewhat easier to continue calculations

I This variable should be treated as read-only by the userI Don‚Ät explicitly assign a value to itI you would create an independent local variable with the

same name masking the built-in variable with its magicbehavior.

Page 73: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Using Python as a CalculatorNumbers

I In interactive mode, the last printed expression is assignedto the variable _

I This means that when you are using Python as a deskcalculator, it is somewhat easier to continue calculations

I This variable should be treated as read-only by the userI Don‚Ät explicitly assign a value to itI you would create an independent local variable with the

same name masking the built-in variable with its magicbehavior.

Page 74: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Using Python as a CalculatorNumbers

I In interactive mode, the last printed expression is assignedto the variable _

I This means that when you are using Python as a deskcalculator, it is somewhat easier to continue calculations

I This variable should be treated as read-only by the userI Don‚Ät explicitly assign a value to itI you would create an independent local variable with the

same name masking the built-in variable with its magicbehavior.

Page 75: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Using Python as a CalculatorNumbers

I In interactive mode, the last printed expression is assignedto the variable _

I This means that when you are using Python as a deskcalculator, it is somewhat easier to continue calculations

I This variable should be treated as read-only by the userI Don‚Ät explicitly assign a value to itI you would create an independent local variable with the

same name masking the built-in variable with its magicbehavior.

Page 76: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Contents

Quick introduction to Python and BiopythonPython: a great language for scienceBioPython, NumPython, SciPython, and more

Basic elements of programmingExpressions and typesVariables and assignmentStrings, escape chars and multiline stringsUser input and formatted printing

Page 77: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Strings

I Besides numbers, Python can also manipulate strings,which can be expressed in several ways

I They can be enclosed in single quotes or double quotes:

Page 78: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Strings

I Besides numbers, Python can also manipulate strings,which can be expressed in several ways

I They can be enclosed in single quotes or double quotes:

Page 79: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Strings

I String literals can span multiple lines in several ways

I Continuation lines can be used, with a backslash as the lastcharacter on the line indicating that the next line is a logicalcontinuation of the line:

I newlines still need to be embedded in the string using \n

I the newline following the trailing backslash is discarded

I This example would print the following:

Page 80: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Strings

I String literals can span multiple lines in several ways

I Continuation lines can be used, with a backslash as the lastcharacter on the line indicating that the next line is a logicalcontinuation of the line:

I newlines still need to be embedded in the string using \n

I the newline following the trailing backslash is discarded

I This example would print the following:

Page 81: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Strings

I String literals can span multiple lines in several ways

I Continuation lines can be used, with a backslash as the lastcharacter on the line indicating that the next line is a logicalcontinuation of the line:

I newlines still need to be embedded in the string using \n

I the newline following the trailing backslash is discarded

I This example would print the following:

Page 82: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Strings

I String literals can span multiple lines in several ways

I Continuation lines can be used, with a backslash as the lastcharacter on the line indicating that the next line is a logicalcontinuation of the line:

I newlines still need to be embedded in the string using \n

I the newline following the trailing backslash is discarded

I This example would print the following:

Page 83: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Strings

I String literals can span multiple lines in several ways

I Continuation lines can be used, with a backslash as the lastcharacter on the line indicating that the next line is a logicalcontinuation of the line:

I newlines still need to be embedded in the string using \n

I the newline following the trailing backslash is discarded

I This example would print the following:

Page 84: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Strings

I strings can be surrounded in a pair of matchingtriple-quotes: """ or ”’

I End of lines do not need to be escaped when usingtriple-quotes, but they will be included in the string

I produces the following output:

Page 85: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Strings

I strings can be surrounded in a pair of matchingtriple-quotes: """ or ”’

I End of lines do not need to be escaped when usingtriple-quotes, but they will be included in the string

I produces the following output:

Page 86: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Strings

I strings can be surrounded in a pair of matchingtriple-quotes: """ or ”’

I End of lines do not need to be escaped when usingtriple-quotes, but they will be included in the string

I produces the following output:

Page 87: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Strings

I If we make the string literal a ‚Äraw‚Ä string, sequencesare not converted to newlines, but the backslash at the endof the line, and the newline character in the source, areboth included in the string as data.

I Thus, the example:

I would print:

Page 88: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Strings

I If we make the string literal a ‚Äraw‚Ä string, sequencesare not converted to newlines, but the backslash at the endof the line, and the newline character in the source, areboth included in the string as data.

I Thus, the example:

I would print:

Page 89: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Strings

I If we make the string literal a ‚Äraw‚Ä string, sequencesare not converted to newlines, but the backslash at the endof the line, and the newline character in the source, areboth included in the string as data.

I Thus, the example:

I would print:

Page 90: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Strings

I Strings can be concatenated (glued together) with the +operator, and repeated with *:

I Two string literals next to each other are automaticallyconcatenated

I the first line above could also have been written word =’Help’ ’A’

I this only works with two literals, not with arbitrary stringexpressions

Page 91: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Strings

I Strings can be concatenated (glued together) with the +operator, and repeated with *:

I Two string literals next to each other are automaticallyconcatenated

I the first line above could also have been written word =’Help’ ’A’

I this only works with two literals, not with arbitrary stringexpressions

Page 92: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Strings

I Strings can be concatenated (glued together) with the +operator, and repeated with *:

I Two string literals next to each other are automaticallyconcatenated

I the first line above could also have been written word =’Help’ ’A’

I this only works with two literals, not with arbitrary stringexpressions

Page 93: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Strings

I Strings can be concatenated (glued together) with the +operator, and repeated with *:

I Two string literals next to each other are automaticallyconcatenated

I the first line above could also have been written word =’Help’ ’A’

I this only works with two literals, not with arbitrary stringexpressions

Page 94: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

StringsStrings can be subscripted (indexed)

I the first character has index 0

I there is no separate character type

I a character is simply a string of size one

I substrings can be specified with the slice notation: twoindices separated by a colon.

Page 95: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

StringsStrings can be subscripted (indexed)

I the first character has index 0

I there is no separate character type

I a character is simply a string of size one

I substrings can be specified with the slice notation: twoindices separated by a colon.

Page 96: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

StringsStrings can be subscripted (indexed)

I the first character has index 0

I there is no separate character type

I a character is simply a string of size one

I substrings can be specified with the slice notation: twoindices separated by a colon.

Page 97: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

StringsStrings can be subscripted (indexed)

I the first character has index 0

I there is no separate character type

I a character is simply a string of size one

I substrings can be specified with the slice notation: twoindices separated by a colon.

Page 98: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

StringsStrings can be subscripted (indexed)

I the first character has index 0

I there is no separate character type

I a character is simply a string of size one

I substrings can be specified with the slice notation: twoindices separated by a colon.

Page 99: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Strings

I Slice indices have useful defaults

I an omitted first index defaults to zero

I an omitted second index defaults to the size of the stringbeing sliced.

Page 100: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Strings

I Slice indices have useful defaults

I an omitted first index defaults to zero

I an omitted second index defaults to the size of the stringbeing sliced.

Page 101: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Strings

I Slice indices have useful defaults

I an omitted first index defaults to zero

I an omitted second index defaults to the size of the stringbeing sliced.

Page 102: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Strings

I Unlike a C string

I Python strings cannot be changed

I Assigning to an indexed position in the string results in anerror:

Page 103: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Strings

I Unlike a C string

I Python strings cannot be changed

I Assigning to an indexed position in the string results in anerror:

Page 104: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Strings

I Unlike a C string

I Python strings cannot be changed

I Assigning to an indexed position in the string results in anerror:

Page 105: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Strings

I However, creating a new string with the combined contentis easy and efficient:

I Here‚Äs a useful invariant of slice operations: s[:i] + s[i:]equals s.

Page 106: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Strings

I However, creating a new string with the combined contentis easy and efficient:

I Here‚Äs a useful invariant of slice operations: s[:i] + s[i:]equals s.

Page 107: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Strings

I Degenerate slice indices are handled gracefully:

I an index that is too large is replaced by the string size

I an upper bound smaller than the lower bound returns anempty string.

Page 108: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Strings

I Degenerate slice indices are handled gracefully:

I an index that is too large is replaced by the string size

I an upper bound smaller than the lower bound returns anempty string.

Page 109: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Strings

I Degenerate slice indices are handled gracefully:

I an index that is too large is replaced by the string size

I an upper bound smaller than the lower bound returns anempty string.

Page 110: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Strings

I Indices may be negative numbers, to start counting fromthe right:

I But note that -0 is really the same as 0, so it does notcount from the right!

Page 111: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Strings

I Indices may be negative numbers, to start counting fromthe right:

I But note that -0 is really the same as 0, so it does notcount from the right!

Page 112: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Strings

I think of the indices as pointing between characters

I with the left edge of the first character numbered 0

I Then the right edge of the last character of a string of ncharacters has index n

I The slice from i to j consists of all characters between theedges labeled i and j

Page 113: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Strings

I think of the indices as pointing between characters

I with the left edge of the first character numbered 0

I Then the right edge of the last character of a string of ncharacters has index n

I The slice from i to j consists of all characters between theedges labeled i and j

Page 114: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Strings

I think of the indices as pointing between characters

I with the left edge of the first character numbered 0

I Then the right edge of the last character of a string of ncharacters has index n

I The slice from i to j consists of all characters between theedges labeled i and j

Page 115: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Strings

I think of the indices as pointing between characters

I with the left edge of the first character numbered 0

I Then the right edge of the last character of a string of ncharacters has index n

I The slice from i to j consists of all characters between theedges labeled i and j

Page 116: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Strings

I For non-negative indices, the length of a slice is thedifference of the indices

I if both are within bounds

I For example the length of word[1:3] is 2.

The built-in function len () returns the length of a string:

Page 117: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Strings

I For non-negative indices, the length of a slice is thedifference of the indices

I if both are within bounds

I For example the length of word[1:3] is 2.

The built-in function len () returns the length of a string:

Page 118: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Strings

I For non-negative indices, the length of a slice is thedifference of the indices

I if both are within bounds

I For example the length of word[1:3] is 2.

The built-in function len () returns the length of a string:

Page 119: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Sequence Typesstr, unicode, list, tuple, buffer, xrange

strings String literals are written in single or double quotes:’xyzzy’, "frobozz".

Unicode strings specified using a preceding ’u’ character: u’abc’,u"def"

lists constructed with square brackets, separating items withcommas: [a, b, c]

tuples Tuples are constructed by the comma operator (notwithin square brackets), with or without enclosingparentheses, but an empty tuple must have theenclosing parentheses, such as a, b, c or (). A singleitem tuple must have a trailing comma, such as (d,).

buffers created by calling the builtin function buffer(). Theydon‚Ät support concatenation or repetition

xrange objects. Created by calling the builtin function buffer().They don‚Ät support concatenation or repetition

Page 120: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Sequence Typesstr, unicode, list, tuple, buffer, xrange

strings String literals are written in single or double quotes:’xyzzy’, "frobozz".

Unicode strings specified using a preceding ’u’ character: u’abc’,u"def"

lists constructed with square brackets, separating items withcommas: [a, b, c]

tuples Tuples are constructed by the comma operator (notwithin square brackets), with or without enclosingparentheses, but an empty tuple must have theenclosing parentheses, such as a, b, c or (). A singleitem tuple must have a trailing comma, such as (d,).

buffers created by calling the builtin function buffer(). Theydon‚Ät support concatenation or repetition

xrange objects. Created by calling the builtin function buffer().They don‚Ät support concatenation or repetition

Page 121: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Sequence Typesstr, unicode, list, tuple, buffer, xrange

strings String literals are written in single or double quotes:’xyzzy’, "frobozz".

Unicode strings specified using a preceding ’u’ character: u’abc’,u"def"

lists constructed with square brackets, separating items withcommas: [a, b, c]

tuples Tuples are constructed by the comma operator (notwithin square brackets), with or without enclosingparentheses, but an empty tuple must have theenclosing parentheses, such as a, b, c or (). A singleitem tuple must have a trailing comma, such as (d,).

buffers created by calling the builtin function buffer(). Theydon‚Ät support concatenation or repetition

xrange objects. Created by calling the builtin function buffer().They don‚Ät support concatenation or repetition

Page 122: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Sequence Typesstr, unicode, list, tuple, buffer, xrange

strings String literals are written in single or double quotes:’xyzzy’, "frobozz".

Unicode strings specified using a preceding ’u’ character: u’abc’,u"def"

lists constructed with square brackets, separating items withcommas: [a, b, c]

tuples Tuples are constructed by the comma operator (notwithin square brackets), with or without enclosingparentheses, but an empty tuple must have theenclosing parentheses, such as a, b, c or (). A singleitem tuple must have a trailing comma, such as (d,).

buffers created by calling the builtin function buffer(). Theydon‚Ät support concatenation or repetition

xrange objects. Created by calling the builtin function buffer().They don‚Ät support concatenation or repetition

Page 123: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Sequence Typesstr, unicode, list, tuple, buffer, xrange

strings String literals are written in single or double quotes:’xyzzy’, "frobozz".

Unicode strings specified using a preceding ’u’ character: u’abc’,u"def"

lists constructed with square brackets, separating items withcommas: [a, b, c]

tuples Tuples are constructed by the comma operator (notwithin square brackets), with or without enclosingparentheses, but an empty tuple must have theenclosing parentheses, such as a, b, c or (). A singleitem tuple must have a trailing comma, such as (d,).

buffers created by calling the builtin function buffer(). Theydon‚Ät support concatenation or repetition

xrange objects. Created by calling the builtin function buffer().They don‚Ät support concatenation or repetition

Page 124: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Sequence Typesstr, unicode, list, tuple, buffer, xrange

strings String literals are written in single or double quotes:’xyzzy’, "frobozz".

Unicode strings specified using a preceding ’u’ character: u’abc’,u"def"

lists constructed with square brackets, separating items withcommas: [a, b, c]

tuples Tuples are constructed by the comma operator (notwithin square brackets), with or without enclosingparentheses, but an empty tuple must have theenclosing parentheses, such as a, b, c or (). A singleitem tuple must have a trailing comma, such as (d,).

buffers created by calling the builtin function buffer(). Theydon‚Ät support concatenation or repetition

xrange objects. Created by calling the builtin function buffer().They don‚Ät support concatenation or repetition

Page 125: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Sequence Typesstr, unicode, list, tuple, buffer, xrange

strings String literals are written in single or double quotes:’xyzzy’, "frobozz".

Unicode strings specified using a preceding ’u’ character: u’abc’,u"def"

lists constructed with square brackets, separating items withcommas: [a, b, c]

tuples Tuples are constructed by the comma operator (notwithin square brackets), with or without enclosingparentheses, but an empty tuple must have theenclosing parentheses, such as a, b, c or (). A singleitem tuple must have a trailing comma, such as (d,).

buffers created by calling the builtin function buffer(). Theydon‚Ät support concatenation or repetition

xrange objects. Created by calling the builtin function buffer().They don‚Ät support concatenation or repetition

Page 126: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Sequence Typesstr, unicode, list, tuple, buffer, xrange

For other containers see the built-in

dict class

set class

collections module.

Page 127: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Sequence Typesstr, unicode, list, tuple, buffer, xrange

For other containers see the built-in

dict class

set class

collections module.

Page 128: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Sequence Typesstr, unicode, list, tuple, buffer, xrange

For other containers see the built-in

dict class

set class

collections module.

Page 129: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Sequence Typesstr, unicode, list, tuple, buffer, xrange

For other containers see the built-in

dict class

set class

collections module.

Page 130: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Contents

Quick introduction to Python and BiopythonPython: a great language for scienceBioPython, NumPython, SciPython, and more

Basic elements of programmingExpressions and typesVariables and assignmentStrings, escape chars and multiline stringsUser input and formatted printing

Page 131: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

User input and formatted printing

I http://docs.python.org/tutorial/inputoutput.html

Page 132: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

User input and formatted printing

I http://docs.python.org/tutorial/inputoutput.html

Page 133: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

User input and formatted printing

EXAMPLE

I file input/output

I bioinf/sw/viewer/wireframe.py

I bioinf/sw/viewer/backbone.py

I bioinf/sw/viewer/pdb.py

I bioinf/sw/viewer/basic.py

I bioinf/sw/viewer/3ETA.pdb

I bioinf/sw/viewer/2ACY.pdb

I bioinf/sw/viewer/1AQU.pdb

I bioinf/sw/viewer/FL06.py

Page 134: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

User input and formatted printing

EXAMPLE

I file input/output

I bioinf/sw/viewer/wireframe.py

I bioinf/sw/viewer/backbone.py

I bioinf/sw/viewer/pdb.py

I bioinf/sw/viewer/basic.py

I bioinf/sw/viewer/3ETA.pdb

I bioinf/sw/viewer/2ACY.pdb

I bioinf/sw/viewer/1AQU.pdb

I bioinf/sw/viewer/FL06.py

Page 135: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

User input and formatted printing

EXAMPLE

I file input/output

I bioinf/sw/viewer/wireframe.py

I bioinf/sw/viewer/backbone.py

I bioinf/sw/viewer/pdb.py

I bioinf/sw/viewer/basic.py

I bioinf/sw/viewer/3ETA.pdb

I bioinf/sw/viewer/2ACY.pdb

I bioinf/sw/viewer/1AQU.pdb

I bioinf/sw/viewer/FL06.py

Page 136: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

User input and formatted printing

EXAMPLE

I file input/output

I bioinf/sw/viewer/wireframe.py

I bioinf/sw/viewer/backbone.py

I bioinf/sw/viewer/pdb.py

I bioinf/sw/viewer/basic.py

I bioinf/sw/viewer/3ETA.pdb

I bioinf/sw/viewer/2ACY.pdb

I bioinf/sw/viewer/1AQU.pdb

I bioinf/sw/viewer/FL06.py

Page 137: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

User input and formatted printing

EXAMPLE

I file input/output

I bioinf/sw/viewer/wireframe.py

I bioinf/sw/viewer/backbone.py

I bioinf/sw/viewer/pdb.py

I bioinf/sw/viewer/basic.py

I bioinf/sw/viewer/3ETA.pdb

I bioinf/sw/viewer/2ACY.pdb

I bioinf/sw/viewer/1AQU.pdb

I bioinf/sw/viewer/FL06.py

Page 138: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

User input and formatted printing

EXAMPLE

I file input/output

I bioinf/sw/viewer/wireframe.py

I bioinf/sw/viewer/backbone.py

I bioinf/sw/viewer/pdb.py

I bioinf/sw/viewer/basic.py

I bioinf/sw/viewer/3ETA.pdb

I bioinf/sw/viewer/2ACY.pdb

I bioinf/sw/viewer/1AQU.pdb

I bioinf/sw/viewer/FL06.py

Page 139: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

User input and formatted printing

EXAMPLE

I file input/output

I bioinf/sw/viewer/wireframe.py

I bioinf/sw/viewer/backbone.py

I bioinf/sw/viewer/pdb.py

I bioinf/sw/viewer/basic.py

I bioinf/sw/viewer/3ETA.pdb

I bioinf/sw/viewer/2ACY.pdb

I bioinf/sw/viewer/1AQU.pdb

I bioinf/sw/viewer/FL06.py

Page 140: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

User input and formatted printing

EXAMPLE

I file input/output

I bioinf/sw/viewer/wireframe.py

I bioinf/sw/viewer/backbone.py

I bioinf/sw/viewer/pdb.py

I bioinf/sw/viewer/basic.py

I bioinf/sw/viewer/3ETA.pdb

I bioinf/sw/viewer/2ACY.pdb

I bioinf/sw/viewer/1AQU.pdb

I bioinf/sw/viewer/FL06.py

Page 141: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

User input and formatted printing

EXAMPLE

I file input/output

I bioinf/sw/viewer/wireframe.py

I bioinf/sw/viewer/backbone.py

I bioinf/sw/viewer/pdb.py

I bioinf/sw/viewer/basic.py

I bioinf/sw/viewer/3ETA.pdb

I bioinf/sw/viewer/2ACY.pdb

I bioinf/sw/viewer/1AQU.pdb

I bioinf/sw/viewer/FL06.py

Page 142: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

User input and formatted printing

EXAMPLE

I file input/output

I bioinf/sw/viewer/wireframe.py

I bioinf/sw/viewer/backbone.py

I bioinf/sw/viewer/pdb.py

I bioinf/sw/viewer/basic.py

I bioinf/sw/viewer/3ETA.pdb

I bioinf/sw/viewer/2ACY.pdb

I bioinf/sw/viewer/1AQU.pdb

I bioinf/sw/viewer/FL06.py

Page 143: Lezione 3 - Introduzione alla programmazione con Pythonpaoluzzi/web/did/bioinf/2011/pdf/lezione03.pdf · Lezione 3 Introduzione alla programmazione con Python Mauro Ceccantiz and

Jennifer Campbell, Paul Gries, Jason Montojo, and GregWilson. Practical Programming: An Introduction to ComputerScience Using Python. The Pragmatic Bookshelf, Raleigh,North Carolina, USA, 2009.

Brad Chapman. Biopython and why you should love it.http://www.biopython.org/DIST/docs/presentations/biopython.pdf,2003.

Katja Schuerer and Catherine Letondal. python course inbioinformatics. Technical report, Pasteur Institute, 2008.

Katja Schuerer, Corinne Maufrais, Catherine Letondal, EricDeveaud, and Marie-Agnes Petit. introduction toprogramming using python,programming course for biologistsat the pasteur institute. Technical report, Pasteur Institute,2008.

Guido van Rossum. Comparing Python to Other Languages.http://www.python.org/doc/essays/comparisons/, 1997.

Guido van Rossum. What is Python? Executive Summary.http://www.python.org/doc/essays/blurb/, 2002.