Tuples Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Slides:



Advertisements
Similar presentations
Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Advertisements

CHAPTER 4 AND 5 Section06: Sequences. General Description "Normal" variables x = 19  The name "x" is associated with a single value Sequence variables:
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.
Slicing Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Control Flow Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Python Programming Chapter 9: Tuples Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Guide to Programming with Python
I210 review Fall 2011, IUB. Python is High-level programming –High-level versus machine language Interpreted Language –Interpreted versus compiled 2.
Images Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Copyright © 2014 Dr. James D. Palmer; This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
JaySummet IPRE Python Review 2. 2 Outline Compound Data Types: Strings, Tuples, Lists & Dictionaries Immutable types: Strings Tuples Accessing.
Lecture 23 – Python dictionaries 1 COMPSCI 101 Principles of Programming.
Python Control of Flow.
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.
“Everything Else”. Find all substrings We’ve learned how to find the first location of a string in another string with find. What about finding all matches?
Python Data Structures
Steganography Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Functions Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
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.
Lecture 21 - Tuples COMPSCI 101 Principles of Programming.
Beyond Lists: Other Data Structures CS303E: Elements of Computers and Programming.
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
 Expression Tree and Objects 1. Elements of Python  Literals, Strings, Tuples, Lists, …  The order of file reading  The order of execution 2.
Browsing Directories Copyright © Software Carpentry and The University of Edinburgh This work is licensed under the Creative Commons Attribution.
Roles of Variables with Examples in Python ® © 2014 Project Lead The Way, Inc.Computer Science and Software Engineering.
Built-in Data Structures in Python An Introduction.
Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Introduction Copyright © Software Carpentry This work is licensed under the Creative Commons Attribution License See
First-Class Functions Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
More Patterns Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Lists Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Tuples Chapter 10 Python for Informatics: Exploring Information
Python Data Structures CMSC 201. Built in Types Today we will be talking about some other built in types in python! Tuples Sets Dictionaries.
Collections Michael Ernst CSE 190p University of Washington.
Guide to Programming with Python Chapter Five Lists and dictionaries (data structure); The Hangman Game.
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
Basics Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Strings Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Dictionaries Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
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.
Python Arithmetic Operators OperatorOperationDescription +AdditionAdd values on either side of the operator -SubtractionSubtract right hand operand from.
Indentations makes the scope/block Function definition def print_message (): print “hello” Function usages print_message () hubo.move ()// hubo is a class.
Guide to Programming with Python Chapter Four Strings, and Tuples; for Loops: The Word Jumble Game.
Unit 4 – Chapter 4 Strings and Tuples. len() Function You can pass any sequence you want to the len() function and it will return the length of the sequence.
Section06: Sequences Chapter 4 and 5. General Description "Normal" variables x = 19 – The name "x" is associated with a single value Sequence variables:
CSC 108H: Introduction to Computer Programming Summer 2012 Marek Janicki.
Python Aliasing Copyright © Software Carpentry 2010
Scientific Programming in Python -- Cheat Sheet
Sets and Dictionaries Examples Copyright © Software Carpentry 2010
When to use Tuples instead of Lists
Chapter 4 Strings & Tuples
Program Design Invasion Percolation: Neighbors
Section 6: Sequences Chapter 4 and 5.
Tuples Chapter 10 Python for Everybody
Bryan Burlingame Halloween 2018
Guide to Programming with Python
CS190/295 Programming in Python for Life Sciences: Lecture 6
Python for Informatics: Exploring Information
Python I Some material adapted from Upenn cmpe391 slides and other sources.
Tuples.
Program Design Invasion Percolation: Bugs
Reference semantics, variables and names
(more) Python.
CISC101 Reminders Assignment 2 due today.
Bryan Burlingame Halloween 2018
Python Review
CSE 231 Lab 7.
Python - Tuples.
Tuple.
Presentation transcript:

Tuples Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See for more information. Python

Tuples A list is a mutable heterogeneous sequence

PythonTuples A list is a mutable heterogeneous sequence A tuple is an immutable heterogeneous sequence

PythonTuples A list is a mutable heterogeneous sequence A tuple is an immutable heterogeneous sequence I.e., a list that can't be changed after creation

PythonTuples A list is a mutable heterogeneous sequence A tuple is an immutable heterogeneous sequence I.e., a list that can't be changed after creation Why provide a less general type of collection?

