2018-03-07 HW

posted Mar 7, 2018, 6:18 AM by Konstantinovich Samuel   [ updated Mar 7, 2018, 7:09 AM ]
Goal: Lists

A list in python is a collection of elements. Each element can be any data type. 
We use square brackets to designate a list:

Here is a list of integers:
[ 1, 2, 3]

You can assign a list to a variable just like every other data type:

nums = [8, 6, 7, 5, 3, 0, 9]

The len operator works on lists too!

print len(nums) #prints 7

You can access individual elements just like a string with:

a. an index
print nums[0]   #prints 8
print nums[len(nums)-1]  #prints 9

b. a slice:
print nums[2:4] #prints [7,5]

lets write a function that accepts a list as a parameter and does something to it:

def doStuffToList( nums ):

Notice the parameter is a variable, not a list of variables. The parameter is the variable you put the argument in.

doStuffToList( [1,2,3] )  #when you call a function, copy the arguments into the parameters:

nums is now [1,2,3]  and the rest of the code will execute.

DO NOT do this:
def f( [x] ):

def f( "x" ):

Work in class, complete at home:
ListProblems (no loops)
 all 4 problems
List problems (single loops)
 count evens
 big diff
 centered average