+ All Categories
Home > Documents > An introduction to Python - Fabrice Rossi...Python and data science I Python is one of the two de...

An introduction to Python - Fabrice Rossi...Python and data science I Python is one of the two de...

Date post: 16-Oct-2020
Category:
Upload: others
View: 11 times
Download: 0 times
Share this document with a friend
80
An introduction to Python Fabrice Rossi CEREMADE Université Paris Dauphine 2020
Transcript
Page 1: An introduction to Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

An introduction to Python

Fabrice Rossi

CEREMADEUniversité Paris Dauphine

2020

Page 2: An introduction to Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Python

https://www.python.org/

é Python is a high level programminglanguage

é Python’s reference implementationis a multiplatform free software

é Python can be extended bythousands of libraries

é 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 Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Python and data science

é Python is one of the two defacto standard languages fordata science (with R)

é Python has a large collectionof high performance datascience oriented libraries

é Python is generallyconsidered to be easy toread

3

Page 4: An introduction to Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Python

Prosé open source implementationé full-fledged programming

languageé strong support from a large

communityé broad coverage of data

science, statistics, etc.é high performance librariesé high quality graphicsé curated distribution

Consé limited point-and-click

supporté rather steep learning curve

compared to an integratedsoftware

é naive code has lowperformances

é “old” language (1990) with alack of modern constructs

4

Page 5: An introduction to Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Recommended installs

é Anaconda (with Python 3.x)é https://www.anaconda.com/distribution/é a python distribution: python + libraries + toolsé data science orientedé anaconda navigator for managing the distribution

é recommended tools (in Anaconda)é VS code or Spyder for Python programmingé JupyterLab for literate programming

é other IDE include PyCharmé do not use Python 2.7

5

Page 6: An introduction to Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Outline

Introduction

Core concepts

Control structures

Functions

Exception handling

6

Page 7: An introduction to Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Outline

Introduction

Core conceptsProgramming LanguageConsole interactionBasic data modelVariablesStringsFunctionsModules

Control structures

Functions

Exception handling

7

Page 8: An introduction to Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Programming Language

Definitioné a formal language with a strict mathematical definitioné defines syntactically correct programsé associated to a semantics

é (formal) model of the computeré effects of a program on the model

In other words...é a programming language can be used to write programs ' textsé a programming language has a strict syntax

é lexical aspects ' word spellingé grammatical aspects ' sentence level

é 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 Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Programming Language

Definitioné a formal language with a strict mathematical definitioné defines syntactically correct programsé associated to a semantics

é (formal) model of the computeré effects of a program on the model

In other words...é a programming language can be used to write programs ' textsé a programming language has a strict syntax

é lexical aspects ' word spellingé grammatical aspects ' sentence level

é 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 Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

A computer

Turing Machineé standard mathematical modelé too low level to a daily use

Other modelsé data oriented modelsé a model of the dataé together with a model of the execution of a program

é effects of instructions/statements on the data ' sentence levelé global flow and organization on a program ' text level

é include input/output aspects

9

Page 11: An introduction to Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Interactive mode

Standard program executioné a program is written in a file (or a set of files)é in some languages the file can be translated to a more efficient

languageé the file (or its translation) is executed on a computer

Console/Shellé some languages have an associated “console” or “shell” (e.g.

Python and R)é one can type interactively program sentences and get associated

resultsé simplifies learning and testing

10

Page 12: An introduction to Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Python Shell

é Python provides a shell forinteractive use

é in general integrated in aspecific window of aprogramming environment

é can be launched from thecommand line (python)

é 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 Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Python Shell

é Python provides a shell forinteractive use

é in general integrated in aspecific window of aprogramming environment

é can be launched from thecommand line (python)

é 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 Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Python Shell

é Python provides a shell forinteractive use

é in general integrated in aspecific window of aprogramming environment

é can be launched from thecommand line (python)

é 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 Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Python Shell

é Python provides a shell forinteractive use

é in general integrated in aspecific window of aprogramming environment

é can be launched from thecommand line (python)

é 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 Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Python Shell

é Python provides a shell forinteractive use

é in general integrated in aspecific window of aprogramming environment

é can be launched from thecommand line (python)

é 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 Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Python Shell

é Python provides a shell forinteractive use

é in general integrated in aspecific window of aprogramming environment

é can be launched from thecommand line (python)

é 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 Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Python Shell

é Python provides a shell forinteractive use

