Methoden

Introductie tot Python

Hugo Bowne-Anderson

Data Scientist at DataCamp

Ingebouwde functies

  • Maximum van lijst: max()

  • Lengte van de lijst of string: len()

  • Index ophalen in lijst: ?

  • Een lijst omdraaien: ?

Introductie tot Python

Terug naar de basis

 

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

ch_3_2_slides.020.png

Introductie tot Python

Terug naar de basis

 

sister = "liz"
height = 1.73
fam = ["liz", 1.73, "emma", 1.68,
       "mom", 1.71, "dad", 1.89]
  • Methoden: functies die bij objecten horen

ch_3_2_slides.024.png

Introductie tot Python

Terug naar de basis

 

sister = "liz"
height = 1.73
fam = ["liz", 1.73, "emma", 1.68,
       "mom", 1.71, "dad", 1.89]
  • Methoden: Functies die bij objecten horen

ch_3_2_slides.028.png

Introductie tot Python

lijstmethoden

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
Introductie tot Python

str-methoden

sister
'liz'
sister.capitalize()
'Liz'
sister.replace("z", "sa")
'lisa'
Introductie tot Python

Methoden

  • Alles = object

  • Objecten hebben bijbehorende methoden, afhankelijk van het type

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

Methoden

sister.index("z")
2
fam.index("mom")
4
Introductie tot Python

Methoden (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]
Introductie tot Python

Samenvatting

Functies

type(fam)
list

Methoden: functies aanroepen op objecten

fam.index("dad")
6
Introductie tot Python

Laten we oefenen!

Introductie tot Python

Preparing Video For Download...