Intro Python Part2
Intro Python Part2
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
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
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
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'>
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
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)
Short definition`:
nt
Return
def is_over_100(x):
Value
at
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
Introduction to Python 28
Default Arguments For Functions
• What about this case?
Introduction to Python 29
Passing Arguments to Functions
Assignment rules hold!
Assigning mutable / immutable objects
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
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).
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
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
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
Introduction to Python 44
Question 3
• Input: a list of grades
• Output: give non-failed grades a factor of y
>>>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 :
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
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!
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
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.
Introduction to Python 58
Dictionaries
Dictionary: a set of key-value pairs.
Introduction to Python 59
Dictionaries
Example: “144” - Map names to phone numbers:
Introduction to Python 60
Dictionaries
Access dictionary Items:
>>> phonebook['Eric Cartman']
'2020'
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'}
Introduction to Python 62
Dictionaries
Idea: Add address to the key
new key
Introduction to Python 63
Dictionaries
Fix: use tuples as keys!
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.
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.
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)
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])