é in general integrated in aspecific window of aprogramming environment

é can be launched from thecommand line (python)

é 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 Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

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 Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Basic data model

Numerical valuesé integersé real numbers

é decimal pointé classical scientific notation

e.g. 1.5e-3é complex numbers

é automatically used in somesituations

é real + img j

Arithmetic operationsé standard operationsé integer oriented

Logical expressionsé boolean (a.k.a. truth value)é True and False valuesé automatic integer conversion

to 1 and 0, respectively (andvice versa)

é logical operators and or noté numerical comparisons

é == and !=é <= and < (and reversed

ones)

13

Page 21: An introduction to Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Basic data model

Numerical valuesé integersé real numbers

é decimal pointé classical scientific notation

e.g. 1.5e-3é complex numbers

é automatically used in somesituations

é real + img j

Arithmetic operationsé standard operationsé integer oriented

Logical expressionsé boolean (a.k.a. truth value)é True and False valuesé automatic integer conversion

to 1 and 0, respectively (andvice versa)

é logical operators and or noté numerical comparisons

é == and !=é <= and < (and reversed

ones)

13

Page 22: An introduction to Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Basic data model

Syntaxé literal values (spelling)

é e.g. numbers and truthvalues

é Python specifies how towrite them

é e.g. 1,5 is not a realnumber!

é operations (grammar)é writing rules are similar to

mathematical onesé with exceptions such as

é == for equalityé ** for exponentiationé etc.

Semanticsé interpretation off the symbols

and of the expressions suchas:é calculation orderingé == tests for equalityé ** can produce complex

numbersé _ is the last value

computedé error cases

>>> 0/0Traceback (most recent call last):File "<stdin>", line 1, in <module>

ZeroDivisionError: division by zero

14

Page 23: An introduction to Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Objects and variables

Objectsé Python manipulates objectsé each object has a type

é specifies the possiblevalues

é specifies the possibleoperations

é examplesé 2 is an inté 2.5 is a floaté True is a boolé 1+2j is a complex

Variablesé objects can be namedé a variable is a name for an

objecté setting/binding a name:

variable = object

é when a name appears in anexpression it is replaced bythe object

é example>>> x = 2>>> 2 * x4

15

Page 24: An introduction to Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

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 pointsé (obvious) sequential modelé no default bindingé case dependanté aliases: several names for

a given objecté variable = variable

does not bind the namestogether

é unconstrained rebinding

16

Page 25: An introduction to Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

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'

Stringsé type str (string)é literal ' ' or " "

é multiline with ''' '''

é concatenationé types are not compatible in

general!

17

Page 26: An introduction to Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

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

Indexingé some Python objects can be

indexedé [index]

é numbering always start at 0é negative indexing enables

reverse orderingé slices a:b

é from a to b-1é missing a: 0é missing b: last index + 1

é negative slicing: same logic

18

Page 27: An introduction to Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Functions

Additional actionsé objects can be manipulated with

more than operatorsé functions provide such additional

actionsé a function

é has a nameé needs 0 or more argument(s)é possibly returns an object

é using a functioné function callé function(argument_1, argument_2)

é 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 Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Functions

Functions are objectsé type function for general

functionsé specific type for built in

functionsé all standard properties apply

é new namesé 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 Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Methods

Functions for objectsé methods are specific

functions associated to someobject types

é special calling syntaxobject.function()

é 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 Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Modules

Extending Pythoné modules provide new

functions and typesé a module must be imported

to have access to its contenté default module sys

Importing modulesé import module gives

access to the names in themodule via module.name

é 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 Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Outline

Introduction

Core concepts

Control structuresNon interactive PythonConditional executionLoops

Functions

Exception handling

23

Page 32: An introduction to Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Input and output

Console limitationé has to be used interactivelyé commands are not savedé reproducibility is not

guaranteed

Scriptsé normal simple python

programs are scripté a script: a text file (generally

ending with .py)é a script is executed by the

python interpreter

Outputsé to output something, use the

print functioné for instance

print(2, 'toto')x = 3print(x, 2 * x, 2 ** x)

will print2 toto3 6 8

Inputsé to input something, use the

input functioné returns always a string str

é convert if needed

24

Page 33: An introduction to Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Conditional execution

Execute if...é programs can include parts

that are executed only ifsome condition is fulfilled

é the condition is written as aBoolean expression

General formif expression:

