Lists and Tuples Intro to Computer Science CS1510 Dr. Sarah Diesburg.

Slides:



Advertisements
Similar presentations
Course A201: Introduction to Programming 10/28/2010.
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.
DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.
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.
October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 1 Introduction to Computer Programming Chapter 4: For Loops, Strings, and Tuples Michael.
Lists in Python.
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.
Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
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 CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Built-in Data Structures in Python An Introduction.
CIT 590 Intro to Programming Lecture 5 – completing lists.
10. Python - Lists The list is a most versatile datatype available in Python, which can be written as a list of comma-separated values (items) between.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
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.
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.
COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON TUPLES, SETS AND DICTIONARIES Jehan-François Pâris
Dictionaries Intro to Computer Science CS 1510 Dr. Sarah Diesburg.
Python Data Structures By Greg Felber. Lists An ordered group of items Does not need to be the same type – Could put numbers, strings or donkeys in the.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 3: Built-in 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.
More on Functions (Part 2) Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
String and Lists Dr. José M. Reyes Álamo.
Tuples and Lists.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Chapter 8 Namespaces and Memory Realities
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Strings
Introduction to Strings
Intro to Computer Science CS 1510 Dr. Sarah Diesburg
Lists – Indexing and Sorting
Bryan Burlingame 03 October 2018
Intro to Computer Science CS 1510 Dr. Sarah Diesburg
Bryan Burlingame Halloween 2018
CS190/295 Programming in Python for Life Sciences: Lecture 6
8 – Lists and tuples John R. Woodward.
Types, Truth, and Expressions (Part 2)
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
CEV208 Computer Programming
String and Lists Dr. José M. Reyes Álamo.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Lists in Python Outputting lists.
Lists – Indexing and Sorting
Introduction to Strings
Intro to Computer Science CS1510 Dr. Sarah Diesburg
15-110: Principles of Computing
Intro to Computer Science CS1510 Dr. Sarah Diesburg
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
Introduction to Strings
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Python Review
Lists – Indexing and Sorting
Copyright (c) 2017 by Dr. E. Horvath
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Types, Truth, and Expressions
CSE 231 Lab 6.
Introduction to Strings
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Lists basics A container is a construct used to group related values together and contains references to other objects instead of data. A list is a container.
Introduction to Computer Science
Presentation transcript:

Lists and Tuples Intro to Computer Science CS1510 Dr. Sarah Diesburg

Homework Assignment PA07 Regular deadline is Monday - 11/3 Remember  Follow the new design document and function commenting Example design document Example function commenting

The Python List Data Structure A list an ordered sequence of items of any type.  myList = [1, [2,1], ‘hello’]

List Indexing We can index into a list in much the same way as a string >>> myList = [1, [2,1], ‘hello’] >>> myList[2] ‘hello’ Let’s play with some indexing problems. Break up into teams of 2 or 3. 4

Example Program myList = [[‘Penny’, 90], [‘Leonard’, 85], 2] print(myList[0]) print(myList[0][0][3] + myList[1][0][2]) print((myList[0][1] + myList[1][1]) / myList[2]) #What is the output of the three print statements? 5

List Methods  myList.append(x)  myList.extend(C)  myList.pop()  myList.insert(i,x)  myList.remove(x)  myList.sort()  myList.reverse()  myList.index(x)  myList.count(x) 6 Which methods modify the list??

Sorting Only lists have a built-in sorting method. Thus you often convert your data to a list if it needs sorting: myLst = list(‘xyzabc’) myLst  [‘x’,’y’,’z’,’a’,’b’,’c’] myLst.sort() # convert back to a string sortStr = ‘’.join(myLst)  ‘abcxyz’

Sorted Function The sorted function will break a sequence into elements and sort the sequence, placing the results in a list: sortLst = sorted(‘hi mom’)  [‘ ‘,’h’,’i’,’m’,’m’,’o’]

Unusual Results myLst = [4, 7, 1, 2] myLst = myLst.sort() myLst  None # what happened? What happened was the sort operation changed the order of the list in place (right side of assignment). Then the sort method returned None, which was assigned to the variable. The list was lost and None is now the value of the variable.

Anagram Example Anagrams are words that contain the same letters in a different order. For example: ‘iceman’ and ‘cinema.’ A strategy to identify anagrams is to take the letters of a word, sort those letters, then compare the sorted sequences. Anagrams should have the same sequence.

Anagram Program Algorithm 1. Input 2 words to examine. 2. Sort the letters of each word into a new string. 3. Compare the resulting sorted strings 11

Tuples

Tuples are easy: they are simply immutable lists. They are designated with a comma:  Not the parenthesis! myTuple = (1,’a’,3.14,True) newTuple = (,) #Empty tuple anotherTuple = 2,3 #Tuple without parenthesis

The Question is, Why? The real question is, why have an immutable list, a tuple, as a separate type? An immutable list gives you a data structure with some integrity, some permanency, if you will. You know you cannot accidentally change one.

Lists and Tuples Everything that works with a list works with a tuple except methods that modify the list/tuple. Thus indexing, slicing, len, print all work as expected. However, none of the mutable methods work: append, extend, remove.

Anagram Example with Tuples Let’s see if we can modify our anagram program to use tuples 16