Friday 20 September 2013

Learning Python-
Intermediate
Lesson 3 : Lists & Functions
Carter Draper
Sources: Zane Cochran, Allan Martell & Code AcademyLet’s get ready!
 List Review
 Function Review
 Using Functions With Lists
 Using the Entire List in a Function
 Lists of ListsList Review
 Indexing an element in a list
 x[n]
 Modifying an element in a list
 x[n] = v
 Appending an element to a list
 x.append(item)List Review
3 Ways to Remove Elements from
Lists
 Remove
 n.remove(item) will remove the actual item if it
finds it.
 Del
 del(n[1] is like .pop in that it will remove the item
at the given index, but it won't return it.
 Pop
 n.pop(index) will remove the item at index from
the list and return it to you.Assignment
Lesson 3
List Review
Exercise 0-3Function Review
 Function Syntax
def test(z):
return z + 1
print test(5)
⇒ Declare Function
⇒ Return Value
⇒ Call Function
 Unknown Number of Arguments
def test2(*args):
print args[0]
⇒ gets all arguments
⇒ prints first argumentAssignment
Lesson 3
Function Review
Exercise 0-2Using Functions With Lists
 You can pass lists to
functions just like any
other argument.
 You can modify elements
in a list while in a function.
 You can also add and
remove elements onto a
list inside a function
 Functions can return single
elements from the list or
the entire list.
my_list= [ 8, 2, 1, 9 ]
def test1(a_list):
return a_list [1]
test1(my_list)
def test2(a_list):
a_list[1] += 1
return a_list
test2(my_list)Assignment
Lesson 3
List Functions
Exercise 0-2Using the Entire List in a
Function
 We’ve changed or added
or removed ONE element
in a list.
 How could we change
multiple elements or all
the elements?
 We can use a for loop and
the range function.Using the Entire List in a
Function
 range( )
 A shortcut for
generating a list
 Can take 3 different
arguments
depending on what
you want it to do
1 Argument
Starts at 0
Increases by 1 until 1 less than 1st argument
range(2) ⇒ [0, 1]
2 Arguments
Starts at 1st argument
Increases by 1 until 1 less than 2nd argument
range(1,3) ⇒ [1, 2]
3 Arguments
Starts at 1st argument
Increases by 3rd argument until less than
2nd argument
range(2,8,3) ⇒ [2,5]Using the Entire List in a
Function
 range(3)
 range(0)
 range(0, 5)
 range(2, 4)
 range(2, 5, 2)
 range(1, 12, 3)
[0, 1, 2]
[]
[0, 1, 2, 3, 4]
[2, 3]
[2, 4]
[1, 4, 7, 10]Using the Entire List in a
Function
 Let’s pretend we have a
list [3, 5, 7]
 How could we find out the
result of all the items in
the list added together?
 What if we didn’t know
how many items were in
the list?
# The Hard Way
n = [3, 5, 7]
count = n[0] + n[1] + n[2]
print count
# The Easy Way
def total(a_list):
sum = 0
for item in a_list:
sum += item
return sumAssignment
Lesson 3
Entire List
Exercise 0-4Lists of Lists
 We know functions can take two arguments
 What types of things could we do if we gave it
two lists?
 farmA = [“sheep” , “chicken”, “cow”]
 farmB = [“goat”, “horse”, “duck”]
 We could write a function to combine these lists!
 You can combine lists like this: farmA + farmBLists of Lists
 We’ve looked at lists of numbers and lists of
strings
 What about lists of lists?
List of Lists
List 1 , List 2 , List 3
-----------------------------------------------
warehouse = [ [1, 2, 3], [4, 5, 6, 7], [8, 9]
]Assignment
Lesson 3
List Lists
Exercise 0-2Quick Look & Reflection
 Are you becoming more comfortable with using lists
and functions?
 What has been your favorite thing so far?
 When would it make sense to have a list of lists?

No comments:

Post a Comment