0% found this document useful (0 votes)
5 views

Introduction-to-Python-Chapter-1-3-Functions-and-Packages

The document provides an introduction to functions and methods in Python, highlighting their importance and usage. It explains built-in functions like max() and round(), as well as methods associated with objects such as lists and strings. Additionally, it discusses packages in Python, including how to install and import them, with examples using NumPy.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Introduction-to-Python-Chapter-1-3-Functions-and-Packages

The document provides an introduction to functions and methods in Python, highlighting their importance and usage. It explains built-in functions like max() and round(), as well as methods associated with objects such as lists and strings. Additionally, it discusses packages in Python, including how to install and import them, with examples using NumPy.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

Functions

Introduction to Python

Hugo Bowne-
Anderson
Data Scientist at DataCamp
Functions
• Nothing new!
• type()
• Piece of reusable code
• Solves particular task
• Call function instead of writing code yourself
Example
fam = [1.73, 1.68, 1.71, 1.89]

fam
[1.73, 1.68, 1.71, 1.89]

max(fam)

1.89
Example
fam = [1.73, 1.68, 1.71, 1.89]

fam
[1.73, 1.68, 1.71, 1.89]

max(fam)

1.89
Example
fam = [1.73, 1.68, 1.71, 1.89]

fam
[1.73, 1.68, 1.71, 1.89]

max(fam)

1.89
Example
fam = [1.73, 1.68, 1.71, 1.89]

fam
[1.73, 1.68, 1.71, 1.89]

max(fam)

1.89

tallest = max(fam)

tallest
1.89
round()
round(1.68, 1)
1.7

round(1.68)

help(round) # Open up documentation

Help on built-in function round in module builtins:


round(number, ndigits=None)
Round a number to a given precision in decimal digits.
The return value is an integer if ndigits is omitted or
None.
Otherwise the return value has the same type as the number.
ndigits may be negative.
round()
help(round)

Help on built-in function round in module builtins:


round(number, ndigits=None)
Round a number to a given precision in decimal digits.
The return value is an integer if ndigits is omitted or None.
Otherwise the return value has the same type as the number. ndigits
may be negative.
round()
help(round)

Help on built-in function round in module builtins:


round(number, ndigits=None)
Round a number to a given precision in decimal digits.
The return value is an integer if ndigits is omitted or None.
Otherwise the return value has the same type as the number. ndigits
may be negative.
round()
help(round)

Help on built-in function round in module builtins:


round(number, ndigits=None)
Round a number to a given precision in decimal digits.
The return value is an integer if ndigits is omitted or None.
Otherwise the return value has the same type as the number. ndigits
may be negative.
round()
help(round)

Help on built-in function round in module builtins:


round(number, ndigits=None)
Round a number to a given precision in decimal digits.
The return value is an integer if ndigits is omitted or None.
Otherwise the return value has the same type as the number. ndigits
may be negative.
round()
help(round)

Help on built-in function round in module builtins:


round(number, ndigits=None)
Round a number to a given precision in decimal digits.
The return value is an integer if ndigits is omitted or None.
Otherwise the return value has the same type as the number. ndigits
may be negative.
round()
help(round)

Help on built-in function round in module builtins:


round(number, ndigits=None)
Round a number to a given precision in decimal digits.
The return value is an integer if ndigits is omitted or None.
Otherwise the return value has the same type as the number. ndigits
may be negative.
round()
help(round)

Help on built-in function round in module builtins:


round(number, ndigits=None)
Round a number to a given precision in decimal digits.
The return value is an integer if ndigits is omitted or None.
Otherwise the return value has the same type as the number. ndigits
may be negative.
round()
help(round)

Help on built-in function round in module builtins:


round(number, ndigits=None)
Round a number to a given precision in decimal digits.
The return value is an integer if ndigits is omitted or None.
Otherwise the return value has the same type as the number. ndigits
may be negative.
round()
help(round)

Help on built-in function round in module builtins:


round(number, ndigits=None)
Round a number to a given precision in decimal digits.
The return value is an integer if ndigits is omitted or None.
Otherwise the return value has the same type as the number. ndigits
may be negative.
round()
help(round)

Help on built-in function round in module builtins:


round(number, ndigits=None)
Round a number to a given precision in decimal digits.
The return value is an integer if ndigits is omitted or None.
Otherwise the return value has the same type as the number. ndigits
may be negative.
round()
help(round)

Help on built-in function round in module builtins:


round(number, ndigits=None)
Round a number to a given precision in decimal digits.
The return value is an integer if ndigits is omitted or None.
Otherwise the return value has the same type as the number. ndigits
may be negative.
round()
help(round)

Help on built-in function round in module builtins:


round(number, ndigits=None)
Round a number to a given precision in decimal digits.
The return value is an integer if ndigits is omitted or None.
Otherwise the return value has the same type as the number. ndigits
may be negative.
round()
help(round)

Help on built-in function round in module builtins:


round(number, ndigits=None)
Round a number to a given precision in decimal digits.
The return value is an integer if ndigits is omitted or None.
Otherwise the return value has the same type as the number. ndigits
may be negative.

• round(number)
• round(number, ndigits)
Find functions
• How to know?
• Standard task -> probably function exists!
• The internet is your friend
Questions?
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: ?
Back 2 Basics

sister = "liz"

height = 1.73

fam = ["liz", 1.73, "emma", 1.68,


"mom", 1.71, "dad", 1.89]
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
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
list methods
fam

['liz', 1.73, 'emma', 1.68, 'mom', 1.71, 'dad', 1.89]

fam.index("mom") # "Call method index() on fam"

fam.count(1.68)

1
str methods
sister

'liz'

sister.capitalize()

'Liz'

sister.replace("z", "sa")

'lisa'
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'


Methods
sister.index("z")

fam.index("mom")

4
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]
Summary
Functions
type(fam)

list

Methods: call functions on objects


fam.index("dad")

6
Questions?
Packages
INTRODUCTION TO PYTHON

Hugo Bowne-
Anderson
Data Scientist at DataCamp
Motivation
• Functions and methods are powerful
• All code in Python distribution?
o Huge code base: messy
o Lots of code you won’t use
o Maintenance problem
Packages
• Directory of Python Scripts
• Each script = module
• Specify functions, methods, types
• Thousands of packages available
o NumPy
o Matplotlib
o Pandas
o scikit-learn
Install package
• http://pip.readthedocs.org/en/stable/installing/

• Download get-pip.py

• Terminal:

o python3 get-pip.py

o pip3 install numpy


Import package
import numpy
array([1, 2, 3])

NameError: name 'array' is not defined

numpy.array([1, 2, 3])

array([1, 2, 3])

import numpy as np from numpy import array


np.array([1, 2, 3]) array([1, 2, 3])

array([1, 2, 3]) array([1, 2, 3])


from numpy import array
• my_script.py
from numpy import array

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

fam_ext = fam + ["me", 1.79]

print(str(len(fam_ext)) + " elements in fam_ext")

np_fam = array(fam_ext)

• Using NumPy, but not very clear


import numpy
import numpy as np
fam = ["liz", 1.73, "emma", 1.68, "mom", 1.71, "dad", 1.89]

fam_ext = fam + ["me", 1.79]

print(str(len(fam_ext)) + " elements in fam_ext")

np_fam = np.array(fam_ext) # Clearly using NumPy


Let's practice!
INTRODUCTION TO PYTHON

You might also like