statement_1statement_2...statement_n

rest of the program

if in Pythoné if is a compound statementé it consists a clause

comprisingé a header

if expression:é a suite whose execution is

controlled by the headeré 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 Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

More conditional execution

Other clauses in ifé a if statement can contain

é one or more elif clausesé one else clause

é 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é the expression of the if header is

evaluatedé if the value is True then the body is

executed and the execution resumes forthe rest of the program

é if the value is False the body is ignoredthe execution resumes on the secondclause

é for each elif header, the execution

follows the same pattern:é if the corresponding expression

is True the body of the clause isexecuted, followed by the rest ofthe program

é if not the execution resumes onthe next clause

é if all expressions evaluate to False thebody of the else clause is executed

26

Page 35: An introduction to Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Repeating instructions

Multiple executionsé programs can include parts

that are executed severaltimes

é repetitions can be conditionalor numbered

Conditional loopwhile expression:

statement_1...statement_n

rest of the program

while in Pythoné compound statement (single

clause)é while expression: is

the header of the clause

Semanticsé the expression of the header is

evaluatedé if the value is True

é the body is executedé the execution resumes on clause

itself!

é if the value is False theexecution resumes for the rest ofthe program

27

Page 36: An introduction to Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Iteration

Iterable objectsé objects which can be

decomposed into severalother objects

é the content of an iterableobject is arranged in acertain order

é iterating over the objectmeans accessing in order toits elements

Stringsé string "content": charactersé iterating a string: in character

order!é 'foobar' gives 'f', 'o',

'o', 'b', 'a' and 'r'

28

Page 37: An introduction to Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Iterating iterables

For loopsé specific loop for iterablesé 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 Pythoné compound statement (single

clause)é for variable in expression: is

the header of the clauseé the expression of the header

must evaluate to an iterableobject

29

Page 38: An introduction to Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

For semantics

Semanticsé the expression is evaluated

to get an iterable objecté for each object in the iterable

é the variable is bound tothe object

é the body of the clause isexecuted

é then the execution of the restof the program resumes

é 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 Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Ranges

Repeating n times someoperationsé very common caseé easy to do with a while but

not immediately obviousk = 0while k < n:

somethingto repeatn timesk = k + 1

more statements

range objectsé integer range iterableé range(n): integers from 0

to n-1 (n values)é simpler solution

for k in range(n):somethingto repeatn times

more statements

é clearer for pythonprogrammer

31

Page 40: An introduction to Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Ranges

More rangesé range operates in a similar way to slicesé range(end): integers from 0 to end-1

é range(begin, end): integers from begin to end-1é range(begin, end, step): integers from begin to end-1 by

increments of stepé range(1, 4, 2) : 1 and 3é range(1, 5, 2) : 1 and 3

é works with negative incrementsé range(5, 2) : emptyé range(5, 2, -1): 5, 4 and 3

32

Page 41: An introduction to Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Outline

Introduction

Core concepts

Control structures

FunctionsDefining functionsNamespacesRecursive functionsParameters and arguments

Exception handling

33

Page 42: An introduction to Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Defining functions

Benefits of user definedfunctionsé provide program organizationé reduce code repetitioné enable using generic

functionalities

Exampledef onemore(x):

return x + 1

General formdef function_name(p_1,...,p_n):

statement_1...statement_n

returné the return statement

defines the value of thefunction

é it terminates the functionexecution

Vocabularyé the code above is a function

definitioné p_1,...,p_n are the

formal parameters of thefunction (possible none)

é the statements form the bodyof the function

34

Page 43: An introduction to Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Calling a function

Definition versus callé the function definition only

makes it available in the restof the program

é a (standard) function call isneeded to use itfunction_name(a_1,...,a_n)

é 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 Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Example

Program1 def onemore(x):2 return x + 134 a = 25 b = onemore(a + 2)

Namespaces

global é onemoreé a ! 2é b ! 5

local é x ! 4

Execution

é lines 1 and 2:é function definitioné onemore is added to the global namespaceé no other statement are executed

é line 4: a added to the global namespace withvalue 2

é line 5:

é a + 2 is evaluated to 4é a local namespace is createdé formal parameters are bound to argumentsé line 2 is executed

é x + 1 is evaluated to 5é the return value of onemore is bound to 5

é the local namespace is discardedé onemore(a + 2) is evaluated to 5é b is bound 5 in the global namespace

36

