1 CSC 221: Introduction to Programming Fall 2011 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.

Slides:



Advertisements
Similar presentations
Container Types in Python
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.
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 17 JavaScript.
1 CSC 221: Introduction to Programming Fall 2011 Text processing  Python strings  indexing & slicing, len function  functions vs. methods  string methods:
Programming Training Main Points: - Lists / Arrays in Python. - Fundamental algorithms on Arrays.
Data Structures in Python By: Christopher Todd. Lists in Python A list is a group of comma-separated values between square brackets. A list is a group.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
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.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 8 More on Strings and Special Methods 1.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
1 CSC 221: Introduction to Programming Fall 2012 Text processing  Python strings  indexing & slicing, len function  functions vs. methods  string methods:
Python for Informatics: Exploring Information
1 CSC 222: Object-Oriented Programming Spring 2012 Object-oriented design  example: word frequencies w/ parallel lists  exception handling  System.out.format.
Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Built-in Data Structures in Python An Introduction.
Lecture 19 - More on Lists, Slicing Lists, List Functions COMPSCI 101 Principles of Programming.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
A Balanced Introduction to Computer Science, 3/E David Reed, Creighton University ©2011 Pearson Prentice Hall ISBN Chapter 17 JavaScript.
Lists CS303E: Elements of Computers and Programming.
CS105 STRING LIST TUPLE DICTIONARY. Characteristics of Sequence What is sequence data type? It stores several objects Each object has an order Each object.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
1 CSC 221: Introduction to Programming Fall 2011 Input & file processing  input vs. raw_input  files: input, output  opening & closing files  read(),
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
LISTS and TUPLES. Topics Sequences Introduction to Lists List Slicing Finding Items in Lists with the in Operator List Methods and Useful Built-in Functions.
CSC 221: Computer Programming I Fall 2001  arrays  homogeneous collection of items, accessible via an index  initializing/traversing/displaying arrays.
Python Programing: An Introduction to Computer Science
Strings CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington 1.
Strings CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington 1.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
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.
CSc 110, Autumn 2016 Lecture 15: lists Adapted from slides by Marty Stepp and Stuart Reges.
String and Lists Dr. José M. Reyes Álamo.
Data types: Complex types (List)
Tuples and Lists.
Python - Lists.
Containers and Lists CIS 40 – Introduction to Programming in Python
String Processing Upsorn Praphamontripong CS 1110
CSc 120 Introduction to Computer Programing II
CMPT 120 Topic: Python strings.
Lecture 16: Lists (cont.) and File Input
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Introduction to Strings
Bryan Burlingame Halloween 2018
Creation, Traversal, Insertion and Removal
Lecture 15: lists Adapted from slides by Marty Stepp and Stuart Reges
Chapter 8 More on Strings and Special Methods
Chapter 8 More on Strings and Special Methods
Python - Strings.
8 – Lists and tuples John R. Woodward.
Lecture 22: lists Adapted from slides by Marty Stepp and Stuart Reges
4. sequence data type Rocky K. C. Chang 16 September 2018
Chapter 8 More on Strings and Special Methods
CSC 221: Introduction to Programming Fall 2018
String and Lists Dr. José M. Reyes Álamo.
Lecture 19: lists Adapted from slides by Marty Stepp and Stuart Reges
Topics Sequences Introduction to Lists List Slicing
CS 1111 Introduction to Programming Spring 2019
15-110: Principles of Computing
CSC 222: Object-Oriented Programming Spring 2013
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Computer Science
Bryan Burlingame Halloween 2018
Topics Sequences Introduction to Lists List Slicing
Chapter 17 JavaScript Arrays
Introduction to Strings
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CMPT 120 Topic: Python strings.
Introduction to Computer Science
Presentation transcript:

1 CSC 221: Introduction to Programming Fall 2011 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice stats  from strings to lists  list methods index, count, sort, reverse, append, extend, insert, remove

