Functions and Libraries
Introduction to Python 1
Today
• Assignment rules in Python
• Functions
o What are they good for
o Built-in Functions
o Defining New Functions
o Global vs. Local variables
o Python Modules / Packages / Libraries
Introduction to Python 2
Assignment rules
in Python
Introduction to Python 3
Assignment rules in Python
• All information in Python is stored in objects of different types (no
primitive types).
• The following statement creates a new object AND a name
pointing to it: a=3
• Assignment of a variable to another (like in b=a) actually copies
the address in memory to which the name (variable) points.
Introduction to Python 4
Example – Assignments in Python
Created by pythontutor.com
Introduction to Python 5
Example – Assignments in Python
Created by pythontutor.com
Introduction to Python 6
Data Types: Mutable vs. Immutable
• Objects of mutable types can be modified after creation.
• Lists are Mutable
• This is legal:
>>> my_list = range(10)
>>> my_list[4] = 500
• Strings are Immutable
• This is not legal (produces an error):
>>> my_string = ‘aaa’
>>> my_string[2] = ‘b’
• Numeric types (int, float,…) are also Immutable
>>> a=5
>>> a=6 (the name variable ‘a’ now references a new object)
Introduction to Python 7
Assignments of List Variables
>>> orig_list = [1,2,3]
>>> copy_list = orig_list
>>> orig_list = [6,7,8,9]
>>> copy_list
[1,2,3]
>>> orig_list
[6,7,8,9]
So far - no surprises
Try it with pythontutor.com
Introduction to Python 8
Assignments of List Variables
>>> copy_list = orig_list
>>> orig_list[0] = 1000
>>> orig_list
[1000,7,8,9]
>>> copy_list
[1000,7,8,9]
Surprise!
Introduction to Python 9
Example – Assignments in Python
• When two name variables are pointing to the
same object, changes made to the object using
one name will also be reflected when the object
is accessed using the second name (because it
is the same object!).
• This is applicable only to mutable objects which
can be changed after they are created.
Created by pythontutor.com
Introduction to Python 10
Assignments of List Variables
List assignment orig_list = [6,7,8,9] creates:
a list object, [6,7,8,9]
a reference from the variable name, orig_list,
to this object.
Memory
Memory
orig_list
]6,7,8,9[
Introduction to Python 11
Assignments of List Variables
The assignment copy_list = orig_list does not
create a new object, just a new variable name,
copy_list, which now refers to the same object.
Memory
orig_list
]6,7,8,9[
copy_list
Introduction to Python 12
Assignments of List Variables
Mutating list elements, orig_list[0] = 1000, does not create
a new object and does not change existing references.
Memory
orig_list
]1000,7,8,9[
copy_list
Introduction to Python 13
Changing Elements in Lists
TASK: Set even list elements to zero
lst = [1, 4, 3, 6, 8]
# take 1 # take 2
for elem in lst: index_lst = range(len(lst))
if elem % 2 == 0: for i in index_lst:
elem = 0 if lst[i] % 2 == 0:
print (lst) lst[i] = 0
[1, 4, 3, 6, 8] print (lst)
[1, 0, 3, 0, 0]
Introduction to Python 14
Functions
o What are they good for
o Built-in Functions
o Defining New Functions
Introduction to Python 15
What is a function in programming ?
• A function is a block of organized, reusable code
that is used to perform a single, well defined action.
• Functions provide better modularity for your
application and a high degree of code reusing
Introduction to Python 16
Function – Definition II
A named sequence of statements that
performs a specific task independently
of the remaining code.
Introduction to Python 17
Modularity enables code reuse !
Definition
Modularity is the degree to which a system's
components may be separated and recombined
(Wikipedia).
Top-down design
Improves maintainability
Enforces logical boundaries
between components
Introduction to Python 18
So – Why use functions?
• Modularity - Break a task into smaller sub-tasks
(divide and conquer), enables code reuse
• Abstraction – Solve each problem once and
wrap it in a function
• Maintenance - Solve bugs once
• Readability – Main code is shorter,
implementation details are hidden within functions
• Limited Variable Scope - Temporary variables
are restricted to the function’s scope
Introduction to Python 19
Python Code Hierarchy
Statement
function
Module
Package
Introduction to Python 20
Built-in Functions
http://docs.python.org/library/functions.html
We already used built-in functions:
>>> type(5)
<type 'int'>
>>> len(range(8,100, 8))
12
Introduction to Python 21
Built-in Functions
Conversion functions:
>>> str(5)
'5'
>>> int(3.2)
3
>>> float(‘1.41421’)
1.41421
Introduction to Python 22
Modules: Functions and Constants
All standard modules and their contents (functions, constants) can be
found at http://docs.python.org/2.7/py-modindex.html
>>> import math # mathematical functions
>>> dir(math)
['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan',
'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp',
'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot',
'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow',
'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']
Introduction to Python 23
Math Functions
import math
radius = 6378;
circumference = 2 * math.pi * radius
Pre-defined constant
log_2_1024 = math.log(1024, 2)
Pre-defined functions contained in
angle = 45 the imported math module
sinus = math.sin(math.radians(angle))
Introduction to Python 24
Defining a new function in Python
Function definition Function name Arguments (=Parameters)
Defining the function Using the function
def is_over_100(x): Function >>> is_over_100(50)
if x > 100: Body False
return True >>> a = is_over_100(150)
else: >>> a
return False True
In
de
Short definition`:
nt
Return
def is_over_100(x):
Value
at
return x > 100
i on
!
Introduction to Python 25
Functions
def function_name(argument1, argument2,...):
statement1
statement2
…
return result1, result2, … # optional, returns the
constant None if
not specified
Calling a function:
var1, var2,… = function_name(val1, val2,...)
Introduction to Python 26
Passing Arguments to Functions
In a function call, before execution:
argument values are assigned to function
arguments by order.
calculator(2, 3, ‘*’)
def calculator(x, y, op):
if op == '+':
return x+y
elif …
else:
return None
Introduction to Python 27
Default Arguments For Functions
• We can specify default values for the arguments of the function.
• The default value will be used only when the function is called
without specifying a value for the argument.
# In this call: x = 1, y = 2
# In this call: x = 3, y = 1 (the default value)
# x doesn’t have a default value, it must be specified
Introduction to Python 28
Default Arguments For Functions
• What about this case?
# In this call: x = 1, y = 2, z = 3, all default values
# In this call: x = 10, y = 2, z = 3
# In this call: x = 1 (default), y = 5, z = 3 (default)
Introduction to Python 29
Passing Arguments to Functions
Assignment rules hold!
Assigning mutable / immutable objects
Contents of mutable arguments (lists)
can be changed within a function.
a 4 5 6 7 8 9
Introduction to Python 30
Example
Introduction to Python 31
Example - GCD
def gcd(a, b):
"greatest common divisor"
while a != 0:
a, b = b%a, a # multiple assignment
return b
>>> gcd.__doc__
'greatest common divisor'
>>> gcd(12, 20)
4
Introduction to Python 32
32
Local vs. Global variables
Introduction to Python 33
Local variables
Consider the following function, operating on two
arguments:
def linear_combination(x,y):
y = 2 * y
return x + y
The formal parameters x and y are local within the
function’s scope, and their “life time" is just the
execution of the function. They disappear when the
function is returned.
3, 4 linear_combination 11
x, y
Introduction to Python 34
Local variables
>>> a = 3
>>> b = 4
>>> linear_combination(a,b)
11 # this is the correct value
>>> a
3
>>> b
4 # b has NOT changed
The change in y is local - inside the body of the function.
Introduction to Python 35
Local variables
>>> x = 3
>>> y = 4
>>> linear_combination(x,y)
11 # this is the correct value
>>> x
3
>>> y
4
The y in the calling environment and the y in the
function are not the same variable!
Introduction to Python 36
Local vs. Global variables
• A local variable is defined within a function, and
exists only within that function.
• A global variable is defined in the main script (outside
of any function) and it is known globally (including
within functions).
Global variables can be
accessed within functions.
The scope
Defining a variable by the same
of variable
name will create a local variable
hiding the global variable ‘localVar’ The scope
of variable
‘globalVar’
is the entire
Local variables exist only with program
the function in which they were
defined
5
10
Introduction to Python 11
Approximating Square Root
Given n> 0, can we find an approximation of √n?
Newton noticed that for any positive n, if we define
Then as k increases, xk gets closer
to √n (xk converges to √n).
Isaac Newton
1642-1727, United Kingdom
Introduction to Python 38
Approximating Square Root
Algorithm
• Guess x
• Improve the guess by averaging x, n/x
• Improve until the guess is close enough
Example: calculate the square root of 2
x=1
n/x = 2 x = ½ (1+ 2) = 3/2
n/x = 4/3 x = ½ (3/2 + 4/3) = 17/12 = 1.416666
n/x = 24/17 x = ½ (17/12 + 24/17) = 577/408 = 1.4142156
Introduction to Python 39
Approximating Square Root
Input
An integer n > 0
The required accuracy (float)
Output
Approximation of √n within the required accuracy
Accuracy measurement
The absolute difference between x and n/x.
Python: abs(y) is the absolute value.
Introduction to Python 40
Approximating Square Root
def sqrt_approx(n, acc):
x = 1.0
while abs(x - n/x) > acc:
x = (x + n/x) / 2
return x
>>> sqrt_approx(2, 1e-5)
1.4142156862745097
>>> sqrt_approx(2, 1e-6)
1.4142135623746899
Introduction to Python 41
Functions as arguments to functions
• An argument to a function can also be a function.
We use the argument op as a
function that accepts one
argument.
# f3 returns max([1,2,3])
# f3 returns len(“abcde”)
# f3 returns f1(5)
Introduction to Python 42
Question 1
• Input: a list of grades
• Output: maximum grade, without using built-in max.
def max_value_in_list(grades):
'''Given a list of grades between 0 and 100, return the maximum grade'''
max_grade = -1
for grd in grades:
if max_grade < grd:
max_grade = grd
return max_grade
>>> x = [78, 88, 95, 92]
>>> print (max_value_in_list(x))
95
Introduction to Python 43
Question 2
• Input: a list of grades, and a list of student names
• Output: a list of students with maximum grade
>>>grades = [99, 80, 92, 99, 97]
>>>names = ["Tom", "Ron", "Benny", "Adi", "Livnat"]
>>>print (students_with_max_grade(grades, names))
['Tom', 'Adi']
Introduction to Python 44
Question 3
• Input: a list of grades
• Output: give non-failed grades a factor of y
We can also use
min(grade[i]+y,100)
>>>print (grades)
[99, 80, 92, 99, 97]
>>>give_factor(grades, 5)
>>>print (grades)
[100, 85, 97, 100, 100]
Introduction to Python 45
Lists, Tuples, Dictionaries
Introduction to Python 46
Creating Lists
Creating list using the list class
list1 = list() # Create an empty list
list2 = list([2, 3, 4]) # Create a list with elements 2, 3, 4
list3 = list(["red", "green", "blue"]) # Create a list with strings
list4 = list(range(3, 6)) # Create a list with elements 3, 4, 5
list5 = list("abcd") # Create a list with characters a, b, c, d
For convenience, you may create a list using the following syntax :
list1 = [] # Same as list()
list2 = [2, 3, 4] # Same as list([2, 3, 4])
list3 = ["red", "green"] # Same as list(["red", "green"])
Introduction to Python 47
Functions for lists
>>> list1 = [2, 3, 4, 1, 32]
>>> len(list1)
5
>>> max(list1)
32
>>> min(list1)
1
>>> sum(list1)
42
>>> import random
>>> random.shuffle(list1) # Shuffle the items in the list
>>> list1
[4, 1, 2, 32, 3]
Introduction to Python 48
The +, *, [ : ], and in Operators
>>> list1 = [2, 3]
>>> list2 = [1, 9]
>>> list3 = list1 + list2
>>> list3
[2, 3, 1, 9]
>>> list3 = 3 * list1
>>> list3
[2, 3, 2, 3, 2, 3]
>>> list4 = list3[2 : 4]
>>> list4
[2, 3]
Introduction to Python 49
The +, *, [ : ], and in Operators
>>> list1 = [2, 3, 5, 2, 33, 21]
>>> list1[-1]
21
>>> list1[-3]
2
>>> list1 = [2, 3, 5, 2, 33, 21]
>>> 2 in list1
True
>>> list1 = [2, 3, 5, 2, 33, 21]
>>> 2.5 in list1
False
Introduction to Python 50
List Comprehension
A list comprehension consists of brackets containing an
expression followed by a for clause, then zero or more for or if
clauses. The result will be a list resulting from evaluating the
expression.
>>> list1 = [x for x in range(0, 5)] # Returns a list of 0, 1, 2, 4
>>> list1
[0, 1, 2, 3, 4]
>>> list2 = [0.5 * x for x in list1]
>>> list2
[0.0, 0.5, 1.0, 1.5, 2.0]
>>> list3 = [x for x in list2 if x < 1.5]
>>> list3
[0.0, 0.5, 1.0]
Introduction to Python 51
Tuples are immutable lists
• Defined using round brackets
>>> my_list = [1,2,3]
>>> my_tuple = (1,2,3)
>>> my_tuple[0:2]
(1,2)
>>> my_tuple[1] = 10
Traceback (most recent call last):
File "<pyshell#20>", line 1, in <module>
my_tuple[1] = 10
TypeError: 'tuple' object does not support item assignment
No append / extend / remove in Tuples!
Introduction to Python 52
Tuples are immutable lists
A tuple is similar to a list, but it is immutable.
Syntax: note the parentheses!
>>> t = ("don't", "worry", "be", "happy") # definition
>>> t
("don't", 'worry', 'be', 'happy')
>>> t[0] # indexing
"don't"
>>> t[-1] # backwords indexing
'happy'
>>> t[1:3] # slicing
('worry', 'be')
Introduction to Python 53
Tuples
>>> t[0] = 'do' # try to change
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
t[0]='do'
TypeError: 'tuple' object does not support
item assignment
Introduction to Python 54
Tuples are immutable lists
• Why use Tuples?
• Faster than lists in certain operations
• The interpreter enforces that they are not changed, and
we’ll see why this is useful.
Introduction to Python 55
Python’s types: Mutable vs. Immutable
Immutable Mutable
Numeric types: int, long, float, list
complex
tuple set
string dict
frozen set
None is a special type representing an absence of a value
Introduction to Python 56
Dictionaries (Hash Tables)
keys values
• Key – Value mapping
• No order
• Fast!
• Usage examples:
• Database
• Dictionary
• Phone book
Introduction to Python 57
Dictionaries
Access to the data in the dictionary:
• Given a key, it is easy to get the value.
• Given a value, you need to go over all the dictionary to get
the key.
Intuition - Yellow Pages:
• Given a name, it’s easy to find
the right phone number
• Given a phone number it’s
difficult to match the name
Introduction to Python 58
Dictionaries
Dictionary: a set of key-value pairs.
>>> dict_name = {key1:val1, key2:val2,…}
Keys are unique and immutable.
Introduction to Python 59
Dictionaries
Example: “144” - Map names to phone numbers:
>>> phonebook = {'Eric Cartman': '2020', 'Stan
March': '5711', 'Kyle Broflovski': '2781'}
>>> phonebook
{'Kyle Broflovski': '2781', 'Eric Cartman': ‘2020',
'Stan March': '5711'}
Note:The pairs order changed!
Introduction to Python 60
Dictionaries
Access dictionary Items:
>>> phonebook['Eric Cartman']
'2020'
Add a new person:
>>> phonebook['Kenny McCormick'] = '1234'
>>> phonebook
{'Kyle Broflovski': '2781', 'Eric Cartman': '2020',
'Kenny McCormick': ‘1234', 'Stan March': '5711'}
Introduction to Python 61
Dictionaries
What happens when we add a key that already exists?
>>> phonebook['Kenny McCormick']= '2222'
>>> phonebook
{'Kyle Broflovski': '2781', 'Eric Cartman': '2020',
'Kenny McCormick': '2222', 'Stan March': '5711'}
Kenny’s phone was
previously 1234 and
now changed to
2222
How can we add another Kenny McCormick in the phone book?
Introduction to Python 62
Dictionaries
Idea: Add address to the key
new key
>>> phonebook= {['Kenny McCormick', 'Southpark']: '2222'}
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
phonebook= {['Kenny McCormick', 'Southpark']: '2222'}
TypeError: unhashable type: 'list'
What’s the problem?
Introduction to Python 63
Dictionaries
Fix: use tuples as keys!
>>> phonebook= {('Kenny McCormick', 'Southpark'): '2222'}
>>> phonebook
{('Kenny McCormick', 'Southpark'): '2222'}
Introduction to Python 64
Dictionary Methods
Function Description
D.get(k, [d]) return D[k] for k in D, otherwise d (default: None).
D.has_key(k) True if D has a key k, False otherwise.
D.items() list of (key, value) pairs, as 2-tuples
D.keys() list of D's keys
D.values() list of D's values
D.pop(k, [d]) remove the specified key k and return its value. If k is
not found, return d.
• An argument shown in [] has a default value. We don’t have to specify the
second argument to get and pop methods.
Introduction to Python 65
Example: Frequency Counter
Assume you want to learn about the frequencies of English
letters usage in text.
You find a long and representative text and start counting.
Which data structure will you use to keep your findings?
supercalifragilisticexpialidocious
Introduction to Python 66
Frequency Counter
text = 'supercalifragilisticexpialidocious'
# count letters – build letters histogram
def get_char_count(text):
char_count = {}
for char in text:
count = char_count.get(char, 0)
count += 1
char_count[char] = count
return char_count
# a shorter version:
char_count = {}
for char in text:
char_count[char] = char_count.get(char, 0) + 1
Introduction to Python 67
Frequency Counter
In what order can we print the counts?
1. We can print the results in an alphabetic order: print the
count for “a”, then “b” and so on.
Sort by the keys
2. We can print the results ordered by the counts: start from
the most frequent letter and finish with the least frequent
Sort by the values
Introduction to Python 68
Print by keys order
# text = 'supercalifragilisticexpialidocious‘
def print_by_keys_order(char_count):
chars = char_count.keys()
sorted_chars = sorted(chars)
for char in sorted_chars:
print (char , ':', char_count[char])
The output is:
a:3
c:3
d:1
e:2
f:1
… Introduction to Python 69
Print by values order
# text = 'supercalifragilisticexpialidocious‘
def print_by_values_order(char_count):
chars = char_count.keys()
sorted_chars = sorted(chars, key=char_count.get, reverse=True)
for char in sorted_chars:
print (char , ':', char_count[char])
The output is:
i:7
a:3
c:3
l:3
s:3
… Introduction to Python 70