Cours
Cours
Prof. M. RIDOUANI
Overview of course:
• Topics include:
• Part 1: Python for Data Analysis (3 weeks)
– Chapter 1: Python Programming (1 week)
– Chapter 2: Data visualisation (1 week)
– Chapter 3: Data preparation (1 week)
4
Pr. Ridouani -- Chapter 1: Python Programming
Programming basics
The Python Interpreter:
•Python is an interpreted language
•The interpreter provides an interactive environment to play with the language
•Results of expressions are printed on the screen.
5
Pr. Ridouani -- Chapter 1: Python Programming
Programming basics
Introduction to Jupyter Notebook:
• Jupyter Notebooks: interactive coding and visualisation of output,
• Jupyter is an anagram of Julia, Python, and R,
• Supports multiple content types: code, text, images,video, etc..
6
Pr. Ridouani -- Chapter 1: Python Programming
Programming basics
jupyter shortcuts :
• Le mode Commande (Command Mode):
A : Insérer une nouvelle cellule au-dessus de la cellule actuelle.
B : Insérer une nouvelle cellule en dessous de la cellule actuelle.
D, D (appuyez deux fois sur D) : Supprimer la cellule actuelle.
Y : Changer le type de cellule en « Code ».
M : Changer le type de cellule en « Markdown ».
Flèche haut/bas : Naviguer entre les cellules.
Maj + Flèche haut/bas : Sélectionner plusieurs cellules
Maj + Entrée : Exécuter la cellule actuelle et passer à la suivante.
Entrée : Changer la cellule en mode édition
O : Changer la disposition des cellules en mode « Collapse/Expand »
L : Activer/désactiver les numéros de ligne
Maj + P : Ouvrir le sélecteur de commandes
Maj + A : Réorganiser les cellules en haut de la page
Maj + B : Réorganiser les cellules en bas de la page
Ctrl + Maj + Entrée : Exécuter toutes les cellules
7
Pr. Ridouani -- Chapter 1: Python Programming
Programming basics
jupyter shortcuts :
• mode Edition (Edit Mode) :
Ctrl + Entrée : Exécuter la cellule actuelle sans passer à la suivante.
Maj + Entrée : Exécuter la cellule actuelle et passer à la suivante.
Ctrl + / : Commenter ou décommenter les lignes sélectionnées.
Tab : Indentation automatique du code ou complétion automatique.
Ctrl + Maj + – : Diviser la cellule en deux à l’endroit du curseur.
Ctrl + A : Sélectionner tout le contenu de la cellule
Ctrl + Z : Annuler la dernière action
Ctrl + Maj + Z ou Ctrl + Y : Rétablir l’action précédemment annulée
Ctrl + D : Supprimer la ligne actuelle
Ctrl + Maj + – : Supprimer tout le contenu de la cellule
Ctrl + Backspace : Supprimer le contenu à gauche du curseur
Ctrl + Suppr : Supprimer le contenu à droite du curseur
Alt (maintenez la touche enfoncée tout en effectuant la sélection) :
Activer/désactiver le mode de sélection de colonne
8
Pr. Ridouani -- Chapter 1: Python Programming
Data Type I: simple variable
variable: A named piece of memory that can store a value
10
Pr. Ridouani -- Chapter 1: Python Programming
Python Comments
There are two types of comments in Python: single-line comments and
multiline comments.
The # symbol is used for single-line comments.
Multiline comments can be given inside triple quotes.
11
Pr. Ridouani -- Chapter 1: Python Programming
Basic Operators in Python
Arithmetic Operators:
12
Pr. Ridouani -- Chapter 1: Python Programming
Basic Operators in Python
Relational Operators:
13
Pr. Ridouani -- Chapter 1: Python Programming
Basic Operators in Python
Assign Operators:
14
Pr. Ridouani -- Chapter 1: Python Programming
Basic Operators in Python
Logical Operators:
15
Pr. Ridouani -- Chapter 1: Python Programming
Strings
Data type: str
letters, special characters, spaces, digits
enclose in quotation marks or single quotes: s = ’L-CISI’ , s = “L-CISI“
Multiline strings: s = ""“Licence
CISI""“
concatenate strings: l=“Licence” , s=“CISI”, F=l + “ ” + s
do some operations on a string: x= (l + “ ” + s + ”\n”)*3
16
Pr. Ridouani -- Chapter 1: Python Programming
Strings: Input/Output (f-String)
input("")
•prints whatever is in the quotes
•user types in something and hits enter
•binds that value to a variable
text = input("Type anything... ")
print(5*text)
f-String
•Provides a way to embed expressions
inside string literals, using a minimal
syntax
•Is a literal string, prefixed with ’f’, which
contains expressions inside braces
•Expressions are evaluated at runtime
and replaced with their values.
17
Pr. Ridouani -- Chapter 1: Python Programming
Ex
Prédire le résultat : opérations
Essayez de prédire le résultat de chacune des instructions suivantes, puis vérifiez-le
dans l’interpréteur Python :
• (1+2)**3
• "Da" * 4
• "Da" + 3
• ("Pa"+"La") * 2
• ("Da"*4) / 2
•5/2
• 5 // 2
•5%2
Prédire le résultat : opérations et conversions de types
Essayez de prédire le résultat de chacune des instructions suivantes, puis vérifiez-le
dans l’interpréteur Python :
• str(4) * int("3")
• int("3") + float("3.2")
• str(3) * float("3.2")
• str(3/4) * 2
18
Pr. Ridouani -- Chapter 1: Python Programming
Strings in Action
iterate over strings in loops using for statements and test membership for both
characters and substrings with the in expression operator, which is essentially a
search
19
Pr. Ridouani -- Chapter 1: Python Programming
Strings in Action
Indexing and Slicing
Because strings are defined as ordered collections of characters, we can access
their components by position
20
Pr. Ridouani -- Chapter 1: Python Programming
Strings in Action
Indexing and Slicing
Because strings are defined as ordered collections of characters, we can access
their components by position
21
Pr. Ridouani -- Chapter 1: Python Programming
Control statements: Conditional execution
The If Statement:
In order to write useful programs, we
almost always need the ability to check
conditions and change the behavior of
the program accordingly. Conditional
statements give us this ability. The
simplest form is the if statement:
22
Pr. Ridouani -- Chapter 1: Python Programming
Control statements: Conditional execution
Variations on if: If-Then-Else Logic / If-Then-ElseIf Logic
23
Pr. Ridouani -- Chapter 1: Python Programming
Control statements: while loops
•<condition> evaluates to a Boolean while <condition>:
•if<condition> is True, do all the <expression1>
steps inside the while code block <expression2>
•check <condition> again ...
•repeat until <condition> is False
while <condition>:
if <condition 1>:
<expression1>
continue
if <condition 2>:
<expression2>
break
else :
<expression3>
24
Pr. Ridouani -- Chapter 1: Python Programming
Control statements: for loops
range(start,stop,step)
default values are start = 0 and step = 1and optional
loop until value is stop -1
For loops
•each time through the loop, <variable> takes a value
•first time, <variable> starts at the smallest value
•next time, <variable> gets the prevvalue + 1
•Etc
26
Pr. Ridouani -- Chapter 1: Python Programming
Data Type II: list, tuple,dictionary,set
27
Pr. Ridouani -- Chapter 1: Python Programming
lists and tuples
Like a string, a list or a tuple is a sequence of values. In a string, the values are
characters; in a list (or a tuple), they can be any type. The values in list are called
elements or sometimes items.
28
Pr. Ridouani -- Chapter 1: Python Programming
Creating lists and tuples
There are several ways to create a new list; the simplest is to enclose the elements
in square brackets (“[" and “]”).
A tuple is a sequence of values much like a list. Syntactically, a tuple is a comma-
separated list of values: (t = 'a', 'b', 'c‘). Although it is not necessary, it is common to
enclose tuples in parentheses to help us quickly identify tuples when we look at
Python code: t = ('a', 'b', 'c‘).
29
Pr. Ridouani -- Chapter 1: Python Programming
Element Indexing
30
Pr. Ridouani -- Chapter 1: Python Programming
Shallow/deep copy or CLONING/ALIASES
y=x: y is an alias for x –changing one changes the other!
z=x.copy(): create a new list z and copy every element
31
Pr. Ridouani -- Chapter 1: Python Programming
Swaping two values
Tuple is conveniently used to swap variable values
32
Pr. Ridouani -- Chapter 1: Python Programming
Common sequence operations
33
Pr. Ridouani -- Chapter 1: Python Programming
Common sequence operations
34
Pr. Ridouani -- Chapter 1: Python Programming
Common sequence operations
35
Pr. Ridouani -- Chapter 1: Python Programming
Iterating on sequeces
36
Pr. Ridouani -- Chapter 1: Python Programming
Iterating on sequeces (creating list)
37
Pr. Ridouani -- Chapter 1: Python Programming
Zipping sequences together
38
Pr. Ridouani -- Chapter 1: Python Programming
Convert List to String and back
•convert string to list with list(s), returns a list with every character from san element
in L
•can use s.split(), to split a string on a character parameter, splits on spaces if called
without a parameter
•use ‘ '.join(L) to turn a list of characters into a string, can give a character in quotes to
add char between every element
39
Pr. Ridouani -- Chapter 1: Python Programming
Dictionaries
How to store student info?
•so far, can store using separate lists for every info
40
Pr. Ridouani -- Chapter 1: Python Programming
Dictionaries
How to update/retrieve student info?
41
Pr. Ridouani -- Chapter 1: Python Programming
Dictionaries
A dictionary is like a list, but more general. In a list, the index positions have to
be integers; in a dictionary, the indices can be (almost) any type.
You can think of a dictionary as a mapping between a set of indices (which are
called keys) and a set of values. Each key maps to a value. The association of a
key and a value is called a key-value pair or sometimes an item..
42
Pr. Ridouani -- Chapter 1: Python Programming
Creating Dictionaries
43
Pr. Ridouani -- Chapter 1: Python Programming
Creating Dictionaries
44
Pr. Ridouani -- Chapter 1: Python Programming
Read-only dictionary operations
45
Pr. Ridouani -- Chapter 1: Python Programming
Mutable dictionary operations
46
Pr. Ridouani -- Chapter 1: Python Programming
Dictionary iterartion
47
Pr. Ridouani -- Chapter 1: Python Programming
Dictionary iterartion
48
Pr. Ridouani -- Chapter 1: Python Programming
Dictionaries
list vs dict
list dict
ordered sequence of elements matches “keys” to “values”
look up elements by an look up one item by another
integer index item
indices have an order no order is guaranteed
index is an integer key can be any immutable
type
49
Pr. Ridouani -- Chapter 1: Python Programming
Dictionaries
Example: create a frequency dictionary mapping character c:int
50
Pr. Ridouani -- Chapter 1: Python Programming
Functions
•write reusable pieces/chunks of code, called functions
•functions are not run in a program until they are “called” or “invoked” in a program
function characteristics:
•has a name
•has parameters (0 or more)
•has a docstring (optional but recommended)
•has a body
•returns something
51
Pr. Ridouani -- Chapter 1: Python Programming
Functions
In the function body
52
Pr. Ridouani -- Chapter 1: Python Programming
Functions
Variable scope
53
Pr. Ridouani -- Chapter 1: Python Programming
Functions
Variable scope
54
Pr. Ridouani -- Chapter 1: Python Programming
Functions
Variable scope
55
Pr. Ridouani -- Chapter 1: Python Programming
Functions
Variable scope
56
Pr. Ridouani -- Chapter 1: Python Programming
Functions
Variable scope
57
Pr. Ridouani -- Chapter 1: Python Programming
Functions
One warning if no return statement
58
Pr. Ridouani -- Chapter 1: Python Programming
Functions
Functions as arguments
59
Pr. Ridouani -- Chapter 1: Python Programming
Functions
Functions as arguments
60
Pr. Ridouani -- Chapter 1: Python Programming
Functions
Functions as arguments
61
Pr. Ridouani -- Chapter 1: Python Programming
Functions
Functions as arguments
62
Pr. Ridouani -- Chapter 1: Python Programming
Functions
Scope example
63
Pr. Ridouani -- Chapter 1: Python Programming
Functions
Scope example
64
Pr. Ridouani -- Chapter 1: Python Programming
Functions
Python Tutor is your best friend to help sort this out!
http://www.pythontutor.com/
#########################
## EXAMPLE: functions as arguments
#########################
def func_a():
print('inside func_a')
def func_b(y):
print('inside func_b')
return y
def func_c(z):
print('inside func_c')
return z()
print(func_a())
print(5+func_b(2))
print(func_c(func_a))
65
Pr. Ridouani -- Chapter 1: Python Programming
Functions
Lambda
f = lambda x, i : x**i
f(2, 4)
66
Pr. Ridouani -- Chapter 1: Python Programming
File
File Handling
The key function for working with files in Python is the open() function.
The open() function takes two parameters; filename, and mode.
There are four different methods (modes) for opening a file:
"r" - Read - Default value. Opens a file for reading, error if the file does not exist
"a" - Append - Opens a file for appending, creates the file if it does not exist
"w" - Write - Opens a file for writing, creates the file if it does not exist
"x" - Create - Creates the specified file, returns an error if the file exists
https://www.w3schools.com/python/python_file_handling.asp
67
Pr. Ridouani -- Chapter 1: Python Programming
File
Read files
To open the file, use the built-in open() function.
The open() function returns a file object, which has a read() method for reading the
content of the file: https://www.w3schools.com/python/python_file_open.asp
68
Pr. Ridouani -- Chapter 1: Python Programming
File
Write to an Existing File
To write to an existing file, you must add a parameter to the open() function:
"a" - Append - will append to the end of the file
"w" - Write - will overwrite any existing content
https://www.w3schools.com/python/python_file_write.asp
69
Pr. Ridouani -- Chapter 1: Python Programming
File
Create a New File
To create a new file in Python, use the open() method, with one of the following
parameters:
"x" - Create - will create a file, returns an error if the file exists
"a" - Append - will create a file if the specified file does not exists
"w" - Write - will create a file if the specified file does not exists
https://www.w3schools.com/python/python_file_write.asp
70
Pr. Ridouani -- Chapter 1: Python Programming
File
Example:
# Code: http://www.py4e.com/code3/open.py
71
Pr. Ridouani -- Chapter 1: Python Programming