Intro to Computer Science CS1510 Dr. Sarah Diesburg

Slides:



Advertisements
Similar presentations
Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
Advertisements

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.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
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.
Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
CIT 590 Intro to Programming Lecture 5 – completing lists.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 8 Working.
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.
Lists CS303E: Elements of Computers and Programming.
Introduction to Strings Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg 1.
Tuples Chapter 10 Python for Informatics: Exploring Information
Tuples and Dictionaries Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Sets Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
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.
Python Programing: An Introduction to Computer Science
Dictionaries Intro to Computer Science CS 1510 Dr. Sarah Diesburg.
Strings CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington 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.
String and Lists Dr. José M. Reyes Álamo.
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 Dr. Sarah Diesburg
Chapter 8 Namespaces and Memory Realities
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Strings
Introduction to Strings
Lists – Indexing and Sorting
Lists in Python.
8 – Lists and tuples John R. Woodward.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
4. sequence data type Rocky K. C. Chang 16 September 2018
Chapter 8 Namespaces and Memory Realities
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Python for Informatics: Exploring Information
String and Lists Dr. José M. Reyes Álamo.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Topics Sequences Introduction to Lists List Slicing
Lists – Indexing and Sorting
Introduction to Strings
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CS1110 Today: collections.
15-110: Principles of Computing
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Chapter 8 Namespaces and Memory Realities
Bryan Burlingame Halloween 2018
Topics Sequences Introduction to Lists List Slicing
And now for something completely different . . .
Introduction to Strings
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Python Review
Lists – Indexing and Sorting
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
Python - Tuples.
Tuple.
Introduction to Computer Science
Presentation transcript:

Intro to Computer Science CS1510 Dr. Sarah Diesburg More with Lists Intro to Computer Science CS1510 Dr. Sarah Diesburg

The Python List Data Structure A list an ordered sequence of items of any type. A list can even contain other lists!

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

Differences Between Lists and Strings Lists can contain a mixture of any python objects 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 “”.

Indexing Can be a little confusing - what does the [ ] mean, a list or an index? [1, 2, 3][1]  2 Context solves the problem. An index always comes at the end of an expression and is preceded by something (a variable, a sequence).

List of Lists What is the second element (index 1) of that list? myLst = [‘a’, [1, 2, 3], ‘z’] What is the second element (index 1) of that list? Another list: myLst[1][0] # apply left to right myLst[1]  [1, 2, 3] [1, 2, 3][0]  1

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.

Iteration for element in [1, [1, 2], ‘a’,True]: print element Gives us

Change an Object’s Contents Strings are immutable. Once created, the object’s contents cannot be changed. New objects can be created to reflect a change, but the object itself cannot be changed: myStr = ‘abc’ myStr[0] = ‘z’ # cannot do! # instead, make new str newStr = myStr.replace(‘a’,’z’)

Lists are Mutable Unlike strings, lists are mutable. You can change the object’s contents! myLst = [1, 2, 3] myLst[0] = 127 print myLst  [127, 2, 3]

List Methods Remember, a function is a small program (such as len) that takes some arguments, the stuff in the parenthesis, and returns some value. A method is called in a special way, the “dot call”. It is called in the context of an object (or a variable holding an object).

Again, Lists have Methods myList = [‘a’,1,True] myList.append(‘z’) arguments to the method the name of the method the object that we are calling the method with

Some New Methods that Modify the List myList[0]=‘a’ Index assignment myList.append(x) Appends x to end of myList myList.extend(C) Takes a collection (like another list) and add each of the collection’s elements to the end of myList

Some New Methods that Modify the List myList.pop() Removes the element at the end of myList and returns that element. myList.insert(i,x) Inserts element x at position i into myList. myList.remove(x) Removes element x from myList

Some New Methods that Modify the List myList.sort() Sorts myList. If sorting a list of lists, only the first element in each list is considered in comparison operations. myList.reverse() Reverses the elements in myList

Some New Methods that do not Modify the List myList.index(x) Returns index value of element x in myList. myList.count(x) Returns the number of times x appears in myList.

More about List Methods Most of these methods do not return a value. This is because lists are mutable so the methods modify the list directly; there is no need to return anything.