Presentation is loading. Please wait.

Presentation is loading. Please wait.

From Think Python How to Think Like a Computer Scientist

Similar presentations


Presentation on theme: "From Think Python How to Think Like a Computer Scientist"— Presentation transcript:

1 From Think Python How to Think Like a Computer Scientist
Python LISTS chapter 10 From Think Python How to Think Like a Computer Scientist

2 Introduction to lists A list is a sequence of values (called elements) that can be any type. Here are some examples of lists [2,4,6,8,10] [‘a’,’b’,’c’,’d’,’e’] [‘hello’,’there’,’Bob’] [‘bob’,23.0,145,[1,2]] # This contains a string, float, int and # another string. Each is an element # We can put these in variables nums = [3,2,5,4.5,3.0,1000] names = [‘Bob’,’Sally’,’Tom’,’Harry] empty = [] #This is the empty string print names  Check this out >>> ['Bob', 'Sally', 'Tom', 'Harry']

3 Lists are ordered and Mutable
#Unlike strings we can modify the individual elements of a list. numbers =[7,-2,3,4,5,6.0] #list indices work like string #indices numbers[3]=10 print numbers # we can print them [7,-2,3,10,5,6.0] #The in operator works here as well >>>3 in numbers True

4 Traversing a list for val in numbers: print val, # Square each number in list for I in range(len(numbers)): numbers[i]=numbers[i]**2 print numbers [49, 4, 9, 100, 25, 36.0] The length of the following list is 4. s = [[1,2],3.0,’Harry’,[3,5,6,1,2]] Of course the length of s[3] is 5 The length of [] is 0

5 Operations on list + works like it does on strings, i.e. it concatinates [1,2,3]+ [4,5,6] becomes [1,2,3,4,5,6] and [1,2,3]*3 becomes [1,2,3,1,2,3,1,2,3] What does 10*[0] give you? [0,0,0,0,0,0,0,0,0,0]

6 List slices t = [11,21,13,44,56,68] print t[2,4] [13,44] # Remember: It doesn’t include slot 4!! print t[3:] [44,56,68] # check this out t[2:5]=[7,3,1] # You can update multiple elements print t [11,21,7,3,1,68]

7 List Methods #Sort: sorts the list in place t = [4,3,5,2,7,1] t.sort()
# Append : adds a new element # to the end t=[2,4,6] t.append(8) print t [2,4,6,8] #extend: adds a list to end of list t=[‘a’,b’,c’,d’] t.extend([‘w’,’x’]) [‘a’,b’,c’,d’,’w’,’x’] #Sort: sorts the list in place t = [4,3,5,2,7,1] t.sort() print t #t is modified! [1,2,3,4,5,7]

8 Accumulating a list def add_them(t): # here t is a list of numbers total =0 for x in t: total += x # same as total = total + x return total # Python already has something like this built in, called sum total = sum(t) #would return its sum # of course you could write your own function to do anything #you want to the elements of t

9 Returning a list #What does the following do? def do_it (s): # Here s is a list of strings res =[] for a in s: res.append(s.capitalize()) return res These guys traverse a list and return parts of it in another list #Returns a list of positive #numbers def get_pos(t): res = [] for i in t: if i>0: res.append(i) return res

10 Deleting from list t=[6,3,7,8,1,9] x=t.pop(3) #remove element in slot 3 and assign it to x print t [6,3,7,1,9] #note : element in slot 3 ie 8 is now gone del t[3] #does the same thing as pop(3) but returns nothing t.remove(8) #use this to remove an 8 from the list # it returns nothing

11 Processing a list and graphing using Matplotlib
from pylab import * x= [-5,-4,-3,-2,-1,0,1,2,3,4,5] y=[] #build y from x for i in x: y.append(i**2-2*i+3) print y plot(x,y) show() Plots parameters are lists!


Download ppt "From Think Python How to Think Like a Computer Scientist"

Similar presentations


Ads by Google