Page 45: An introduction to Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Example

Program1 def onemore(x):2 return x + 134 a = 25 b = onemore(a + 2)

Namespacesglobal é onemore

é a ! 2é b ! 5

local é x ! 4

Executioné lines 1 and 2:

é function definitioné onemore is added to the global namespaceé no other statement are executed

é line 4: a added to the global namespace withvalue 2

é line 5:

é a + 2 is evaluated to 4é a local namespace is createdé formal parameters are bound to argumentsé line 2 is executed

é x + 1 is evaluated to 5é the return value of onemore is bound to 5

é the local namespace is discardedé onemore(a + 2) is evaluated to 5é b is bound 5 in the global namespace

36

Page 46: An introduction to Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Example

Program1 def onemore(x):2 return x + 134 a = 25 b = onemore(a + 2)

Namespacesglobal é onemore

é a ! 2

é b ! 5

local é x ! 4

Executioné lines 1 and 2:

é function definitioné onemore is added to the global namespaceé no other statement are executed

é line 4: a added to the global namespace withvalue 2

é line 5:

é a + 2 is evaluated to 4é a local namespace is createdé formal parameters are bound to argumentsé line 2 is executed

é x + 1 is evaluated to 5é the return value of onemore is bound to 5

é the local namespace is discardedé onemore(a + 2) is evaluated to 5é b is bound 5 in the global namespace

36

Page 47: An introduction to Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Example

Program1 def onemore(x):2 return x + 134 a = 25 b = onemore(a + 2)

Namespacesglobal é onemore

é a ! 2

é b ! 5

local é x ! 4

Executioné lines 1 and 2:

é function definitioné onemore is added to the global namespaceé no other statement are executed

é line 4: a added to the global namespace withvalue 2

é line 5:

é a + 2 is evaluated to 4é a local namespace is createdé formal parameters are bound to argumentsé line 2 is executed

é x + 1 is evaluated to 5é the return value of onemore is bound to 5

é the local namespace is discardedé onemore(a + 2) is evaluated to 5é b is bound 5 in the global namespace

36

Page 48: An introduction to Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Example

Program1 def onemore(x):2 return x + 134 a = 25 b = onemore(a + 2)

Namespacesglobal é onemore

é a ! 2

é b ! 5

local é x ! 4

Executioné lines 1 and 2:

é function definitioné onemore is added to the global namespaceé no other statement are executed

é line 4: a added to the global namespace withvalue 2

é line 5:é a + 2 is evaluated to 4

é a local namespace is createdé formal parameters are bound to argumentsé line 2 is executed

é x + 1 is evaluated to 5é the return value of onemore is bound to 5

é the local namespace is discardedé onemore(a + 2) is evaluated to 5é b is bound 5 in the global namespace

36

Page 49: An introduction to Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Example

Program1 def onemore(x):2 return x + 134 a = 25 b = onemore(a + 2)

Namespacesglobal é onemore

é a ! 2

é b ! 5

local é x ! 4

Executioné lines 1 and 2:

é function definitioné onemore is added to the global namespaceé no other statement are executed

é line 4: a added to the global namespace withvalue 2

é line 5:é a + 2 is evaluated to 4é a local namespace is created

é formal parameters are bound to argumentsé line 2 is executed

é x + 1 is evaluated to 5é the return value of onemore is bound to 5

é the local namespace is discardedé onemore(a + 2) is evaluated to 5é b is bound 5 in the global namespace

36

Page 50: An introduction to Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Example

Program1 def onemore(x):2 return x + 134 a = 25 b = onemore(a + 2)

Namespacesglobal é onemore

é a ! 2

é b ! 5

local é x ! 4

Executioné lines 1 and 2:

é function definitioné onemore is added to the global namespaceé no other statement are executed

é line 4: a added to the global namespace withvalue 2

é line 5:é a + 2 is evaluated to 4é a local namespace is createdé formal parameters are bound to arguments

é line 2 is executedé x + 1 is evaluated to 5é the return value of onemore is bound to 5

é the local namespace is discardedé onemore(a + 2) is evaluated to 5é b is bound 5 in the global namespace

36

Page 51: An introduction to Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Example

Program1 def onemore(x):2 return x + 134 a = 25 b = onemore(a + 2)

Namespacesglobal é onemore

é a ! 2

é b ! 5

local é x ! 4

Executioné lines 1 and 2:

