Intro to Computer Science CS1510 Dr. Sarah Diesburg

Slides:



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

CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Container Types in Python
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.
Lists Introduction to Computing Science and Programming I.
JaySummet IPRE Python Review 2. 2 Outline Compound Data Types: Strings, Tuples, Lists & Dictionaries Immutable types: Strings Tuples Accessing.
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.
CS 100: Roadmap to Computing Fall 2014 Lecture 01.
October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 1 Introduction to Computer Programming Chapter 4: For Loops, Strings, and Tuples Michael.
Chapter 7 Lists and Tuples. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Data Structures.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Lists and Tuples Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Built-in Data Structures in Python An Introduction.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 8 Working.
Introduction to Strings Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg 1.
Tuples and Dictionaries Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
Python Programing: An Introduction to Computer Science
Dictionaries Intro to Computer Science CS 1510 Dr. Sarah Diesburg.
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.
String and Lists Dr. José M. Reyes Álamo.
Intro to CS Nov 21, 2016.
COMPSCI 107 Computer Science Fundamentals
Generating Random Numbers
Tuples and Lists.
Python - Lists.
Containers and Lists CIS 40 – Introduction to Programming in Python
CS 100: Roadmap to Computing
CMSC201 Computer Science I for Majors Lecture 12 – Lists (cont)
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
CISC101 Reminders Quiz 2 this week.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Strings
Introduction to Strings
Intro to Computer Science CS 1510 Dr. Sarah Diesburg
Bryan Burlingame 03 October 2018
Intro to Computer Science CS 1510 Dr. Sarah Diesburg
CISC101 Reminders Slides have changed from those posted last night…
CS190/295 Programming in Python for Life Sciences: Lecture 6
Data Structures – 1D Lists
4. sequence data type Rocky K. C. Chang 16 September 2018
Thinking about Strings
Intro to Computer Science CS1510 Dr. Sarah Diesburg
String and Lists Dr. José M. Reyes Álamo.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Topics Sequences Introduction to Lists List Slicing
Introduction to Strings
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CS1110 Today: collections.
15-110: Principles of Computing
Intro to Computer Science CS1510 Dr. Sarah Diesburg
15-110: Principles of Computing
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CISC101 Reminders Assignment 2 due today.
CHAPTER 4: Lists, Tuples and Dictionaries
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Topics Sequences Introduction to Lists List Slicing
Introduction to Strings
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Python Review
CS 100: Roadmap to Computing
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CSE 231 Lab 6.
Introduction to Strings
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Tuple.
Introduction to Computer Science
Presentation transcript:

Intro to Computer Science CS1510 Dr. Sarah Diesburg Introducing Lists Intro to Computer Science CS1510 Dr. Sarah Diesburg

Last Time We wrapped up using files and functions We will revisit them soon

Data Structures Data structures are particular ways of storing data to make some operations easier or more efficient. That is, they are tuned for certain tasks. Data structures are suited to solving certain problems.

Kinds of Data Structures Roughly two kinds of data structures: Built-in data structures - data structures that are so common as to be provided by default. User-defined data structures - (classes in object oriented programming) designed for a particular task.

Python Built-in Data Structures Python comes with a general set of built-in data structures: string lists tuples dictionaries sets others... Today we are going to look at lists

The Python List Data Structure A list is very simple - it is just an ordered sequence of items. You have seen such a sequence before in a string. A string is just a particular kind of list. What kind?

Example with the split method Remember how we used the split method in the last assignment? >>>myStr = “Fake,Player,CF,10000,452” >>>first,last,team,yards,td = myStr.split(“,”) The split string method returned the string split up into exactly 5 pieces

Example with the split method But what if we didn’t have exactly 5 pieces? >>>myStr = “Sarah,Diesburg,0” >>>first,last,team,yards,td = myStr.split(“,”) What will happen?

Example with the split method We need to do something more general when we want to break up a string into smaller pieces And we don’t know the exact number of pieces!

Using split to give us a list We can use the split method to give us a list of an unknown amount of pieces >>>myStr = “I like to eat cookies” >>>pieces = myStr.split(“ “) What is in pieces?

Using split to give us a list We can use the split method to give us a list of an unknown amount of pieces >>>myStr = “I like to eat cookies.” >>>pieces = myStr.split(“ “) What is in pieces? >>>pieces [‘I’, ‘like’, ‘to’, ‘eat’, ‘cookies’]

Using split to give us a list We can use the split method to give us a list of an unknown amount of pieces >>>myStr = “I like to eat cookies. Don’t judge me.” What if there’s more than one space in between the words?

Using split to give us a list We can use the split method to give us a list of an unknown amount of pieces >>>myStr = “I like to eat cookies. Don’t judge me.” >>>pieces = myStr.split() [‘I’, ‘like’, ‘to’, ‘eat’, ‘cookies.’, “Don’t”, ‘judge’ ‘me.’]

Using split to give us a list The split method gives me an ordered sequence of items! Is an iterable data structure How can I get each piece out of a list? Use a for loop!

Make a List Like all data structures, lists have a constructor, named the same as the data structure. It takes an iterable data structure and adds each item to the list. It also has a shortcut: the use of square brackets [ ] to indicate explicit items.

More List Making >>> aList = list(‘abc’) This creates the list that looks like this: [‘a’, ‘b’, ‘c’] >>>newList = [1, 3.14159, ‘a’, True] We can also create a list with this shortcut This list contains a lot of data structures of different types – what are they?

Similarities with Strings concatenate/+ (but only of lists) repeat/* indexing (the [ ] operator) slicing ([:]) membership (the in operator) len (the length operator)

Operators [1, 2, 3] + [4]  [1, 2, 3, 4] [1, 2, 3] * 2  [1, 2, 3, 1, 2, 3] 1 in [1, 2, 3]  True [1, 2, 3] < [1, 2, 4]  True Compare index to index, the first difference determines the result.

Differences Between Lists and Strings Lists can contain a mixture of any python object; strings can only hold characters. 1,”bill”,1.2345, True Lists are mutable; their values can be changed while strings are immutable. Lists are designated with [ ], with elements separated by commas; strings use “”.

Adding to lists We can use the list method append() to add on to the end of a list >>>myList = [‘I’, ‘like’] >>>myList.append(‘cookies’) >>>myList [‘I’, ‘like’, ‘cookies’]

List Functions len(lst): Number of elements in list (top level). len([1, [1, 2], 3])  3 min(lst): Minimum element in the list. If list of lists, looks at first element of each list. max(lst): Max element in the list. sum(lst): Sum the elements, numeric only.