PythonTuples A list is a mutable heterogeneous sequence A tuple is an immutable heterogeneous sequence I.e., a list that can't be changed after creation Why provide a less general type of collection? Full explanation will have to wait for lecture on sets and dictionaries

PythonTuples A list is a mutable heterogeneous sequence A tuple is an immutable heterogeneous sequence I.e., a list that can't be changed after creation Why provide a less general type of collection? Full explanation will have to wait for lecture on sets and dictionaries Useful even before then

PythonTuples Create tuples using () instead of []

PythonTuples Create tuples using () instead of [] Still index using [] (because everything does)

PythonTuples Create tuples using () instead of [] Still index using [] (because everything does) >>> primes = (2, 3, 5, 7) >>> print primes[0], primes[-1] 2 7 >>>

PythonTuples Create tuples using () instead of [] Still index using [] (because everything does) >>> primes = (2, 3, 5, 7) >>> print primes[0], primes[-1] 2 7 >>> empty_tuple = () >>> print len(empty_tuple) 0 >>>

PythonTuples Create tuples using () instead of [] Still index using [] (because everything does) >>> primes = (2, 3, 5, 7) >>> print primes[0], primes[-1] 2 7 >>> empty_tuple = () >>> print len(empty_tuple) 0 >>> Must use (val,) for tuple with one element

PythonTuples Create tuples using () instead of [] Still index using [] (because everything does) >>> primes = (2, 3, 5, 7) >>> print primes[0], primes[-1] 2 7 >>> empty_tuple = () >>> print len(empty_tuple) 0 >>> Must use (val,) for tuple with one element Because math says that (5) is just 5

PythonTuples Create tuples using () instead of [] Still index using [] (because everything does) >>> primes = (2, 3, 5, 7) >>> print primes[0], primes[-1] 2 7 >>> empty_tuple = () >>> print len(empty_tuple) 0 >>> Must use (val,) for tuple with one element Because math says that (5) is just 5 One of Python's few syntactic warts…

PythonTuples Don't need parentheses if context is enough

PythonTuples Don't need parentheses if context is enough >>> primes = 2, 3, 5, 7 >>> print primes (2, 3, 5, 7) >>>

PythonTuples Don't need parentheses if context is enough >>> primes = 2, 3, 5, 7 >>> print primes (2, 3, 5, 7) >>> Can use on the left of assignment

PythonTuples Don't need parentheses if context is enough >>> primes = 2, 3, 5, 7 >>> print primes (2, 3, 5, 7) >>> Can use on the left of assignment >>> left, middle, right = 2, 3, 5 >>>

PythonTuples Don't need parentheses if context is enough >>> primes = 2, 3, 5, 7 >>> print primes (2, 3, 5, 7) >>> Can use on the left of assignment >>> left, middle, right = 2, 3, 5 >>> print left 2 >>> print middle 3 >>> print right 5 >>>

PythonTuples Don't need parentheses if context is enough >>> primes = 2, 3, 5, 7 >>> print primes (2, 3, 5, 7) >>> Can use on the left of assignment >>> left, middle, right = 2, 3, 5 >>> print left 2 >>> print middle 3 >>> print right 5 >>> With great power comes great responsibility…

PythonTuples Allows functions to return multiple values

PythonTuples Allows functions to return multiple values >>> def bounds(values):... low = min(values)... high = max(values)... return (low, high)... >>>

PythonTuples Allows functions to return multiple values >>> def bounds(values):... low = min(values)... high = max(values)... return (low, high)... >>> print bounds([3, -5, 9, 4, 17, 0]) (-5, 17) >>>

PythonTuples Allows functions to return multiple values >>> def bounds(values):... low = min(values)... high = max(values)... return (low, high)... >>> print bounds([3, -5, 9, 4, 17, 0]) (-5, 17) >>> least, greatest = bounds([3, -5, 9, 4, 17, 0]) >>> print least 5 >>> print greatest 17 >>>

PythonTuples Sometimes used to return (success, result) pairs

PythonTuples Sometimes used to return (success, result) pairs def read_if_available(datafile_name): if file_exists(datafile_name):... return (True, data_values) else: return (False, [])

PythonTuples Sometimes used to return (success, result) pairs def read_if_available(datafile_name): if file_exists(datafile_name):... return (True, data_values) else: return (False, []) success, data = read_if_available('mydata.dat') if success:...

