Méthodes

Introduction à Python

Hugo Bowne-Anderson

Data Scientist at DataCamp

Fonctions intégrées

  • Maximum de la liste : max()

  • Longueur de la liste ou de la chaîne : len()

  • Obtenir l'index dans la liste : ?

  • Inverser une liste : ?

Introduction à Python

Retour aux fondamentaux

 

sister = "liz"
height = 1.73
fam = ["liz", 1.73, "emma", 1.68,
       "mom", 1.71, "dad", 1.89]

ch_3_2_slides.020.png

Introduction à Python

Retour aux fondamentaux

 

sister = "liz"
height = 1.73
fam = ["liz", 1.73, "emma", 1.68,
       "mom", 1.71, "dad", 1.89]
  • Méthodes : Fonctions appartenant à des objets

ch_3_2_slides.024.png

Introduction à Python

Retour aux fondamentaux

 

sister = "liz"
height = 1.73
fam = ["liz", 1.73, "emma", 1.68,
       "mom", 1.71, "dad", 1.89]
  • Méthodes : Fonctions appartenant à des objets

ch_3_2_slides.028.png

Introduction à Python

Méthodes list

fam
['liz', 1.73, 'emma', 1.68, 'mom', 1.71, 'dad', 1.89]
fam.index("mom") # "Call method index() on fam"
4
fam.count(1.73)
1
Introduction à Python

Méthodes str

sister
'liz'
sister.capitalize()
'Liz'
sister.replace("z", "sa")
'lisa'
Introduction à Python

Méthodes

  • Tout = objet

  • Les objets disposent de méthodes associées, en fonction de leur type

sister.replace("z", "sa")
'lisa'
fam.replace("mom", "mommy")
AttributeError: 'list' object has no attribute 'replace'
Introduction à Python

Méthodes

sister.index("z")
2
fam.index("mom")
4
Introduction à Python

Méthodes (2)

fam
['liz', 1.73, 'emma', 1.68, 'mom', 1.71, 'dad', 1.89]
fam.append("me")

fam
['liz', 1.73, 'emma', 1.68, 'mom', 1.71, 'dad', 1.89, 'me']
fam.append(1.79)
fam
['liz', 1.73, 'emma', 1.68, 'mom', 1.71, 'dad', 1.89, 'me', 1.79]
Introduction à Python

Récapitulatif

Fonctions

type(fam)
list

Méthodes : appeler des fonctions sur des objets

fam.index("dad")
6
Introduction à Python

Passons à la pratique !

Introduction à Python

Preparing Video For Download...