String review recall that a Python string is a sequence of characters basic operations on strings:  + concatenates strings  * concatenates multiple copies  len function  indexing via [i]  slicing via [s:e], [s:e:n], [:e], [s:], … string methods:  isupper, islower, isalpha, isdigit, isspace  capitalize, upper, lower  find, rfind, count, replace  strip, lstrip, rstrip  center, ljust, rjust 2

Lists closely related: a list is a sequence of arbitrary items  can specify the contents of a list in brackets, separated by commas the same sequence operations apply to lists:  + concatenates lists  * concatenates multiple copies  len function  indexing via [i]  slicing via [s:e], [s:e:n], [:e], [s:], … 3

List operation examples 4 note: range returns a list of numbers

Exercises grab a partner and go to codingbat.comcodingbat.com  solve problems (in order) from the List-1 category  all of these problems can be solved using list indexing, slicing, +, and * 5

Traversing a list sequence as with strings (character sequences), can traverse a list using for-in 6 likewise, can test for list membership using in

Example: averaging numbers suppose you have a list of numbers (e.g., grades)  want to calculate the average of those numbers 7 any potential problems?  i.e., will this function work correctly on all possible lists of numbers? modify this function so that it returns 0.0 on an empty list

Dice stats recall earlier examples involving simulated dice rolls  suppose we wanted to perform repeated rolls, keep track of the number of 7's 8

Dice stats (cont.) we could similarly define functions for the other dice totals  or, could generalize by adding a parameter for the desired total 9 do these totals add up to 10,000? should they?

Dice stats (cont.) the problem with separate calls for each dice total is that each call is independent of the others  actually simulating 10,000 x 11 different rolls  WASTEFUL, and leads to inconsistent results 10 what we need is to be able to count all the totals at the same time  could do it by having 11 variables, each keeping count for a dice total  would need an 11-case if-elif-else to check for each dice total  would then need to return or print each count

UGLY! extremely tedious what if we wanted to extend this example to 8-sided dice? what would have to change? 11

Better solution 11 separate variables are unwieldy – no way to test/update uniformly alternatively, could place all 11 counters in a single list  actually, to simplify things will create a list with 13 numbers twoCount will be stored in rollCount[2] threeCount will be stored in rollCount[3] … twelveCount will be stored in rollCount[12] 12

Nicer output simply returning the list of counts is not ideal  shows the placeholder numbers in indices 0 and 1  difficult to see which number goes with which dice total 13

Generalized version finally, could generalize to work for different dice  can even format the output to align the numbers 14

Strings & lists there is a useful string method that converts a string into a list  str.split() returns a list of the separate words in str  by default, it uses whitespace to separate the words, but can provide an optional argument to specify different separators 15

Adding user interaction since it is easier for the user to enter multiple items in a string, reading input as a string and then splitting into a list for processing is common  will the following work for averaging grades? 16

Adding user interaction (cont.) not quite – split produces an array of strings  but, average is expecting a list of numbers  could go through the list and convert each string with the corresponding number  simpler, could have average convert each element to a number when processing 17

Mystery function what does this function do? 18

Pig Latin revisited if we want to convert a phrase into Pig Latin  split the string into individual words  use the pigLatin function to convert  append the Pig Latin words back together 19 why the call to strip?

List methods similar to string, the list type has useful methods defined for it index(x) Return the index of the first occurrence of x (error if not in the list) count(x) Return the number of times x appears in the list sort() Sort the items of the list, in place reverse() Reverse the elements of the list, in place the following methods actually modify the contents of the list  unlike strings, lists are mutable objects append(x) Add x to the end of the list extend(L) Extend the list by appending all the items in L to end insert(i, x) Insert x at a given position i remove(x) Remove the first occurrence of x (error if not in the list) 20

More practice grab a partner and go back to codingbat.comcodingbat.com  solve the first 3 problems from the List-2 category  these problems can be solved using a loop to traverse and inspect the list items 21