Python Data Structures: Lists

Slides:



Advertisements
Similar presentations
ThinkPython Ch. 10 CS104 Students o CS104 n Prof. Norman.
Advertisements

Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Identifiers and Assignment Statements. Data structures In any programming language you need to refer to data The simplest way is with the actual data.
COMPUTER SCIENCE FEBRUARY 2011 Lists in Python. Introduction to Lists Lists (aka arrays): an ordered set of elements  A compound data type, like strings.
Lists in Python.
Handling Lists F. Duveau 16/12/11 Chapter 9.2. Objectives of the session: Tools: Everything will be done with the Python interpreter in the Terminal Learning.
9/16/2015BCHB Edwards Introduction to Python BCHB Lecture 5.
8/29/2014BCHB Edwards Introduction to Python BCHB Lecture 2.
Last Week if statement print statement input builtin function strings and methods for loop.
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
9/14/2015BCHB Edwards Introduction to Python BCHB Lecture 4.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
9/23/2015BCHB Edwards Advanced Python Data Structures BCHB Lecture 7.
You Need an Interpreter!. Closing the GAP Thus far, we’ve been struggling to speak to computers in “their” language, maybe its time we spoke to them in.
9/21/2015BCHB Edwards Python Data Structures: Lists BCHB Lecture 6.
Jim Havrilla. Invoking Python Just type “python –m script.py [arg]” or “python –c command [arg]” To exit, quit() or Control-D is used To just use the.
Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
PROGRAMMING LANGUAGE C++ lecture2 أ. منى الزهراني.
CSC 4630 Meeting 17 March 21, Exam/Quiz Schedule Due to ice, travel, research and other commitments that we all have: –Quiz 2, scheduled for Monday.
9/11/2015BCHB Edwards Introduction to Python BCHB Lecture 3.
CSC 4630 Perl 3 adapted from R. E. Beck. Problem But we worked on it first: Input: Read from a text file named in a command line argument Output: List.
Computer Science 101 Python Lists. Literals, Assignment, Comparisons, Concatenation Similar to the behavior of strings so far a = [1, 2, 3] b = range(1,
Python Files and Lists. Files  Chapter 9 actually introduces you to opening up files for reading  Chapter 14 has more on file I/O  Python can read.
Python Lists and Sequences Peter Wad Sackett. 2DTU Systems Biology, Technical University of Denmark List properties What are lists? A list is a mutable.
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.
Lists/Dictionaries. What we are covering Data structure basics Lists Dictionaries Json.
String and Lists Dr. José M. Reyes Álamo.
Introduction to Python
Introduction to Python
Advanced Python Idioms
Python unit_4 review Tue/Wed, Dec 1-2
COMPSCI 107 Computer Science Fundamentals
Introduction to Python
Topic: Python’s building blocks -> Statements
Advanced Python Data Structures
Containers and Lists CIS 40 – Introduction to Programming in Python
Introduction to Python
Advanced Python Data Structures
Introduction to Python
Introduction to Python
Lecture 10 Data Collections
Principles of Computing – UFCFA3-30-1
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Python Primer 2: Functions and Control Flow
Bryan Burlingame 03 October 2018
Lists in Python.
Creation, Traversal, Insertion and Removal
Data Structures – 1D Lists
Recitation Outline C++ STL associative containers Examples
String and Lists Dr. José M. Reyes Álamo.
Python Data Structures: Lists
Introduction to Python
Python Lists and Sequences
Introduction to Python
15-110: Principles of Computing
Advanced Python Data Structures
Introduction to Python
Advanced Python Idioms
Python Basics with Jupyter Notebook
Introduction to Python
Python Data Structures: Lists
Introduction to Python
Advanced Python Idioms
Introduction to Computer Science
COMPUTER PROGRAMMING SKILLS
And now for something completely different . . .
Class code for pythonroom.com cchsp2cs
Introduction to Computer Science
Presentation transcript:

Python Data Structures: Lists BCHB524 Lecture 6 BCHB524 - Edwards

Outline Review Homework 3 Solutions Lists Documentation BCHB524 - Edwards

Python Review Printing and execution Variables and basic data-types: integers, floats, strings Arithmetic with, conversion between String characters and chunks, string methods Functions, using/calling and defining: Use in any expression Parameters as input, return for output Control Flow: if statements – conditional execution for statements – iterative execution Command-line, sys.argv, standard input and output BCHB524 - Edwards

Python Data-structures: Lists Compound data-structure, stores any number of arbitrary data-items. Data-items can be different types Can have no items (empty) Items can be accessed by index or iteration Items can be changed Items can be added (inserted, appended) Items can be deleted BCHB524 - Edwards

Lists: Syntax and item access # Simple list of integers l = [1,2,3,4,5] print "List of ints:",l # Simple list of mixed types l = ['a',1,'b',1.5] print "Mixed list:",l # First item of the list print "First item:",l[0] # Last item of the list print "Last item:",l[-1] # Assign a (new) value to the first item... l[0] = True print "New first item:",l # Test the value of the last item... if l[-1] == 'b':     print "Last item is 'b'!" [] [1,2,3,4,5] l = ['a',1,'b',1.5] l l[0] l[-1] l[0] = True print l l[-1] == 'b' BCHB524 - Edwards

Lists: Adding items and iteration # Initialize l = [True, 'fdjasklfjal', 1, 'b', 1.5] # Insert a new value at position 1 l.insert(1,'fdjasklfjal') print "List:",l # Append a new value (to the end) l.append('100.0') print "List:",l # How many items on the list? print "Numbers of items:",len(l) # Iterate through the list, one at a time... for item in l:      print "Item:",item [] [1,2,3,4,5] l = ['a',1,'b',1.5] l l[0] l[-1] l[0] = True print l l[-1] == 'b' BCHB524 - Edwards

Lists: Slices and out-of-range access # Initialize l = [True, 'fdjasklfjal', 1, 'b', 1.5, '100.0'] print "Here is the list:",l # Select sub-lists first_three = l[0:3] print "List with first three items:",first_three last_two = l[-2:] print "List with last two items:",last_two # Assign to sub-lists l[0:3] = [1,2] print "First three items replaced with two items:",l # Out of range access print "The tenth item of the list is:",l[10] [] [1,2,3,4,5] l = ['a',1,'b',1.5] l l[0] l[-1] l[0] = True print l l[-1] == 'b' BCHB524 - Edwards

Range is just a list # Numbers starting from zero and less than 10 print range(0,10) # Numbers starting at 0 less than 10, by twos print range(0,10,2) # Numbers starting at 1 less than 5 print range(1,5) # Ten numbers starting at zero less than 10 print range(10) # Ten numbers staring from 10 downwards, greater than 0  print range(10,0,-1) # Empty list - can't omit bound  print range(10,-1) # Empty list - can't omit start print range(0,-1) [] [1,2,3,4,5] l = ['a',1,'b',1.5] l l[0] l[-1] l[0] = True print l l[-1] == 'b' BCHB524 - Edwards

Strings to Lists # Demonstrating split: string -> list s='a,b,c,d,e' print s.split(',') # Leave arguement empty for whitespace split s = 'The quick brown fox jumped over' print s.split() # One-or-more spaces count as one s = 'The quick        brown     fox' print s.split() # Convert a string to a list print list('abcdef') [] [1,2,3,4,5] l = ['a',1,'b',1.5] l l[0] l[-1] l[0] = True print l l[-1] == 'b' BCHB524 - Edwards

Lists to Strings # Initialize s='a,b,c,d,e' # String to list l = s.split(',') print l # Join with "." print '.'.join(l) # Join with a space print ' '.join(l) # Join with nothing! print ''.join(l) [] [1,2,3,4,5] l = ['a',1,'b',1.5] l l[0] l[-1] l[0] = True print l l[-1] == 'b' BCHB524 - Edwards

List iteration # Initialize and print s = 'a b c d e' l = s.split() print l # Simple and compact for c in l:     print c, print # Ugh! Don't do this! for i in range(0,len(l)):     print l[i], print # Sometimes this is most clear for c in s.split():     print c, print [] [1,2,3,4,5] l = ['a',1,'b',1.5] l l[0] l[-1] l[0] = True print l l[-1] == 'b' BCHB524 - Edwards

Handy list functions # Initialize s = 'The quick brown fox jumped over' l = s.split() # Let's print out the phrase: for word in l:     print word, print # Words sorted! for word in sorted(l):     print word, print # Words reversed! for word in reversed(l):     print word, print [] [1,2,3,4,5] l = ['a',1,'b',1.5] l l[0] l[-1] l[0] = True print l l[-1] == 'b' BCHB524 - Edwards

Python Documentation BCHB524 - Edwards

Library Reference BCHB524 - Edwards

Library Reference BCHB524 - Edwards

String & List Methods BCHB524 - Edwards

String & List Methods BCHB524 - Edwards

String & List Methods BCHB524 - Edwards

Built-in Functions BCHB524 - Edwards

Built-in Functions BCHB524 - Edwards