Presentation is loading. Please wait.

Presentation is loading. Please wait.

Python Mini-Course University of Oklahoma Department of Psychology

Similar presentations


Presentation on theme: "Python Mini-Course University of Oklahoma Department of Psychology"— Presentation transcript:

1 Python Mini-Course University of Oklahoma Department of Psychology
Day 4 – Lesson 14 Lists Python Mini-Course University of Oklahoma Department of Psychology Python Mini-Course: Day 4 – Lesson 14 5/02/09

2 Lesson objectives Describe the characteristics of the list data structure in Python Perform basic operations with lists including creation, concatenation, repetition, slicing, and traversing Use string methods that require lists (join, split) Use lists in functions Python Mini-Course: Day 4 – Lesson 14 5/02/09

3 The list data structure
In Python, a list is a mutable sequence of values Each value in the list is an element or item Elements can be any Python data type Lists can mix data types Elements can be nested lists Python Mini-Course: Day 4 – Lesson 14 5/02/09

4 Creating lists numbers = [1, 2, 3, 4] print numbers cheeses = ['swiss', 'cheddar', 'ricotta', 'gouda'] print cheeses Python Mini-Course: Day 4 – Lesson 14 5/02/09

5 Creating lists mixed = [1, 'a', 3.45] print mixed single = ['z'] print single, type(single) empty = [] print empty Python Mini-Course: Day 4 – Lesson 14 5/02/09

6 Repeating a list Use the * operator: meat = ['spam']*4 print meat
Python Mini-Course: Day 4 – Lesson 14 5/02/09

7 List indexing Elements within a list are indexed (see Lesson 10)
print cheeses[0] Lists are mutable cheeses[0] = 'Feta' print cheeses Python Mini-Course: Day 4 – Lesson 14 5/02/09

8 Slicing a list Like strings and other sequences, lists can be sliced
print cheeses[1:4] print cheeses[:2] print cheeses[2:] Python Mini-Course: Day 4 – Lesson 14 5/02/09

9 Changing a slice roster = ['Meghan', 'Tricia', 'Juan', 'Alton', 'Darrel', 'Jen'] print roster roster[1:3] = ['Sam', 'Kerri'] roster[3:5] = ['Tayla'] Python Mini-Course: Day 4 – Lesson 14 5/02/09

10 Inserting elements Slice notation print roster
roster[2:2] = ['Dana', 'Ryan'] print roster Python Mini-Course: Day 4 – Lesson 14 5/02/09

11 Deleting elements Set slice to empty list The del keyword
roster[3:5] = [] print roster The del keyword del roster[2:3] Python Mini-Course: Day 4 – Lesson 14 5/02/09

12 The insert and append methods
The insert method roster.insert(2,'Jakob') print roster The append method roster.append('Tonya') Python Mini-Course: Day 4 – Lesson 14 5/02/09

13 The extend method Adds a list to the end of an existing list
adds = ['Ian', 'Stacie'] roster.extend(adds) print roster Python Mini-Course: Day 4 – Lesson 14 5/02/09

14 Extending a list Can also use += operator roster += ['Anya']
print roster Python Mini-Course: Day 4 – Lesson 14 5/02/09

15 Using the + operator a = [1, 2, 3] b = [4, 5, 6] c = a + b
print a, b, c *The + operator returns a new list that is a concatenation of two lists Python Mini-Course: Day 4 – Lesson 14 5/02/09

16 Note on list operations
Be careful when using the + operator and append method Try this: d = c + 7 Or this c.append(b) print c Python Mini-Course: Day 4 – Lesson 14 5/02/09

17 List assignment and aliasing
b = a c = a[:] a[2] = 9 print a, b, c *The slice operator returns a copy of a list Python Mini-Course: Day 4 – Lesson 14 5/02/09

18 Other list methods roster.sort() print roster roster.reverse()
Python Mini-Course: Day 4 – Lesson 14 5/02/09

19 Other list methods print roster.index('Tonya') print roster.index('Tonya', 2, 5) print roster.count('Sam') roster.remove('Sam') print roster Python Mini-Course: Day 4 – Lesson 14 5/02/09

20 The join string method Concatenates a sequence of strings into a single string with sep inserted between each item. Syntax: sep.join(list) Python Mini-Course: Day 4 – Lesson 14 5/02/09

21 The split string method
Returns a list of words from a string using sep as the delimiter string Syntax: sep.split(list) Python Mini-Course: Day 4 – Lesson 14 5/02/09

22 Example: join_split.py
t = ['pining', 'for', 'the', 'fjords'] delimiter = '_' s = delimiter.join(t) print s u = s.split(delimiter) print u Python Mini-Course: Day 4 – Lesson 14 5/02/09

23 Example print ''.join(t) print ' '.join(t) print '\t'.join(t)
Python Mini-Course: Day 4 – Lesson 14 5/02/09

24 Traversing a list for index in range(len(roster)): print roster[index] for student in roster: print student for index, student in enumerate(roster): print index, student Python Mini-Course: Day 4 – Lesson 14 5/02/09

25 Traversing a list What does this do? empty = [] for x in empty:
print x Python Mini-Course: Day 4 – Lesson 14 5/02/09

26 Nested lists nested = [[1,2,3],[4,5,6],[7,8,9]] print nested print nested[0] print nested[0][1] Python Mini-Course: Day 4 – Lesson 14 5/02/09

27 Traversing nested lists
for i in range(len(nested)): for j in range(len(nested[i])): print nested[i][j] Python Mini-Course: Day 4 – Lesson 14 5/02/09

28 Traversing nested lists
for nest in nested: for item in nest: print item Python Mini-Course: Day 4 – Lesson 14 5/02/09

29 Using lists: cumulate.py
def cumulate(seq): c_sum = 0 for item in seq: c_sum += item return c_sum a = [12, 78, 32, 82] s = cumulate(a) print s Python Mini-Course: Day 4 – Lesson 14 5/02/09

30 Returning lists from functions: only_upper.py
def only_upper(t): res = [] for s in t: if s.isupper(): res.append(s) return res text = 'Bold cOlOrs Make for Easy Reading' secret = only_upper(text) print secret Python Mini-Course: Day 4 – Lesson 14 5/02/09

31 Modifying lists in functions
In Python, arguments are passed by reference The parameter in the function is an alias for the argument that was passed in If a mutable object is changed inside the function, it is also changed outside the function Python Mini-Course: Day 4 – Lesson 14 5/02/09

32 Example: byref.py def change(seq): print 'Passed in: ' + str(seq) seq.append('new item') print 'Changed to: ' + str(seq) original = [1, 2, 3] print original change(original) Python Mini-Course: Day 4 – Lesson 14 5/02/09

33 Example: byref2.py def change(seq): print 'Passed in: ' + str(seq) seq.append('new item') print 'Changed to: ' + str(seq) new_seq = ['created','in','function'] print 'New seq: ' + str(new_seq) original = [1, 2, 3] new_seq = ['outside','the','function'] print original change(original) print new_seq Python Mini-Course: Day 4 – Lesson 14 5/02/09

34 Suggested exercises Exercise 10.5 – Solving the "Birthday Paradox" by a Monte Carlo simulation Exercise 10.6 – Removing duplicates from a list Exercise 10.8 – Bisection search Python Mini-Course: Day 4 – Lesson 14 5/02/09


Download ppt "Python Mini-Course University of Oklahoma Department of Psychology"

Similar presentations


Ads by Google