lezione 3 - introduzione alla programmazione con...
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/1.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/2.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/3.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/4.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/5.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/6.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/7.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/8.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/9.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/10.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/11.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/12.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/13.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/14.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/15.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/16.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/17.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/18.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/19.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/20.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/21.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/22.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/23.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/24.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/25.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/26.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/27.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/28.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/29.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/30.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/31.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/32.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/33.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/34.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/35.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/36.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/37.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/38.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/39.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/40.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/41.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/42.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/43.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/44.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/45.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/46.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/47.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/48.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/49.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/50.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/51.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/52.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/53.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/54.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/55.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/56.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/57.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/58.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/59.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/60.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/61.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/62.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/63.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/64.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/65.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/66.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/67.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/68.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/69.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/70.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/71.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/72.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/73.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/74.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/75.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/76.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/77.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/78.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/79.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/80.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/81.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/82.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/83.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/84.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/85.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/86.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/87.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/88.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/89.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/90.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/91.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/92.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/93.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/94.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/95.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/96.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/97.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/98.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/99.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/100.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/101.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/102.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/103.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/104.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/105.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/106.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/107.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/108.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/109.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/110.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/111.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/112.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/113.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/114.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/115.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/116.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/117.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/118.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/119.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/120.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/121.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/122.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/123.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/124.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/125.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/126.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/127.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/128.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/129.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/130.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/131.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/132.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/133.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/134.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/135.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/136.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/137.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/138.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/139.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/140.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/141.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/142.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042318/5f07d4d47e708231d41ef872/html5/thumbnails/143.jpg)
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.