Python Mini-Course University of Oklahoma Department of Psychology

Slides:



Advertisements
Similar presentations
TWO STEP EQUATIONS 1. SOLVE FOR X 2. DO THE ADDITION STEP FIRST
Advertisements

Advanced Piloting Cruise Plot.
1
Feichter_DPG-SYKL03_Bild-01. Feichter_DPG-SYKL03_Bild-02.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
Chapter 1 The Study of Body Function Image PowerPoint
Copyright © 2011, Elsevier Inc. All rights reserved. Chapter 6 Author: Julia Richards and R. Scott Hawley.
Author: Julia Richards and R. Scott Hawley
1 Copyright © 2013 Elsevier Inc. All rights reserved. Appendix 01.
Properties Use, share, or modify this drill on mathematic properties. There is too much material for a single class, so you’ll have to select for your.
UNITED NATIONS Shipment Details Report – January 2006.
1 RA I Sub-Regional Training Seminar on CLIMAT&CLIMAT TEMP Reporting Casablanca, Morocco, 20 – 22 December 2005 Status of observing programmes in RA I.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Title Subtitle.
Properties of Real Numbers CommutativeAssociativeDistributive Identity + × Inverse + ×
Exit a Customer Chapter 8. Exit a Customer 8-2 Objectives Perform exit summary process consisting of the following steps: Review service records Close.
FACTORING ax2 + bx + c Think “unfoil” Work down, Show all steps.
Year 6 mental test 5 second questions
Year 6 mental test 10 second questions
REVIEW: Arthropod ID. 1. Name the subphylum. 2. Name the subphylum. 3. Name the order.
Break Time Remaining 10:00.
PP Test Review Sections 6-1 to 6-6
Data Structures: A Pseudocode Approach with C
1 CSE1301 Computer Programming: Lecture 27 List Manipulation.
Data Structures Using C++
ABC Technology Project
EU market situation for eggs and poultry Management Committee 20 October 2011.
EU Market Situation for Eggs and Poultry Management Committee 21 June 2012.
1 Undirected Breadth First Search F A BCG DE H 2 F A BCG DE H Queue: A get Undiscovered Fringe Finished Active 0 distance from A visit(A)
2 |SharePoint Saturday New York City
Green Eggs and Ham.
VOORBLAD.
15. Oktober Oktober Oktober 2012.
1 Breadth First Search s s Undiscovered Discovered Finished Queue: s Top of queue 2 1 Shortest path from s.
Copyright © 2012, Elsevier Inc. All rights Reserved. 1 Chapter 7 Modeling Structure with Blocks.
1 RA III - Regional Training Seminar on CLIMAT&CLIMAT TEMP Reporting Buenos Aires, Argentina, 25 – 27 October 2006 Status of observing programmes in RA.
BIOLOGY AUGUST 2013 OPENING ASSIGNMENTS. AUGUST 7, 2013  Question goes here!
Factor P 16 8(8-5ab) 4(d² + 4) 3rs(2r – s) 15cd(1 + 2cd) 8(4a² + 3b²)
Basel-ICU-Journal Challenge18/20/ Basel-ICU-Journal Challenge8/20/2014.
1..
Lilian Blot PART III: ITERATIONS Core Elements Autumn 2012 TPOP 1.
CONTROL VISION Set-up. Step 1 Step 2 Step 3 Step 5 Step 4.
© 2012 National Heart Foundation of Australia. Slide 2.
LO: Count up to 100 objects by grouping them and counting in 5s 10s and 2s. Mrs Criddle: Westfield Middle School.
Understanding Generalist Practice, 5e, Kirst-Ashman/Hull
Model and Relationships 6 M 1 M M M M M M M M M M M M M M M M
25 seconds left…...
Container Types in Python
Januar MDMDFSSMDMDFSSS
Analyzing Genes and Genomes
We will resume in: 25 Minutes.
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
Essential Cell Biology
Clock will move after 1 minute
Intracellular Compartments and Transport
PSSA Preparation.
Essential Cell Biology
1 Chapter 13 Nuclear Magnetic Resonance Spectroscopy.
Energy Generation in Mitochondria and Chlorplasts
Select a time to count down from the clock above
Murach’s OS/390 and z/OS JCLChapter 16, Slide 1 © 2002, Mike Murach & Associates, Inc.
Python Mini-Course University of Oklahoma Department of Psychology Day 1 – Lesson 4 Beginning Functions 4/5/09 Python Mini-Course: Day 1 - Lesson 4 1.
Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 15 Tuples 5/02/09 Python Mini-Course: Day 4 – Lesson 15 1.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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