é function definitioné onemore is added to the global namespaceé no other statement are executed

é line 4: a added to the global namespace withvalue 2

é line 5:é a + 2 is evaluated to 4é a local namespace is createdé formal parameters are bound to argumentsé line 2 is executed

é x + 1 is evaluated to 5é the return value of onemore is bound to 5

é the local namespace is discardedé onemore(a + 2) is evaluated to 5é b is bound 5 in the global namespace

36

Page 52: An introduction to Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Example

Program1 def onemore(x):2 return x + 134 a = 25 b = onemore(a + 2)

Namespacesglobal é onemore

é a ! 2

é b ! 5

local é x ! 4

Executioné lines 1 and 2:

é function definitioné onemore is added to the global namespaceé no other statement are executed

é line 4: a added to the global namespace withvalue 2

é line 5:é a + 2 is evaluated to 4é a local namespace is createdé formal parameters are bound to argumentsé line 2 is executed

é x + 1 is evaluated to 5é the return value of onemore is bound to 5

é the local namespace is discarded

é onemore(a + 2) is evaluated to 5é b is bound 5 in the global namespace

36

Page 53: An introduction to Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Example

Program1 def onemore(x):2 return x + 134 a = 25 b = onemore(a + 2)

Namespacesglobal é onemore

é a ! 2

é b ! 5

local é x ! 4

Executioné lines 1 and 2:

é function definitioné onemore is added to the global namespaceé no other statement are executed

é line 4: a added to the global namespace withvalue 2

é line 5:é a + 2 is evaluated to 4é a local namespace is createdé formal parameters are bound to argumentsé line 2 is executed

é x + 1 is evaluated to 5é the return value of onemore is bound to 5

é the local namespace is discardedé onemore(a + 2) is evaluated to 5

é b is bound 5 in the global namespace

36

Page 54: An introduction to Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Example

Program1 def onemore(x):2 return x + 134 a = 25 b = onemore(a + 2)

Namespacesglobal é onemore

é a ! 2é b ! 5

local é x ! 4

Executioné lines 1 and 2:

é function definitioné onemore is added to the global namespaceé no other statement are executed

é line 4: a added to the global namespace withvalue 2

é line 5:é a + 2 is evaluated to 4é a local namespace is createdé formal parameters are bound to argumentsé line 2 is executed

é x + 1 is evaluated to 5é the return value of onemore is bound to 5

é the local namespace is discardedé onemore(a + 2) is evaluated to 5é b is bound 5 in the global namespace

36

Page 55: An introduction to Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Return

Semanticsé return both

é binds the value of the functioné interrupts its execution

é a function can contain multiples return statements (only one willbe executed)

é when a function contains no return statementé its value is Noneé its execution continues until the end of its body

37

Page 56: An introduction to Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Examples

Multiple return1 def my_fun(x, y):2 if x > y:3 return x4 else:5 return y

é the function value is obviouslythe largest of its two arguments

é if the first argument is thelargest one, the first returnstatement is executed and thusonly lines 2 and 3 are executed

é in the other case, the secondreturn statement is executed

No return1 def foo(x):2 x = x + 13 print(x)

é lines 2 and 3 are alwaysexecuted

é the value of the function is Noneé do not confuse printing and

returning a value! The program1 def foo(x):2 x = x + 13 print(x)45 y = foo(2)6 print(y)

prints3None

38

Page 57: An introduction to Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Namespaces

DefinitionA namespace binds names to objects

Examplesé the built-in namespace (with type, len, etc.)é the global namespace of a programé the local namespace of a function (during its execution)

Important aspectsé namespaces are runtime dynamical entitiesé two different namespaces can contain the same name bound to

different objects

39

Page 58: An introduction to Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Scopes

DefinitionA scope is a textual part of a program in which a namespace is directlyaccessible

Examplesé a Python program is a scope (associated to the global namespace

of the program) which is enclosed in the scope of the built-innamespace

é a function definition defines a scope which is enclosed in theglobal scope

40

Page 59: An introduction to Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Access rules

Directly accessibleé names in the namespace of the local scope are directly accessible

(those are local names)é names in namespaces associated to enclosing function scopes

are directly accessible (when a function is defined inside anotherfunction)

é global names are accessible (names in the global enclosingnamespace)

é built-in names are accessibleé 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 Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Example

Non local accessThis program

