an introduction to pythonapiacoa.org/publications/teaching/python/intro-python.pdf · python and...
TRANSCRIPT
![Page 1: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/1.jpg)
An introduction to Python
Fabrice Rossi
CEREMADEUniversité Paris-Dauphine
2019
![Page 2: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/2.jpg)
Python
https://www.python.org/
I Python is a high level programminglanguage
I Python’s reference implementationis a multiplatform free software
I Python can be extended bythousands of libraries
I Python is generally considered to beeasy to learn
name = input("What's your name? ")print("Hello", name)
What's your name? John DoeHello John Doe
2
![Page 3: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/3.jpg)
Python and data science
I Python is one of the two defacto standard languages fordata science (with R)
I Python has a large collectionof high performance datascience oriented libraries
I Python is generallyconsidered to be easy toread
3
![Page 4: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/4.jpg)
Python
ProsI open source implementationI full-fledged programming
languageI strong support from a large
communityI broad coverage of data
science, statistics, etc.I high performance librariesI high quality graphicsI curated distribution
ConsI limited point-and-click
supportI rather steep learning curve
compared to an integratedsoftware
I naive code has lowperformances
I “old” language (1990) with alack of modern constructs
4
![Page 5: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/5.jpg)
Recommended installs
I Anaconda (with Python 3.x)I https://www.anaconda.com/distribution/I a python distribution: python + libraries + toolsI data science orientedI anaconda navigator for managing the distribution
I recommended tools (in Anaconda)I VS code or Spyder for Python programmingI JupyterLab for literate programming
I other IDE include PyCharmI do not use Python 2.7
5
![Page 6: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/6.jpg)
Outline
Introduction
Core concepts
Control structures
Functions
Exception handling
6
![Page 7: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/7.jpg)
Outline
Introduction
Core conceptsProgramming LanguageConsole interactionBasic data modelVariablesStringsFunctionsModules
Control structures
Functions
Exception handling
7
![Page 8: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/8.jpg)
Programming Language
DefinitionI a formal language with a strict mathematical definitionI defines syntactically correct programsI associated to a semantics
I (formal) model of the computerI effects of a program on the model
In other words...I a programming language can be used to write programs ' textsI a programming language has a strict syntax
I lexical aspects ' word spellingI grammatical aspects ' sentence level
I when a program follows the syntax, it has a proper meaning i.e.an effect on the computer on which it runs
8
![Page 9: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/9.jpg)
Programming Language
DefinitionI a formal language with a strict mathematical definitionI defines syntactically correct programsI associated to a semantics
I (formal) model of the computerI effects of a program on the model
In other words...I a programming language can be used to write programs ' textsI a programming language has a strict syntax
I lexical aspects ' word spellingI grammatical aspects ' sentence level
I when a program follows the syntax, it has a proper meaning i.e.an effect on the computer on which it runs
8
![Page 10: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/10.jpg)
A computer
Turing MachineI standard mathematical modelI too low level to a daily use
Other modelsI data oriented modelsI a model of the dataI together with a model of the execution of a program
I effects of instructions/statements on the data ' sentence levelI global flow and organization on a program ' text level
I include input/output aspects
9
![Page 11: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/11.jpg)
Interactive mode
Standard program executionI a program is written in a file (or a set of files)I in some languages the file can be translated to a more efficient
languageI the file (or its translation) is executed on a computer
Console/ShellI some languages have an associated “console” or “shell” (e.g.
Python and R)I one can type interactively program sentences and get associated
resultsI simplifies learning and testing
10
![Page 12: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/12.jpg)
Python Shell
I Python provides a shell forinteractive use
I in general integrated in aspecific window of aprogramming environment
I can be launched from thecommand line (python)
I command prompt >>>
WarningThe behavior of a program in theshell is not exactly the same as thebehavior of a program outside of theshell
11
![Page 13: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/13.jpg)
Python Shell
I Python provides a shell forinteractive use
I in general integrated in aspecific window of aprogramming environment
I can be launched from thecommand line (python)
I command prompt >>>
Python 3.7.3 (default, Mar 27 2019, 22:11:17)[GCC 7.3.0] :: Anaconda, Inc. on linuxType "help", "copyright", "credits" or "license"
for more information.>>>
WarningThe behavior of a program in theshell is not exactly the same as thebehavior of a program outside of theshell
11
![Page 14: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/14.jpg)
Python Shell
I Python provides a shell forinteractive use
I in general integrated in aspecific window of aprogramming environment
I can be launched from thecommand line (python)
I command prompt >>>
Python 3.7.3 (default, Mar 27 2019, 22:11:17)[GCC 7.3.0] :: Anaconda, Inc. on linuxType "help", "copyright", "credits" or "license"
for more information.>>> 2 + 2
WarningThe behavior of a program in theshell is not exactly the same as thebehavior of a program outside of theshell
11
![Page 15: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/15.jpg)
Python Shell
I Python provides a shell forinteractive use
I in general integrated in aspecific window of aprogramming environment
I can be launched from thecommand line (python)
I command prompt >>>
Python 3.7.3 (default, Mar 27 2019, 22:11:17)[GCC 7.3.0] :: Anaconda, Inc. on linuxType "help", "copyright", "credits" or "license"
for more information.>>> 2 + 24>>>
WarningThe behavior of a program in theshell is not exactly the same as thebehavior of a program outside of theshell
11
![Page 16: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/16.jpg)
Python Shell
I Python provides a shell forinteractive use
I in general integrated in aspecific window of aprogramming environment
I can be launched from thecommand line (python)
I command prompt >>>
Python 3.7.3 (default, Mar 27 2019, 22:11:17)[GCC 7.3.0] :: Anaconda, Inc. on linuxType "help", "copyright", "credits" or "license"
for more information.>>> 2 + 24>>> 4 ** 3
WarningThe behavior of a program in theshell is not exactly the same as thebehavior of a program outside of theshell
11
![Page 17: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/17.jpg)
Python Shell
I Python provides a shell forinteractive use
I in general integrated in aspecific window of aprogramming environment
I can be launched from thecommand line (python)
I command prompt >>>
Python 3.7.3 (default, Mar 27 2019, 22:11:17)[GCC 7.3.0] :: Anaconda, Inc. on linuxType "help", "copyright", "credits" or "license"
for more information.>>> 2 + 24>>> 4 ** 364>>>
WarningThe behavior of a program in theshell is not exactly the same as thebehavior of a program outside of theshell
11
![Page 18: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/18.jpg)
Python Shell
I Python provides a shell forinteractive use
I in general integrated in aspecific window of aprogramming environment
I can be launched from thecommand line (python)
I command prompt >>>
Python 3.7.3 (default, Mar 27 2019, 22:11:17)[GCC 7.3.0] :: Anaconda, Inc. on linuxType "help", "copyright", "credits" or "license"
for more information.>>> 2 + 24>>> 4 ** 364>>>
WarningThe behavior of a program in theshell is not exactly the same as thebehavior of a program outside of theshell
11
![Page 19: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/19.jpg)
Python Shell as a calculator
>>> 2.5 / 1.31.923076923076923>>> 2,5 / 1,3(2, 5.0, 3)>>> 1 + 2 / 31.6666666666666665>>> (1 + 2) / 31.0>>> 5 / 22.5>>> 5 // 22>>> 5 % 21>>> -5 // 2-3>>> 5 ** 53125>>> 2 ** 0.51.4142135623730951
>>> 12.5 - 4 / 511.7>>> _ + 213.7>>> 4.5 > 3.5True>>> (2.5 >= 3) or (2.5 < 3)True>>> -1 ** 0.5
-1.0>>> (-1) ** 0.5(6.123233995736766e-17+1j)>>> _ ** 2(-1+1.2246467991473532e-16j)>>> 0j0j>>> 1j ** 2(-1+0j)
12
![Page 20: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/20.jpg)
Basic data model
Numerical valuesI integersI real numbers
I decimal pointI classical scientific notation
e.g. 1.5e-3I complex numbers
I automatically used in somesituations
I real + img j
Arithmetic operationsI standard operationsI integer oriented
Logical expressionsI boolean (a.k.a. truth value)I True and False valuesI automatic integer conversion
to 1 and 0, respectively (andvice versa)
I logical operators and or notI numerical comparisons
I == and !=I <= and < (and reversed
ones)
13
![Page 21: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/21.jpg)
Basic data model
Numerical valuesI integersI real numbers
I decimal pointI classical scientific notation
e.g. 1.5e-3I complex numbers
I automatically used in somesituations
I real + img j
Arithmetic operationsI standard operationsI integer oriented
Logical expressionsI boolean (a.k.a. truth value)I True and False valuesI automatic integer conversion
to 1 and 0, respectively (andvice versa)
I logical operators and or notI numerical comparisons
I == and !=I <= and < (and reversed
ones)
13
![Page 22: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/22.jpg)
Basic data model
SyntaxI literal values (spelling)
I e.g. numbers and truthvalues
I Python specifies how towrite them
I e.g. 1,5 is not a realnumber!
I operations (grammar)I writing rules are similar to
mathematical onesI with exceptions such as
I == for equalityI ** for exponentiationI etc.
SemanticsI interpretation off the symbols
and of the expressions suchas:I calculation orderingI == tests for equalityI ** can produce complex
numbersI _ is the last value
computedI error cases
>>> 0/0Traceback (most recent call last):File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero
14
![Page 23: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/23.jpg)
Objects and variables
ObjectsI Python manipulates objectsI each object has a type
I specifies the possiblevalues
I specifies the possibleoperations
I examplesI 2 is an intI 2.5 is a floatI True is a boolI 1+2j is a complex
VariablesI objects can be namedI a variable is a name for an
objectI setting/binding a name:
variable = object
I when a name appears in anexpression it is replaced bythe object
I example>>> x = 2>>> 2 * x4
15
![Page 24: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/24.jpg)
Examples
>>> x = 4>>> y = 3>>> y / x0.75>>> zTraceback (most recent call last):File "<stdin>", line 1, in <module>
NameError: name 'z' is not defined>>> y / XTraceback (most recent call last):File "<stdin>", line 1, in <module>
NameError: name 'X' is not defined>>> z = x>>> z4>>> x = 3>>> z4>>> y = z < x>>> yFalse
Key pointsI (obvious) sequential modelI no default bindingI case dependantI aliases: several names for
a given objectI variable = variable
does not bind the namestogether
I unconstrained rebinding
16
![Page 25: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/25.jpg)
Texts
>>> "A text"'A text'>>> 'Another text''Another text'>>> '''yet... another... text''''yet\nanother\ntext'>>> ''''>>> u = 'my text'>>> u * 2'my textmy text'>>> t = ' is mine!'>>> u + t'my text is mine!'>>> 2 + tTraceback (most recent call last):File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
StringsI type str (string)I literal ' ' or " "
I multiline with ''' '''
I concatenationI types are not compatible in
general!
17
![Page 26: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/26.jpg)
Slices
>>> x = 'abcdefg'>>> x[0]'a'>>> x[4]'e'>>> x[-1]'g'>>> x[0:3]'abc'>>> x[:4]'abcd'>>> x[2:]'cdefg'>>> x[-3:]'efg'>>> x[:-3]'abcd'>>> x[7]Traceback (most recent call last):File "<stdin>", line 1, in <module>
IndexError: string index out of range
IndexingI some Python objects can be
indexedI [index]
I numbering always start at 0I negative indexing enables
reverse orderingI slices a:b
I from a to b-1I missing a: 0I missing b: last index + 1
I negative slicing: same logic
18
![Page 27: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/27.jpg)
Functions
Additional actionsI objects can be manipulated with
more than operatorsI functions provide such additional
actionsI a function
I has a nameI needs 0 or more argument(s)I possibly returns an object
I using a functionI function callI function(argument_1, argument_2)
I function()
>>> len('abcd')4>>> type(2)<class 'int'>>>> type('2')<class 'str'>>>> complex(2,-1)(2-1j)>>> int(2.4)2>>> round(17.23,1)17.2>>> str(3)'3'>>> abs(-4)4>>> x = 2>>> 'x=' + str(x)'x=2'
19
![Page 28: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/28.jpg)
Functions
Functions are objectsI type function for general
functionsI specific type for built in
functionsI all standard properties apply
I new namesI function as an argument to
a function
>>> len<built-in function len>>>> type(len)<class 'builtin_function_or_method'>>>> foo = len>>> foo<built-in function len>>>> foo('abc')3>>> str(foo)'<built-in function len>'
20
![Page 29: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/29.jpg)
Methods
Functions for objectsI methods are specific
functions associated to someobject types
I special calling syntaxobject.function()
I equivalent toType.function(object)
>>> 'bla'.capitalize()'Bla'>>> 'tototi'.find('t')0>>> 'tototi'.find('ti')4>>> foo = 'et' * 3>>> foo'etetet'>>> foo.upper()'ETETET'>>> foo.count('et')3
21
![Page 30: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/30.jpg)
Modules
Extending PythonI modules provide new
functions and typesI a module must be imported
to have access to its contentI default module sys
Importing modulesI import module gives
access to the names in themodule via module.name
I import module as blaturns that into bla.name
>>> import math>>> math.pi3.141592653589793>>> math.factorial(20)2432902008176640000>>> math.log(2)0.6931471805599453>>> math.ceil(3.4)4>>> import random as rd>>> rd.random()0.9786544666626154>>> rd.random()0.7496554473100112>>> rd.randint(1,10)2>>> rd.randint(1,10)8
22
![Page 31: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/31.jpg)
Outline
Introduction
Core concepts
Control structuresNon interactive PythonConditional executionLoops
Functions
Exception handling
23
![Page 32: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/32.jpg)
Input and output
Console limitationI has to be used interactivelyI commands are not savedI reproducibility is not
guaranteed
ScriptsI normal simple python
programs are scriptI a script: a text file (generally
ending with .py)I a script is executed by the
python interpreter
OutputsI to output something, use the
print functionI for instance
print(2, 'toto')x = 3print(x, 2 * x, 2 ** x)
will print2 toto3 6 8
InputsI to input something, use the
input functionI returns always a string str
I convert if needed
24
![Page 33: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/33.jpg)
Conditional execution
Execute if...I programs can include parts
that are executed only ifsome condition is fulfilled
I the condition is written as aBoolean expression
General formif expression:
statement_1statement_2...statement_n
rest of the program
if in PythonI if is a compound statementI it consists a clause
comprisingI a header
if expression:I a suite whose execution is
controlled by the headerI in general the suite (a.k.a.
the body) is made of a seriesof indented statements
Semanticsthe body is executed if and only ifthe expression of the clauseevaluates to True
25
![Page 34: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/34.jpg)
More conditional execution
Other clauses in ifI a if statement can contain
I one or more elif clausesI one else clause
I general formif expression_1:
statement_1...statement_n
elif expression_2:...
elif expresion_3:...
else:...
rest of the program
SemanticsThe compound instruction is executed asfollows
I the expression of the if header isevaluated
I if the value is True then the body isexecuted and the execution resumes forthe rest of the program
I if the value is False the body is ignoredthe execution resumes on the secondclause
I for each elif header, the execution
follows the same pattern:I if the corresponding expression
is True the body of the clause isexecuted, followed by the rest ofthe program
I if not the execution resumes onthe next clause
I if all expressions evaluate to False thebody of the else clause is executed
26
![Page 35: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/35.jpg)
Repeating instructions
Multiple executionsI programs can include parts
that are executed severaltimes
I repetitions can be conditionalor numbered
Conditional loopwhile expression:
statement_1...statement_n
rest of the program
while in PythonI compound statement (single
clause)I while expression: is
the header of the clause
SemanticsI the expression of the header is
evaluatedI if the value is True
I the body is executedI the execution resumes on clause
itself!
I if the value is False theexecution resumes for the rest ofthe program
27
![Page 36: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/36.jpg)
Iteration
Iterable objectsI objects which can be
decomposed into severalother objects
I the content of an iterableobject is arranged in acertain order
I iterating over the objectmeans accessing in order toits elements
StringsI string "content": charactersI iterating a string: in character
order!I 'foobar' gives 'f', 'o',
'o', 'b', 'a' and 'r'
28
![Page 37: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/37.jpg)
Iterating iterables
For loopsI specific loop for iterablesI the loop execute a code for
each value contained in theiterable
General formfor variable in expression:
statement_1...statement_n
rest of the program
for in PythonI compound statement (single
clause)I for variable in expression: is
the header of the clauseI the expression of the header
must evaluate to an iterableobject
29
![Page 38: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/38.jpg)
For semantics
SemanticsI the expression is evaluated
to get an iterable objectI for each object in the iterable
I the variable is bound tothe object
I the body of the clause isexecuted
I then the execution of the restof the program resumes
I if the iterable is empty, the forloop does not execute (noerror)
ExampleThe programfor x in 'foobar':
print(x)
printsfoobar
30
![Page 39: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/39.jpg)
Ranges
Repeating n times someoperationsI very common caseI easy to do with a while but
not immediately obviousk = 0while k < n:
somethingto repeatn timesk = k + 1
more statements
range objectsI integer range iterableI range(n): integers from 0
to n-1 (n values)I simpler solution
for k in range(n):somethingto repeatn times
more statements
I clearer for pythonprogrammer
31
![Page 40: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/40.jpg)
Ranges
More rangesI range operates in a similar way to slicesI range(end): integers from 0 to end-1
I range(begin, end): integers from begin to end-1I range(begin, end, step): integers from begin to end-1 by
increments of stepI range(1, 4, 2) : 1 and 3I range(1, 5, 2) : 1 and 3
I works with negative incrementsI range(5, 2) : emptyI range(5, 2, -1): 5, 4 and 3
32
![Page 41: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/41.jpg)
Outline
Introduction
Core concepts
Control structures
FunctionsDefining functionsNamespacesRecursive functionsParameters and arguments
Exception handling
33
![Page 42: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/42.jpg)
Defining functions
Benefits of user definedfunctionsI provide program organizationI reduce code repetitionI enable using generic
functionalities
Exampledef onemore(x):
return x + 1
General formdef function_name(p_1,...,p_n):
statement_1...statement_n
returnI the return statement
defines the value of thefunction
I it terminates the functionexecution
VocabularyI the code above is a function
definitionI p_1,...,p_n are the
formal parameters of thefunction (possible none)
I the statements form the bodyof the function
34
![Page 43: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/43.jpg)
Calling a function
Definition versus callI the function definition only
makes it available in the restof the program
I a (standard) function call isneeded to use itfunction_name(a_1,...,a_n)
I the expressiona_1,...,a_n are thearguments of the call
Semanticsa function call is evaluated as follows
1. arguments are evaluated
2. a new namespace is created
3. formal parameters becomevariables in the new namespaceand are bound to thecorresponding arguments
4. the body of the function isexecuted
5. the namespace is discarded
6. the value of the function call isthe result of the execution of thebody
35
![Page 44: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/44.jpg)
Example
Program1 def onemore(x):2 return x + 13
4 a = 25 b = onemore(a + 2)
Namespaces
global I onemoreI a → 2I b → 5
local I x → 4
Execution
I lines 1 and 2:I function definitionI onemore is added to the global namespaceI no other statement are executed
I line 4: a added to the global namespace withvalue 2
I line 5:
I a + 2 is evaluated to 4I a local namespace is createdI formal parameters are bound to argumentsI line 2 is executed
I x + 1 is evaluated to 5I the return value of onemore is bound to 5
I the local namespace is discardedI onemore(a + 2) is evaluated to 5I b is bound 5 in the global namespace
36
![Page 45: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/45.jpg)
Example
Program1 def onemore(x):2 return x + 13
4 a = 25 b = onemore(a + 2)
Namespacesglobal I onemore
I a → 2I b → 5
local I x → 4
ExecutionI lines 1 and 2:
I function definitionI onemore is added to the global namespaceI no other statement are executed
I line 4: a added to the global namespace withvalue 2
I line 5:
I a + 2 is evaluated to 4I a local namespace is createdI formal parameters are bound to argumentsI line 2 is executed
I x + 1 is evaluated to 5I the return value of onemore is bound to 5
I the local namespace is discardedI onemore(a + 2) is evaluated to 5I b is bound 5 in the global namespace
36
![Page 46: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/46.jpg)
Example
Program1 def onemore(x):2 return x + 13
4 a = 25 b = onemore(a + 2)
Namespacesglobal I onemore
I a → 2
I b → 5
local I x → 4
ExecutionI lines 1 and 2:
I function definitionI onemore is added to the global namespaceI no other statement are executed
I line 4: a added to the global namespace withvalue 2
I line 5:
I a + 2 is evaluated to 4I a local namespace is createdI formal parameters are bound to argumentsI line 2 is executed
I x + 1 is evaluated to 5I the return value of onemore is bound to 5
I the local namespace is discardedI onemore(a + 2) is evaluated to 5I b is bound 5 in the global namespace
36
![Page 47: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/47.jpg)
Example
Program1 def onemore(x):2 return x + 13
4 a = 25 b = onemore(a + 2)
Namespacesglobal I onemore
I a → 2
I b → 5
local I x → 4
ExecutionI lines 1 and 2:
I function definitionI onemore is added to the global namespaceI no other statement are executed
I line 4: a added to the global namespace withvalue 2
I line 5:
I a + 2 is evaluated to 4I a local namespace is createdI formal parameters are bound to argumentsI line 2 is executed
I x + 1 is evaluated to 5I the return value of onemore is bound to 5
I the local namespace is discardedI onemore(a + 2) is evaluated to 5I b is bound 5 in the global namespace
36
![Page 48: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/48.jpg)
Example
Program1 def onemore(x):2 return x + 13
4 a = 25 b = onemore(a + 2)
Namespacesglobal I onemore
I a → 2
I b → 5
local I x → 4
ExecutionI lines 1 and 2:
I function definitionI onemore is added to the global namespaceI no other statement are executed
I line 4: a added to the global namespace withvalue 2
I line 5:I a + 2 is evaluated to 4
I a local namespace is createdI formal parameters are bound to argumentsI line 2 is executed
I x + 1 is evaluated to 5I the return value of onemore is bound to 5
I the local namespace is discardedI onemore(a + 2) is evaluated to 5I b is bound 5 in the global namespace
36
![Page 49: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/49.jpg)
Example
Program1 def onemore(x):2 return x + 13
4 a = 25 b = onemore(a + 2)
Namespacesglobal I onemore
I a → 2
I b → 5
local I x → 4
ExecutionI lines 1 and 2:
I function definitionI onemore is added to the global namespaceI no other statement are executed
I line 4: a added to the global namespace withvalue 2
I line 5:I a + 2 is evaluated to 4I a local namespace is created
I formal parameters are bound to argumentsI line 2 is executed
I x + 1 is evaluated to 5I the return value of onemore is bound to 5
I the local namespace is discardedI onemore(a + 2) is evaluated to 5I b is bound 5 in the global namespace
36
![Page 50: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/50.jpg)
Example
Program1 def onemore(x):2 return x + 13
4 a = 25 b = onemore(a + 2)
Namespacesglobal I onemore
I a → 2
I b → 5
local I x → 4
ExecutionI lines 1 and 2:
I function definitionI onemore is added to the global namespaceI no other statement are executed
I line 4: a added to the global namespace withvalue 2
I line 5:I a + 2 is evaluated to 4I a local namespace is createdI formal parameters are bound to arguments
I line 2 is executedI x + 1 is evaluated to 5I the return value of onemore is bound to 5
I the local namespace is discardedI onemore(a + 2) is evaluated to 5I b is bound 5 in the global namespace
36
![Page 51: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/51.jpg)
Example
Program1 def onemore(x):2 return x + 13
4 a = 25 b = onemore(a + 2)
Namespacesglobal I onemore
I a → 2
I b → 5
local I x → 4
ExecutionI lines 1 and 2:
I function definitionI onemore is added to the global namespaceI no other statement are executed
I line 4: a added to the global namespace withvalue 2
I line 5:I a + 2 is evaluated to 4I a local namespace is createdI formal parameters are bound to argumentsI line 2 is executed
I x + 1 is evaluated to 5I the return value of onemore is bound to 5
I the local namespace is discardedI onemore(a + 2) is evaluated to 5I b is bound 5 in the global namespace
36
![Page 52: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/52.jpg)
Example
Program1 def onemore(x):2 return x + 13
4 a = 25 b = onemore(a + 2)
Namespacesglobal I onemore
I a → 2
I b → 5
local I x → 4
ExecutionI lines 1 and 2:
I function definitionI onemore is added to the global namespaceI no other statement are executed
I line 4: a added to the global namespace withvalue 2
I line 5:I a + 2 is evaluated to 4I a local namespace is createdI formal parameters are bound to argumentsI line 2 is executed
I x + 1 is evaluated to 5I the return value of onemore is bound to 5
I the local namespace is discarded
I onemore(a + 2) is evaluated to 5I b is bound 5 in the global namespace
36
![Page 53: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/53.jpg)
Example
Program1 def onemore(x):2 return x + 13
4 a = 25 b = onemore(a + 2)
Namespacesglobal I onemore
I a → 2
I b → 5
local I x → 4
ExecutionI lines 1 and 2:
I function definitionI onemore is added to the global namespaceI no other statement are executed
I line 4: a added to the global namespace withvalue 2
I line 5:I a + 2 is evaluated to 4I a local namespace is createdI formal parameters are bound to argumentsI line 2 is executed
I x + 1 is evaluated to 5I the return value of onemore is bound to 5
I the local namespace is discardedI onemore(a + 2) is evaluated to 5
I b is bound 5 in the global namespace
36
![Page 54: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/54.jpg)
Example
Program1 def onemore(x):2 return x + 13
4 a = 25 b = onemore(a + 2)
Namespacesglobal I onemore
I a → 2I b → 5
local I x → 4
ExecutionI lines 1 and 2:
I function definitionI onemore is added to the global namespaceI no other statement are executed
I line 4: a added to the global namespace withvalue 2
I line 5:I a + 2 is evaluated to 4I a local namespace is createdI formal parameters are bound to argumentsI line 2 is executed
I x + 1 is evaluated to 5I the return value of onemore is bound to 5
I the local namespace is discardedI onemore(a + 2) is evaluated to 5I b is bound 5 in the global namespace
36
![Page 55: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/55.jpg)
Return
SemanticsI return both
I binds the value of the functionI interrupts its execution
I a function can contain multiples return statements (only one willbe executed)
I when a function contains no return statementI its value is NoneI its execution continues until the end of its body
37
![Page 56: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/56.jpg)
Examples
Multiple return1 def my_fun(x, y):2 if x > y:3 return x4 else:5 return y
I the function value is obviouslythe largest of its two arguments
I if the first argument is thelargest one, the first returnstatement is executed and thusonly lines 2 and 3 are executed
I in the other case, the secondreturn statement is executed
No return1 def foo(x):2 x = x + 13 print(x)
I lines 2 and 3 are alwaysexecuted
I the value of the function is NoneI do not confuse printing and
returning a value! The program1 def foo(x):2 x = x + 13 print(x)4
5 y = foo(2)6 print(y)
prints3None
38
![Page 57: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/57.jpg)
Namespaces
DefinitionA namespace binds names to objects
ExamplesI the built-in namespace (with type, len, etc.)I the global namespace of a programI the local namespace of a function (during its execution)
Important aspectsI namespaces are runtime dynamical entitiesI two different namespaces can contain the same name bound to
different objects
39
![Page 58: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/58.jpg)
Scopes
DefinitionA scope is a textual part of a program in which a namespace is directlyaccessible
ExamplesI a Python program is a scope (associated to the global namespace
of the program) which is enclosed in the scope of the built-innamespace
I a function definition defines a scope which is enclosed in theglobal scope
40
![Page 59: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/59.jpg)
Access rules
Directly accessibleI names in the namespace of the local scope are directly accessible
(those are local names)I names in namespaces associated to enclosing function scopes
are directly accessible (when a function is defined inside anotherfunction)
I global names are accessible (names in the global enclosingnamespace)
I built-in names are accessibleI names are searched for in order from the local scope to the
built-in one: the first match is used!
41
![Page 60: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/60.jpg)
Example
Non local accessThis program
1 x = 1 # global scope2
3 def f(y):4 # local scope of f5 return max(x, y)6
7 print(f(2))8 x = 39 print(f(2))
prints23
Do not do that!
Scopes
1. built-in2. global (the program)3. local to f
AccessesI max is accessible as a name of the built-in
namespaceI y is accessible in f as a name of the
namespace created when f is executedand attached to the scope of f
I x is accessible in f as a name of the globalnamescape attached to the global scopewhich encloses the scope of f
42
![Page 61: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/61.jpg)
Example
Non local accessThis program
1 x = 1 # global scope2
3 def f(y):4 # local scope of f5 return max(x, y)6
7 print(f(2))8 x = 39 print(f(2))
prints23
Do not do that!
Scopes
1. built-in2. global (the program)3. local to f
AccessesI max is accessible as a name of the built-in
namespaceI y is accessible in f as a name of the
namespace created when f is executedand attached to the scope of f
I x is accessible in f as a name of the globalnamescape attached to the global scopewhich encloses the scope of f
42
![Page 62: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/62.jpg)
Examples
Cannot access enclosedscopesIn this program
1 def f(z):2 return z + 13
4 print(f(2))5 print(z)
line 5 prints an error of the formNameError: name 'z' is not defined
z is not accessible in the globalscope.
PriorityThis program
1 def g(x):2 return x + 13
4 x = 25 print(g(3))6 print(x)
prints42
I x is both a local name(parameter) and a global one
I the name is searched first inthe local namespace andthen in enclosing ones
43
![Page 63: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/63.jpg)
Recursive functions
Calling oneselfI a function body may
contain calls to itselfI leverage dynamic
namespaces: each callhas its own namespace
Example1 def facto(n):2 if n <= 1:3 return 14 else:5 return n * facto(n-1)
Analyzing a call
facto(4)n → 4facto(3)
n → 3facto(2)
n → 2facto(1)
n → 1return 1
n * facto(1)→ 2return 2
n * facto(2)→ 6return 6
n * facto(3)→ 24return 24
44
![Page 64: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/64.jpg)
Recursive functions
Calling oneselfI a function body may
contain calls to itselfI leverage dynamic
namespaces: each callhas its own namespace
Example1 def facto(n):2 if n <= 1:3 return 14 else:5 return n * facto(n-1)
Analyzing a call
facto(4)n → 4facto(3)
n → 3facto(2)
n → 2facto(1)
n → 1return 1
n * facto(1)→ 2return 2
n * facto(2)→ 6return 6
n * facto(3)→ 24return 24
44
![Page 65: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/65.jpg)
Matching parameters and arguments
Positional matchingI standard caseI definition
def function_name(p_1,...,p_n)
I call function_name(a_1,...,a_n)I constraints and semantics
I exactly as many arguments as formalparameters
I p_k is bound to a_k
I the position of the argument decides itsformal parameters
ExampleThe programdef f(x, y):
return x - y
x = 2y = 3print(f(y, x))
prints1
45
![Page 66: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/66.jpg)
Keyword Arguments
Name matchingI definition
def function_name(p_1,...,p_n)
I callfunction_name(p_1 = a_1,...,p_n = a_n)
I constraints and semanticsI exactly as many arguments as formal
parametersI p_k is bound to the argument associated
to its name in the callI only the names are used, not the
positionsfunction_name(p_n = a_n,...,p_1 = a_1)
ExampleThe programdef f(x, y):
return x - y
print(f(y = 3, x = 2))
prints-1
46
![Page 67: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/67.jpg)
Mixing both types
RulesI a function call may mix
positional arguments andkeyword arguments
I positional arguments mustappear first
I when a keyword argument isused, all subsequentarguments must use thekeyword mode
ExamplesI with
def f(a, b, c):...
I incorrect callsI f(2,b=3,4)
I f(b=3,c=4,1)
I correct callsI f(2,b=3,c=4) (a is bound to
2)I f(2,c=5, b=2) (a is bound
to 2)
47
![Page 68: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/68.jpg)
Default arguments
Main use of keywordargumentsI to enable function calls with
missing argumentsI via default values for missing
arguments
Function definitionwith default valuesdef f_n(p_1=d_1,...,p_n=d_n):
statement_1...statement_n
Rules and semanticsI p_k=d_k specifies both a
formal parameter p_k and itsdefault value d_k
I defaults values are optional(may be given for a subset ofthe parameters only)
I when a parameter has nomatching argument in a call,it is bound to its default value
48
![Page 69: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/69.jpg)
Example
The program1 def foo(a, b = 2, c = 3):2 return (a + c) / b3
4 print(foo(2))5 print(foo(4, c = 2))6 print(foo(3, 5))7 print(foo(c = 4, a = 8))
prints2.53.01.26.0
Interpretationdefault values are underlined
line a b c4 2 2 35 4 2 26 3 5 37 8 2 4
49
![Page 70: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/70.jpg)
Outline
Introduction
Core concepts
Control structures
Functions
Exception handling
50
![Page 71: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/71.jpg)
Problems in programs
Errors and ExceptionsI Syntax errors: the program is not an acceptable python program
and cannot be executedI Exceptions: errors detected during execution
Syntax errorRunningy = 5x = 3 +/ y
printsTraceback (most recent call last):File "error.py", line 2x = 3 +/ y
^SyntaxError: invalid syntax
ExceptionRunningy = 5x = 3 +/ y
printsTraceback (most recent call last):
File "zero.py", line 3, in <module>z = x/y
ZeroDivisionError: division by zero
51
![Page 72: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/72.jpg)
Exceptions
Handling exceptionsI normal behavior: an
exception stops theprogram
I desirable behavior: fix theproblem and continue
I mechanismI try somethingI if it does not work and
induces an exception dosomething else
Exampletry:
answer = input('Enter an integer = ')x = int(answer)
except ValueError:print(answer,' is not an integer')x = 0
print(x)
Normal outputEnter an integer = 55
Exceptional outputEnter an integer = foofoo is not an integer0
52
![Page 73: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/73.jpg)
Handling Exceptions
try statementI try is a compound statement which starts with a try clauseI followed by
I a single finally clauseI or at least one except clause with possibly an else clause and a
finally clause
Short versiontry:
body_tfinally:body_f
Long versiontry:body_t
except type_1:body_e_1
except type_2:body_e_2...
else:body_el
finally:body_f
53
![Page 74: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/74.jpg)
Handling Exceptions
try:body_t
except type_1:body_e_1
except type_2:body_e_2...
else:body_el
finally:body_f
rest of the program
SemanticsI Python tries to execute body_t
I if this does not produce any exception, theexecution continues through the else andthen through the rest of the program
I if an exception of type T is raisedI Python search for a matching type in the
except headers in order (an empty typein a except matches any exceptiontype)
I if a matching type is found, thecorresponding body is executed andthen the rest of the program is executed
I the finally clause is always executed, evenif an exception occurs in the except or elseclause
54
![Page 75: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/75.jpg)
Example
try:v = input('x = ')x = int(v)print(1/x)
except ValueError:print(v,'is not an integer')
except ZeroDivisionError:print('no inverse for',x)
else:print('ok')
finally:print('this is the end')
print('rest of the program')
InteractionsI if the user inputs 2.5, she gets
2.5 is not an integerthis is the endrest of the program
I if the user inputs 4, she gets0.25okthis is the endrest of the program
I if the user inputs 0, she getsno inverse for 0this is the endrest of the program
55
![Page 76: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/76.jpg)
Missing an exception
An exception is unhandledI when it occurs in the try
clause and is not matchedI when it occurs in a except
clauseI when it occurs in a else
clauseI when it occurs in a finally
clause
and it is passed to the enclosingenvironment
ExampleThe following programtry:
try:x = int('2.5')
except ValueError:print('got it')print(1/0)
except ZeroDivisionError:print('missed')
finally:print('exiting')
except ZeroDivisionError:print('caught')
printsgot itexitingcaught
56
![Page 77: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/77.jpg)
Next Steps
1. Data structures in Python2. Data manipulation in Python
57
![Page 78: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/78.jpg)
Licence
This work is licensed under a Creative CommonsAttribution-ShareAlike 4.0 International License.
http://creativecommons.org/licenses/by-sa/4.0/
58
![Page 79: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/79.jpg)
Version
Last git commit: 2019-12-04By: Fabrice Rossi ([email protected])Git hash: 53ac9a0c6fbbf86d54b074dfebaadba23f75f6c2
59
![Page 80: An introduction to Pythonapiacoa.org/publications/teaching/python/intro-python.pdf · Python and data science I Python is one of the two de facto standard languages for data science](https://reader034.vdocuments.mx/reader034/viewer/2022042218/5ec45ba4729db15f1a3085d2/html5/thumbnails/80.jpg)
Changelog
I November 2019: added exception handlingI October 2019: initial version
60