CSE 231 Lab 7.

Slides:



Advertisements
Similar presentations
Container Types in Python
Advertisements

Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 15 Tuples 5/02/09 Python Mini-Course: Day 4 – Lesson 15 1.
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.
Sequences The range function returns a sequence
Sequences A sequence is a list of elements Lists and tuples
1 Sequences A sequence is a list of elements Lists and tuples – Lists mutable – Tuples immutable Sequence elements can be indexed with subscripts – First.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
Guide to Programming with Python
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.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
Lists Victor Norman CS104. Reading Quiz Q1: What is printed by the following statements? alist = [3, 67, “cat”, [56, 57, “dog”], [ ], 3.14, False] print(3.14.
Topics: Sequence Sequences Index into a sequence [] notation Slicing and other operations.
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.
CIT 590 Intro to Programming Lecture 5 – completing lists.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
Lecture 04 – Models of memory Mutable and immutable data.
Tuples Chapter 10 Python for Informatics: Exploring Information
Collections Michael Ernst CSE 190p University of Washington.
Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 11 Using strings and sequences 5/02/09 Python Mini-Course: Day 3 – Lesson.
Guide to Programming with Python Chapter Five Lists and dictionaries (data structure); The Hangman Game.
Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.
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.
Lists Victor Norman CS104. Reading Quiz Lists Our second collection data type – elements are in order (like strings) – indexed from 0 to n – 1 (like.
Strings Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Strings CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington 1.
Python Basics 본 자료는 다음의 웹 사이트를 정리 한 내용이니 참조 바랍니다. ythonBasics.
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.
Equality, references and mutability COMPSCI 105 SS 2015 Principles of Computer Science.
Guide to Programming with Python Chapter Four Strings, and Tuples; for Loops: The Word Jumble Game.
Intro2cs Tirgul 4.
CMSC201 Computer Science I for Majors Lecture 14 – Tuples
Tuples and Lists.
Python - Lists.
CS-104 Final Exam Review Victor Norman.
Section 6: Sequences Chapter 4 and 5.
Ruth Anderson CSE 140 University of Washington
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Lists Part 1 Taken from notes by Dr. Neil Moore
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Guide to Programming with Python
CS190/295 Programming in Python for Life Sciences: Lecture 6
Data types Numeric types Sequence types float int bool list str
Python for Informatics: Exploring Information
CEV208 Computer Programming
Ruth Anderson UW CSE 160 Winter 2017
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Topics Sequences Introduction to Lists List Slicing
Lists Part 1 Taken from notes by Dr. Neil Moore
Basic String Operations
Michael Ernst CSE 140 University of Washington
Intro to Computer Science CS1510 Dr. Sarah Diesburg
By- Anshul Kumar Class: XI “B”
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Topics Basic String Operations String Slicing
CHAPTER 4: Lists, Tuples and Dictionaries
Ruth Anderson CSE 160 University of Washington
Topics Sequences Introduction to Lists List Slicing
Ruth Anderson UW CSE 160 Spring 2018
CMSC201 Computer Science I for Majors Lecture 16 – Tuples
Topics Basic String Operations String Slicing
CSE 231 Lab 6.
Course A201: Introduction to Programming
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Objects Management.
Topics Basic String Operations String Slicing
Python - Tuples.
Ruth Anderson UW CSE 160 Winter 2016
Presentation transcript:

CSE 231 Lab 7

Topics to cover Mutable List vs Immutable Tuples Shallow and deep copy operations LISTS and tuples in functions A Python list is a sequence of objects. It is a sequence like a string. It shares characteristics such as indexing & slicing, iteration (for) and membership (in) as well as functions such as len().

Mutable List vs Immutable Tuples List can be changed or modified whereas tuple can’t be changed or modified Using a tuple instead of a list can give the programmer and the interpreter a hint that the data should not be changed. List has more functionality than the tuple.

Shallow and deep copy operations Shallow copy constructs a new object and then inserts references into it to the objects found in the original. In [3]: colours2[1] = "green“ In [4]: print(colours1) In [1]: colours1 = ["red", "blue"] In [2]: colours2 = colours1 ['red', 'green']

Shallow and deep copy operations Deep copy constructs a new object and then, recursively, inserts copies into it of the objects found in the original. In [1]: import copy In [2]: lst1 = ['a','b',['ab','ba']] In [3]: lst2 = copy.deepcopy(lst1) In [4]: lst2[2][1] = "d" In [5]: lst2[0] = "c“

Do these in PythonTutor.com L1 = ['a','b','c'] L2 = [1,L1,2] print(L2) L1[1] = 'X' [1, ['a', 'b', 'c'], 2] [1, ['a', 'X', 'c'], 2] What is the output?

Do these in PythonTutor.com L3 = L2 L4 = L2[:] L1.append(100) print(L3) print(L4) What is the output? [1, ['a', 'X', 'c', 100], 2]

Do these in PythonTutor.com print(L3 is L2) print(L4 is L2) L2.append('axe') print(L3) print(L4) True False What is the output? [1, ['a', 'X', 'c', 100], 2, 'axe'] [1, ['a', 'X', 'c', 100], 2]

Do these in PythonTutor.com import copy L5 = copy.deepcopy(L2) print(L5) print(L2) What is the output? [1, ['a', 'X', 'c', 100], 2, 'axe']

Do these in PythonTutor.com L2.append(222) print(L5) print(L2) What is the output? [1, ['a', 'X', 'c', 100], 2, 'axe'] [1, ['a', 'X', 'c', 100], 2, 'axe', 222]

LISTS and tuples in functions def doubleStuff(alist): for position in range(len(alist)): alist[position] = 2 * alist[position] things = [2, 5, 9] doubleStuff(things) print(things) lst = [2, 5, 9] doubleStuff(lst[:]) print(lst) [4, 10, 18] [2, 5, 9]

LISTS and tuples in functions def doubleStuff(atuple): for position in range(len(atuple)): atuple[position] = 2 * atuple[position] things = (2, 5, 9) doubleStuff(things) print(things) TypeError: 'tuple' object does not support item assignment Tuple are immutable cannot be modified after creation

LISTS and tuples in functions def newStuff(atuple): atuple=2*atuple things = (2, 5, 9) newStuff(things) print(things) (2, 5, 9)