PythonTuples Sometimes used to return (success, result) pairs def read_if_available(datafile_name): if file_exists(datafile_name):... return (True, data_values) else: return (False, []) success, data = read_if_available('mydata.dat') if success:... We'll meet a better way in the lecture on testing

PythonTuples Provides a quick way to swap variables' values

PythonTuples Provides a quick way to swap variables' values >>> left, right = 0, 10 >>>

PythonTuples Provides a quick way to swap variables' values >>> left, right = 0, 10 >>> right, left = left, right >>>

PythonTuples Provides a quick way to swap variables' values >>> left, right = 0, 10 >>> right, left = left, right >>> print right 0 >>> print left 10 >>>

PythonTuples Provides a quick way to swap variables' values >>> left, right = 0, 10 >>> right, left = left, right >>> print right 0 >>> print left 10 >>> Python creates temporaries if needed

PythonTuples Provides a quick way to swap variables' values >>> left, right = 0, 10 >>> right, left = left, right >>> print right 0 >>> print left 10 >>> Python creates temporaries if needed left right 0 10

PythonTuples Provides a quick way to swap variables' values >>> left, right = 0, 10 >>> right, left = left, right >>> print right 0 >>> print left 10 >>> Python creates temporaries if needed left right _tmp_ 0 10

PythonTuples Provides a quick way to swap variables' values >>> left, right = 0, 10 >>> right, left = left, right >>> print right 0 >>> print left 10 >>> Python creates temporaries if needed left right _tmp_ 0 10

PythonTuples Provides a quick way to swap variables' values >>> left, right = 0, 10 >>> right, left = left, right >>> print right 0 >>> print left 10 >>> Python creates temporaries if needed left right _tmp_ 0 10

PythonTuples Provides a quick way to swap variables' values >>> left, right = 0, 10 >>> right, left = left, right >>> print right 0 >>> print left 10 >>> Python creates temporaries if needed left right _tmp_ 0 10

PythonTuples Provides a quick way to swap variables' values >>> left, right = 0, 10 >>> right, left = left, right >>> print right 0 >>> print left 10 >>> Python creates temporaries if needed left right 0 10

PythonTuples And an easy way to unpack a list

PythonTuples And an easy way to unpack a list >>> colors = ['yellow', 'magenta', 'lavender'] >>>

PythonTuples And an easy way to unpack a list >>> colors = ['yellow', 'magenta', 'lavender'] >>> left, middle, right = colors >>>

PythonTuples And an easy way to unpack a list >>> colors = ['yellow', 'magenta', 'lavender'] >>> left, middle, right = colors >>> print left yellow >>> print middle magenta >>> print right lavender >>>

PythonTuples And an easy way to unpack a list >>> colors = ['yellow', 'magenta', 'lavender'] >>> left, middle, right = colors >>> print left yellow >>> print middle magenta >>> print right lavender >>> Number of values must be the same

PythonTuples Often used in loops

PythonTuples Often used in loops >>> pairs = ((1, 10), (2, 20), (3, 30), (4, 40)) >>>

PythonTuples Often used in loops >>> pairs = ((1, 10), (2, 20), (3, 30), (4, 40)) >>> for p in pairs:... print p[0] + p[1]

PythonTuples Often used in loops >>> pairs = ((1, 10), (2, 20), (3, 30), (4, 40)) >>> for p in pairs:... print p[0] + p[1]

PythonTuples Often used in loops >>> pairs = ((1, 10), (2, 20), (3, 30), (4, 40)) >>> for (low, high) in pairs:... print low + high

PythonTuples Often used in loops >>> pairs = ((1, 10), (2, 20), (3, 30), (4, 40)) >>> for (low, high) in pairs:... print low + high >>>

PythonTuples The enumerate function produces (index, value) pairs

PythonTuples The enumerate function produces (index, value) pairs >>> colors = ['yellow', 'magenta', 'lavender'] >>> for (i, name) in enumerate(colors):... print i, name

PythonTuples The enumerate function produces (index, value) pairs >>> colors = ['yellow', 'magenta', 'lavender'] >>> for (i, name) in enumerate(colors):... print i, name... 0 yellow 1 magenta 2 lavender >>>

PythonTuples The enumerate function produces (index, value) pairs >>> colors = ['yellow', 'magenta', 'lavender'] >>> for (i, name) in enumerate(colors):... print i, name... 0 yellow 1 magenta 2 lavender >>> Prefer this to range(len(values))

October 2010 created by Greg Wilson Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See for more information.