Introduction to Object-Oriented Programming in Python
George Boorman
Curriculum Manager, DataCamp
$$\Large{\text{Object = data + functionality}}$$
State - an object's data
Behavior - an object's functionality
Object | Type |
---|---|
5 |
int |
"Hello" |
str |
pd.DataFrame() |
DataFrame |
sum() |
function |
... | ... |
lists
are a class[1, 2, 3, 4, 5]
.append()
type()
to find the classtype([1, 2, 3, 4, 5])
<class 'list'>
import pandas as pd df = pd.DataFrame({"a": [1,2,3], "b": [4,5,6]})
# shape attribute df.shape
(3, 2)
obj.
to access attributes and methodsimport pandas as pd df = pd.DataFrame({"a": [1,2,3], "b": [4,5,6]})
# head method df.head()
a b
0 1 4
1 2 5
2 3 6
# Display attributes and methods
dir([1, 2, 3, 4])
['__add__',
'__class__',
'__contains__',
'__delattr__',
...
'pop',
'remove',
'reverse',
'sort']
# Display attributes and methods
dir(list)
['__add__',
'__class__',
'__contains__',
'__delattr__',
...
'pop',
'remove',
'reverse',
'sort']
Term | Definition |
---|---|
Class | A blueprint/template used to build objects |
Object | A combination of data and functionality; An instance of a class |
State | Data associated with an object, assigned through attributes |
Behavior | An object's functionality, defined through methods |
Introduction to Object-Oriented Programming in Python