1 x = 1 # global scope23 def f(y):4 # local scope of f5 return max(x, y)67 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

Accessesé max is accessible as a name of the built-in

namespaceé y is accessible in f as a name of the

namespace created when f is executedand attached to the scope of f

é 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 Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Example

Non local accessThis program

1 x = 1 # global scope23 def f(y):4 # local scope of f5 return max(x, y)67 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

Accessesé max is accessible as a name of the built-in

namespaceé y is accessible in f as a name of the

namespace created when f is executedand attached to the scope of f

é 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 Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Examples

Cannot access enclosedscopesIn this program

1 def f(z):2 return z + 134 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 + 134 x = 25 print(g(3))6 print(x)

prints42

é x is both a local name(parameter) and a global one

é the name is searched first inthe local namespace andthen in enclosing ones

43

Page 63: An introduction to Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Recursive functions

Calling oneselfé a function body may

contain calls to itselfé 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 Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Recursive functions

Calling oneselfé a function body may

contain calls to itselfé 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 Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Matching parameters and arguments

Positional matchingé standard caseé definition

def function_name(p_1,...,p_n)

é call function_name(a_1,...,a_n)é constraints and semantics

é exactly as many arguments as formalparameters

é p_k is bound to a_k

é 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 Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Keyword Arguments

Name matchingé definition

def function_name(p_1,...,p_n)

é callfunction_name(p_1 = a_1,...,p_n = a_n)

é constraints and semanticsé exactly as many arguments as formal

parametersé p_k is bound to the argument associated

to its name in the callé 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 Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Mixing both types

Rulesé a function call may mix

positional arguments andkeyword arguments

é positional arguments mustappear first

é when a keyword argument isused, all subsequentarguments must use thekeyword mode

Examplesé with

def f(a, b, c):...

é incorrect callsé f(2,b=3,4)

é f(b=3,c=4,1)

é correct callsé f(2,b=3,c=4) (a is bound to

2)é f(2,c=5, b=2) (a is bound

to 2)

47

Page 68: An introduction to Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Default arguments

Main use of keywordargumentsé to enable function calls with

missing argumentsé 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 semanticsé p_k=d_k specifies both a

formal parameter p_k and itsdefault value d_k

é defaults values are optional(may be given for a subset ofthe parameters only)

é when a parameter has nomatching argument in a call,it is bound to its default value

48

Page 69: An introduction to Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Example

The program1 def foo(a, b = 2, c = 3):2 return (a + c) / b34 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 Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Outline

Introduction

Core concepts

Control structures

Functions

Exception handling

50

Page 71: An introduction to Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Problems in programs

Errors and Exceptionsé Syntax errors: the program is not an acceptable python program

and cannot be executedé 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 Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Exceptions

Handling exceptionsé normal behavior: an

exception stops theprogram

é desirable behavior: fix theproblem and continue

é mechanismé try somethingé 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 Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Handling Exceptions

try statementé try is a compound statement which starts with a try clauseé followed by

é a single finally clauseé 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 Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

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

Semanticsé Python tries to execute body_t

é if this does not produce any exception, theexecution continues through the else andthen through the rest of the program

é if an exception of type T is raisedé Python search for a matching type in the

except headers in order (an empty typein a except matches any exceptiontype)

é if a matching type is found, thecorresponding body is executed andthen the rest of the program is executed

é the finally clause is always executed, evenif an exception occurs in the except or elseclause

54

Page 75: An introduction to Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

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')

Interactionsé if the user inputs 2.5, she gets

2.5 is not an integerthis is the endrest of the program

é if the user inputs 4, she gets0.25okthis is the endrest of the program

é if the user inputs 0, she getsno inverse for 0this is the endrest of the program

55

Page 76: An introduction to Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Missing an exception

An exception is unhandledé when it occurs in the try

clause and is not matchedé when it occurs in a except

clauseé when it occurs in a else

clauseé 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 Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Next Steps

1. Data structures in Python2. Data manipulation in Python

57

Page 78: An introduction to Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

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 Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Version

Last git commit: 2019-12-09By: Fabrice Rossi ([email protected])Git hash: ba0fa5483950d448f7a9210e4ac63ceabff8fb3f

59

Page 80: An introduction to Python - Fabrice Rossi...Python and data science I Python is one of the two de facto standard languages for data science (with R) I Python has a large collection

Changelog

é November 2019: added exception handlingé October 2019: initial version

60


Recommended