Methods

Introduction to Python

Hugo Bowne-Anderson

Data Scientist at DataCamp

Built-in Functions

  • Maximum of list: max()

  • Length of list or string: len()

  • Get index in list: ?

  • Reversing a list: ?

Introduction to Python

Back 2 Basics

 

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 to Python

Back 2 Basics

 

sister = "liz"
height = 1.73
fam = ["liz", 1.73, "emma", 1.68,
       "mom", 1.71, "dad", 1.89]
  • Methods: Functions that belong to objects

ch_3_2_slides.024.png

Introduction to Python

Back 2 Basics

 

sister = "liz"
height = 1.73
fam = ["liz", 1.73, "emma", 1.68,
       "mom", 1.71, "dad", 1.89]
  • Methods: Functions that belong to objects

ch_3_2_slides.028.png

Introduction to Python

list methods

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 to Python

str methods

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

Methods

  • Everything = object

  • Object have methods associated, depending on type

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

Methods

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

Methods (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 to Python

Summary

Functions

type(fam)
list

Methods: call functions on objects

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

Let's practice!

Introduction to Python

Preparing Video For Download...