0% found this document useful (0 votes)
55 views9 pages

CH 3 2 Slides Edx

This document introduces Python methods and built-in functions for data science. It discusses how everything in Python is an object that can have methods associated with its type. It provides examples of using common list, string, and float methods like index(), capitalize(), and append(). The document demonstrates how methods allow calling functions on objects, unlike built-in functions that operate independently.

Uploaded by

kencasanov
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views9 pages

CH 3 2 Slides Edx

This document introduces Python methods and built-in functions for data science. It discusses how everything in Python is an object that can have methods associated with its type. It provides examples of using common list, string, and float methods like index(), capitalize(), and append(). The document demonstrates how methods allow calling functions on objects, unlike built-in functions that operate independently.

Uploaded by

kencasanov
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

INTRO TO PYTHON FOR DATA SCIENCE

Methods
Intro to Python for Data Science

Built-in Functions
Maximum of list: max()
Length of list or string: len()
Get index in list: ?
Reversing a list: ?
Intro to Python for Data Science

Back 2 Basics type examples of methods

In [1]: sister = "liz" Object str capitalize()


replace()

In [2]: height = 1.73 Object float bit_length()


conjugate()

In [3]: fam = ["liz", 1.73, "emma", 1.68, Object list index()


"mom", 1.71, "dad", 1.89] count()

Methods: Functions that belong to objects


Intro to Python for Data Science

list methods
In [4]: fam
Out[4]: ['liz', 1.73, 'emma', 1.68, 'mom', 1.71, 'dad', 1.89]

In [5]: fam.index("mom")
"Call method index() on fam"
Out[5]: 4

In [6]: fam.count(1.73)
Out[6]: 1
Intro to Python for Data Science

str methods
In [7]: sister
Out[7]: 'liz'

In [8]: sister.capitalize()
Out[8]: 'Liz'

In [9]: sister.replace("z", "sa")


Out[9]: 'lisa'
Intro to Python for Data Science

Methods
Everything = object
Object have methods associated, depending on type

In [10]: sister.replace("z", "sa")


Out[10]: 'lisa'

In [11]: fam.replace("mom", "mommy")


AttributeError: 'list' object has no attribute 'replace'

In [12]: sister.index("z")
Out[12]: 2

In [13]: fam.index("mom")
Out[13]: 4
Intro to Python for Data Science

Methods (2)
In [14]: fam
Out[14]: ['liz', 1.73, 'emma', 1.68, 'mom', 1.71, 'dad', 1.89] !
In [15]: fam.append("me")

In [16]: fam
Out[16]: ['liz', 1.73, 'emma', 1.68, 'mom', 1.71, 'dad', 1.89, 'me']

In [17]: fam.append(1.79)

In [18]: fam
Out[18]: ['liz', 1.73, 'emma', 1.68, 'mom', 1.71, 'dad', 1.89, 'me', 1.79]
Intro to Python for Data Science

Summary
Functions

In [11]: type(fam)
Out[11]: list

Methods: call functions on objects

In [12]: fam.index("dad")
Out[12]: 6
INTRO TO PYTHON FOR DATA SCIENCE

Lets practice!

You might also like