SlideShare a Scribd company logo
1 of 55
Download to read offline
Python 
Tuples 
Copyright © Software Carpentry 2010 
This work is licensed under the Creative Commons Attribution License 
See http://software-carpentry.org/license.html for more information.
A list is a mutable heterogeneous sequence 
Python Tuples
A list is a mutable heterogeneous sequence 
A tuple is an immutable heterogeneous sequence 
Python Tuples
A list is a mutable heterogeneous sequence 
A tuple is an immutable heterogeneous sequence 
I.e., a list that can't be changed aafftteerr ccrreeaattiioonn 
Python Tuples
A list is a mutable heterogeneous sequence 
A tuple is an immutable heterogeneous sequence 
I.e., a list that can't be changed aafftteerr ccrreeaattiioonn 
Why provide a less general type of collection? 
Python Tuples
A list is a mutable heterogeneous sequence 
A tuple is an immutable heterogeneous sequence 
I.e., a list that can't be changed aafftteerr ccrreeaattiioonn 
Why provide a less general type of collection? 
Full explanation will have to wait for lecture on 
sets and dictionaries 
Python Tuples
A list is a mutable heterogeneous sequence 
A tuple is an immutable heterogeneous sequence 
I.e., a list that can't be changed aafftteerr ccrreeaattiioonn 
Why provide a less general type of collection? 
Full explanation will have to wait for lecture on 
sets and dictionaries 
Useful even before then 
Python Tuples
Create tuples using () instead of [] 
Python Tuples
Create tuples using () instead of [] 
Still index using [] (because everything does) 
Python Tuples
Create tuples using () instead of [] 
Still index using [] (because everything does) 
>>>>>>>>>>>>>>>>>>>>>>>> pprriimmeess == ((22,, 33,, 55,, 77)) 
>>>>>>>>>>>> print primes[0], primes[-1] 
2 7 
>>>>>>>>>>>> 
Python Tuples
Create tuples using () instead of [] 
Still index using [] (because everything does) 
>>>>>>>>>>>>>>>>>>>>>>>> pprriimmeess == ((22,, 33,, 55,, 77)) 
>>>>>>>>>>>> print primes[0], primes[-1] 
2 7 
>>>>>>>>>>>> empty_tuple = () 
>>>>>>>>>>>> print len(empty_tuple) 
0 
>>>>>>>>>>>> 
Python Tuples
Create tuples using () instead of [] 
Still index using [] (because everything does) 
>>>>>>>>>>>>>>>>>>>>>>>> pprriimmeess == ((22,, 33,, 55,, 77)) 
>>>>>>>>>>>> print primes[0], primes[-1] 
2 7 
>>>>>>>>>>>> empty_tuple = () 
>>>>>>>>>>>> print len(empty_tuple) 
0 
>>>>>>>>>>>> 
Must use (val,) for tuple with one element 
Python Tuples
Create tuples using () instead of [] 
Still index using [] (because everything does) 
>>>>>>>>>>>>>>>>>>>>>>>> pprriimmeess == ((22,, 33,, 55,, 77)) 
>>>>>>>>>>>> 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 
Python Tuples
Create tuples using () instead of [] 
Still index using [] (because everything does) 
>>>>>>>>>>>>>>>>>>>>>>>> pprriimmeess == ((22,, 33,, 55,, 77)) 
>>>>>>>>>>>> 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… 
Python Tuples
Don't need parentheses if context is enough 
Python Tuples
Don't need parentheses if context is enough 
>>>>>>>>>>>> primes = 2, 3, 5, 7 
>>>>>>>>>>>> print primes 
(2, 3, 5, 7) 
>>>>>>>>>>>> 
Python Tuples
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 
Python Tuples
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 
>>>>>>>>>>>> 
Python Tuples
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 
>>>>>>>>>>>> 
Python Tuples
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… 
Python Tuples
Allows functions to return multiple values 
Python Tuples
Allows functions to return multiple values 
>>>>>>>>>>>> def bounds(values): 
............ low = min(values) 
........................ hhiigghh == mmaaxx((vvaalluueess)) 
............ return (low, high) 
............ 
>>>>>>>>>>>> 
Python Tuples
Allows functions to return multiple values 
>>>>>>>>>>>> def bounds(values): 
............ low = min(values) 
........................ hhiigghh == mmaaxx((vvaalluueess)) 
............ return (low, high) 
............ 
>>>>>>>>>>>> print bounds([3, -5, 9, 4, 17, 0]) 
(-5, 17) 
>>>>>>>>>>>> 
Python Tuples
Allows functions to return multiple values 
>>>>>>>>>>>> def bounds(values): 
............ low = min(values) 
........................ hhiigghh == mmaaxx((vvaalluueess)) 
............ return (low, high) 
............ 
>>>>>>>>>>>> print bounds([3, -5, 9, 4, 17, 0]) 
(-5, 17) 
>>>>>>>>>>>> least, greatest = bounds([3, -5, 9, 4, 17, 0]) 
>>>>>>>>>>>> pppprrrriiiinnnntttt least 
5 
>>>>>>>>>>>> pppprrrriiiinnnntttt greatest 
17 
>>> 
Python Tuples
Sometimes used to return (success, result) pairs 
Python Tuples
Sometimes used to return (success, result) pairs 
ddddeeeeffff read_if_available(datafile_name): 
iiiiffff file_exists(datafile_name): 
... 
rrrreeeettttuuuurrrrnnnn (TTTTrrrruuuueeee, data_values) 
eeeellllsssseeee: 
rrrreeeettttuuuurrrrnnnn (FFFFaaaallllsssseeee, []) 
Python Tuples
Sometimes used to return (success, result) pairs 
ddddeeeeffff read_if_available(datafile_name): 
iiiiffff file_exists(datafile_name): 
... 
rrrreeeettttuuuurrrrnnnn (TTTTrrrruuuueeee, data_values) 
eeeellllsssseeee: 
rrrreeeettttuuuurrrrnnnn (FFFFaaaallllsssseeee, []) 
success, data = read_if_available('mydata.dat') 
iiiiffff success: 
... 
Python Tuples
Sometimes used to return (success, result) pairs 
ddddeeeeffff read_if_available(datafile_name): 
iiiiffff file_exists(datafile_name): 
... 
rrrreeeettttuuuurrrrnnnn (TTTTrrrruuuueeee, data_values) 
eeeellllsssseeee: 
rrrreeeettttuuuurrrrnnnn (FFFFaaaallllsssseeee, []) 
success, data = read_if_available('mydata.dat') 
iiiiffff success: 
... 
We'll meet a better way in the lecture on testing 
Python Tuples
Provides a quick way to swap variables' values 
Python Tuples
Provides a quick way to swap variables' values 
>>>>>>>>>>>> left, right = 0, 10 
>>>>>>>>>>>> 
Python Tuples
Provides a quick way to swap variables' values 
>>>>>>>>>>>> left, right = 0, 10 
>>>>>>>>>>>> right, left = left, right 
>>>>>>>>>>>>>>>>>>>>>>>> 
Python Tuples
Provides a quick way to swap variables' values 
>>>>>>>>>>>> left, right = 0, 10 
>>>>>>>>>>>> right, left = left, right 
>>>>>>>>>>>>>>>>>>>>>>>> pprriinntt rriigghhtt 
0 
>>>>>>>>>>>> print left 
10 
>>>>>>>>>>>> 
Python Tuples
Provides a quick way to swap variables' values 
>>>>>>>>>>>> left, right = 0, 10 
>>>>>>>>>>>> right, left = left, right 
>>>>>>>>>>>>>>>>>>>>>>>> pprriinntt rriigghhtt 
0 
>>>>>>>>>>>> print left 
10 
>>>>>>>>>>>> 
Python creates temporaries if needed 
Python Tuples
Provides a quick way to swap variables' values 
>>>>>>>>>>>> left, right = 0, 10 
>>>>>>>>>>>> right, left = left, right 
>>>>>>>>>>>>>>>>>>>>>>>> pprriinntt rriigghhtt 
0 
>>>>>>>>>>>> print left 
10 
>>>>>>>>>>>> 
Python creates temporaries if needed 
left 
right 
0 
10 
Python Tuples
Provides a quick way to swap variables' values 
>>>>>>>>>>>> left, right = 0, 10 
>>>>>>>>>>>> right, left = left, right 
>>>>>>>>>>>>>>>>>>>>>>>> pprriinntt rriigghhtt 
0 
>>>>>>>>>>>> print left 
10 
>>>>>>>>>>>> 
Python creates temporaries if needed 
left 
right 
_tmp_ 
0 
10 
Python Tuples
Provides a quick way to swap variables' values 
>>>>>>>>>>>> left, right = 0, 10 
>>>>>>>>>>>> right, left = left, right 
>>>>>>>>>>>>>>>>>>>>>>>> pprriinntt rriigghhtt 
0 
>>>>>>>>>>>> print left 
10 
>>>>>>>>>>>> 
Python creates temporaries if needed 
left 
right 
_tmp_ 
0 
10 
Python Tuples
Provides a quick way to swap variables' values 
>>>>>>>>>>>> left, right = 0, 10 
>>>>>>>>>>>> right, left = left, right 
>>>>>>>>>>>>>>>>>>>>>>>> pprriinntt rriigghhtt 
0 
>>>>>>>>>>>> print left 
10 
>>>>>>>>>>>> 
Python creates temporaries if needed 
left 
right 
_tmp_ 
0 
10 
Python Tuples
Provides a quick way to swap variables' values 
>>>>>>>>>>>> left, right = 0, 10 
>>>>>>>>>>>> right, left = left, right 
>>>>>>>>>>>>>>>>>>>>>>>> pprriinntt rriigghhtt 
0 
>>>>>>>>>>>> print left 
10 
>>>>>>>>>>>> 
Python creates temporaries if needed 
left 
right 
_tmp_ 
0 
10 
Python Tuples
Provides a quick way to swap variables' values 
>>>>>>>>>>>> left, right = 0, 10 
>>>>>>>>>>>> right, left = left, right 
>>>>>>>>>>>>>>>>>>>>>>>> pprriinntt rriigghhtt 
0 
>>>>>>>>>>>> print left 
10 
>>>>>>>>>>>> 
Python creates temporaries if needed 
left 
right 
0 
10 
Python Tuples
And an easy way to unpack a list 
Python Tuples
And an easy way to unpack a list 
>>>>>>>>>>>> colors = ['yellow', 'magenta', 'lavender'] 
>>>>>>>>>>>> 
Python Tuples
And an easy way to unpack a list 
>>>>>>>>>>>> colors = ['yellow', 'magenta', 'lavender'] 
>>>>>>>>>>>> left, middle, right = colors 
>>>>>>>>>>>>>>>>>>>>>>>> 
Python Tuples
And an easy way to unpack a list 
>>>>>>>>>>>> colors = ['yellow', 'magenta', 'lavender'] 
>>>>>>>>>>>> left, middle, right = colors 
>>>>>>>>>>>>>>>>>>>>>>>> pprriinntt lleefftt 
yellow 
>>>>>>>>>>>> print middle 
magenta 
>>>>>>>>>>>> print right 
lavender 
>>>>>>>>>>>> 
Python Tuples
And an easy way to unpack a list 
>>>>>>>>>>>> colors = ['yellow', 'magenta', 'lavender'] 
>>>>>>>>>>>> left, middle, right = colors 
>>>>>>>>>>>>>>>>>>>>>>>> pprriinntt lleefftt 
yellow 
>>>>>>>>>>>> print middle 
magenta 
>>>>>>>>>>>> print right 
lavender 
>>>>>>>>>>>> 
Number of values must be the same 
Python Tuples
Often used in loops 
Python Tuples
Often used in loops 
>>>>>>>>>>>> pairs = ((1, 10), (2, 20), (3, 30), (4, 40)) 
>>>>>>>>>>>> 
Python Tuples
Often used in loops 
>>>>>>>>>>>> pairs = ((1, 10), (2, 20), (3, 30), (4, 40)) 
>>>>>>>>>>>> for p in pairs: 
........................ pprriinntt pp[[00]] ++ pp[[11]] 
Python Tuples
Often used in loops 
>>>>>>>>>>>> pairs = ((1, 10), (2, 20), (3, 30), (4, 40)) 
>>>>>>>>>>>> for p in pairs: 
........................ pprriinntt pp[[00]] ++ pp[[11]] 
Python Tuples
Often used in loops 
>>>>>>>>>>>> pairs = ((1, 10), (2, 20), (3, 30), (4, 40)) 
>>>>>>>>>>>> for (low, high) in pairs: 
........................ pprriinntt llooww ++ hhiigghh 
Python Tuples
Often used in loops 
>>>>>>>>>>>> pairs = ((1, 10), (2, 20), (3, 30), (4, 40)) 
>>>>>>>>>>>> for (low, high) in pairs: 
........................ pprriinntt llooww ++ hhiigghh 
............ 
11 
22 
33 
44 
>>>>>>>>>>>> 
Python Tuples
The enumerate function produces (index, value) pairs 
Python Tuples
The enumerate function produces (index, value) pairs 
>>>>>>>>>>>> colors = ['yellow', 'magenta', 'lavender'] 
>>>>>>>>>>>> for (i, name) in enumerate(colors): 
........................ pprriinntt ii,, nnaammee 
Python Tuples
The enumerate function produces (index, value) pairs 
>>>>>>>>>>>> colors = ['yellow', 'magenta', 'lavender'] 
>>>>>>>>>>>> for (i, name) in enumerate(colors): 
........................ pprriinntt ii,, nnaammee 
............ 
0 yellow 
1 magenta 
2 lavender 
>>>>>>>>>>>> 
Python Tuples
The enumerate function produces (index, value) pairs 
>>>>>>>>>>>> colors = ['yellow', 'magenta', 'lavender'] 
>>>>>>>>>>>> for (i, name) in enumerate(colors): 
........................ pprriinntt ii,, nnaammee 
............ 
0 yellow 
1 magenta 
2 lavender 
>>>>>>>>>>>> 
Prefer this to range(len(values)) 
Python Tuples
created by 
Greg Wilson 
October 2010 
Copyright © Software Carpentry 2010 
This work is licensed under the Creative Commons Attribution License 
See http://software-carpentry.org/license.html for more information.

More Related Content

What's hot

Programming Under Linux In Python
Programming Under Linux In PythonProgramming Under Linux In Python
Programming Under Linux In PythonMarwan Osman
 
Python for Linux System Administration
Python for Linux System AdministrationPython for Linux System Administration
Python for Linux System Administrationvceder
 
Build a compiler in 2hrs - NCrafts Paris 2015
Build a compiler in 2hrs -  NCrafts Paris 2015Build a compiler in 2hrs -  NCrafts Paris 2015
Build a compiler in 2hrs - NCrafts Paris 2015Phillip Trelford
 
Argparse: Python command line parser
Argparse: Python command line parserArgparse: Python command line parser
Argparse: Python command line parserTimo Stollenwerk
 
Python - An Introduction
Python - An IntroductionPython - An Introduction
Python - An IntroductionEueung Mulyana
 
Commit ускоривший python 2.7.11 на 30% и новое в python 3.5
Commit ускоривший python 2.7.11 на 30% и новое в python 3.5Commit ускоривший python 2.7.11 на 30% и новое в python 3.5
Commit ускоривший python 2.7.11 на 30% и новое в python 3.5PyNSK
 
An introduction to Python for absolute beginners
An introduction to Python for absolute beginnersAn introduction to Python for absolute beginners
An introduction to Python for absolute beginnersKálmán "KAMI" Szalai
 
PyCon 2013 : Scripting to PyPi to GitHub and More
PyCon 2013 : Scripting to PyPi to GitHub and MorePyCon 2013 : Scripting to PyPi to GitHub and More
PyCon 2013 : Scripting to PyPi to GitHub and MoreMatt Harrison
 
Why Python (for Statisticians)
Why Python (for Statisticians)Why Python (for Statisticians)
Why Python (for Statisticians)Matt Harrison
 
Fizz and buzz of computer programs in python.
Fizz and buzz of computer programs in python.Fizz and buzz of computer programs in python.
Fizz and buzz of computer programs in python.Esehara Shigeo
 
Python于Web 2.0网站的应用 - QCon Beijing 2010
Python于Web 2.0网站的应用 - QCon Beijing 2010Python于Web 2.0网站的应用 - QCon Beijing 2010
Python于Web 2.0网站的应用 - QCon Beijing 2010Qiangning Hong
 
Python for-unix-and-linux-system-administration
Python for-unix-and-linux-system-administrationPython for-unix-and-linux-system-administration
Python for-unix-and-linux-system-administrationVictor Marcelino
 
Py4inf 05-iterations (1)
Py4inf 05-iterations (1)Py4inf 05-iterations (1)
Py4inf 05-iterations (1)karan saini
 
Py4inf 05-iterations
Py4inf 05-iterationsPy4inf 05-iterations
Py4inf 05-iterationskaran saini
 
Reversing the dropbox client on windows
Reversing the dropbox client on windowsReversing the dropbox client on windows
Reversing the dropbox client on windowsextremecoders
 
Lesson1 python an introduction
Lesson1 python an introductionLesson1 python an introduction
Lesson1 python an introductionArulalan T
 

What's hot (20)

Programming Under Linux In Python
Programming Under Linux In PythonProgramming Under Linux In Python
Programming Under Linux In Python
 
Python for Linux System Administration
Python for Linux System AdministrationPython for Linux System Administration
Python for Linux System Administration
 
Build a compiler in 2hrs - NCrafts Paris 2015
Build a compiler in 2hrs -  NCrafts Paris 2015Build a compiler in 2hrs -  NCrafts Paris 2015
Build a compiler in 2hrs - NCrafts Paris 2015
 
Argparse: Python command line parser
Argparse: Python command line parserArgparse: Python command line parser
Argparse: Python command line parser
 
Python
PythonPython
Python
 
Python - An Introduction
Python - An IntroductionPython - An Introduction
Python - An Introduction
 
Commit ускоривший python 2.7.11 на 30% и новое в python 3.5
Commit ускоривший python 2.7.11 на 30% и новое в python 3.5Commit ускоривший python 2.7.11 на 30% и новое в python 3.5
Commit ускоривший python 2.7.11 на 30% и новое в python 3.5
 
An introduction to Python for absolute beginners
An introduction to Python for absolute beginnersAn introduction to Python for absolute beginners
An introduction to Python for absolute beginners
 
PyCon 2013 : Scripting to PyPi to GitHub and More
PyCon 2013 : Scripting to PyPi to GitHub and MorePyCon 2013 : Scripting to PyPi to GitHub and More
PyCon 2013 : Scripting to PyPi to GitHub and More
 
Why Python (for Statisticians)
Why Python (for Statisticians)Why Python (for Statisticians)
Why Python (for Statisticians)
 
Fizz and buzz of computer programs in python.
Fizz and buzz of computer programs in python.Fizz and buzz of computer programs in python.
Fizz and buzz of computer programs in python.
 
Python于Web 2.0网站的应用 - QCon Beijing 2010
Python于Web 2.0网站的应用 - QCon Beijing 2010Python于Web 2.0网站的应用 - QCon Beijing 2010
Python于Web 2.0网站的应用 - QCon Beijing 2010
 
RabbitMQ
RabbitMQRabbitMQ
RabbitMQ
 
Python Workshop
Python  Workshop Python  Workshop
Python Workshop
 
Python Basic
Python BasicPython Basic
Python Basic
 
Python for-unix-and-linux-system-administration
Python for-unix-and-linux-system-administrationPython for-unix-and-linux-system-administration
Python for-unix-and-linux-system-administration
 
Py4inf 05-iterations (1)
Py4inf 05-iterations (1)Py4inf 05-iterations (1)
Py4inf 05-iterations (1)
 
Py4inf 05-iterations
Py4inf 05-iterationsPy4inf 05-iterations
Py4inf 05-iterations
 
Reversing the dropbox client on windows
Reversing the dropbox client on windowsReversing the dropbox client on windows
Reversing the dropbox client on windows
 
Lesson1 python an introduction
Lesson1 python an introductionLesson1 python an introduction
Lesson1 python an introduction
 

Similar to Tuples

Python 101: Python for Absolute Beginners (PyTexas 2014)
Python 101: Python for Absolute Beginners (PyTexas 2014)Python 101: Python for Absolute Beginners (PyTexas 2014)
Python 101: Python for Absolute Beginners (PyTexas 2014)Paige Bailey
 
Lecture-2-Python-Basic-Elements-Sep04-2018.pptx
Lecture-2-Python-Basic-Elements-Sep04-2018.pptxLecture-2-Python-Basic-Elements-Sep04-2018.pptx
Lecture-2-Python-Basic-Elements-Sep04-2018.pptxAbdulQadeerBilal
 
Python 3.3 チラ見
Python 3.3 チラ見Python 3.3 チラ見
Python 3.3 チラ見Toru Furukawa
 
Introduction to Python for Bioinformatics
Introduction to Python for BioinformaticsIntroduction to Python for Bioinformatics
Introduction to Python for BioinformaticsJosé Héctor Gálvez
 
Τα Πολύ Βασικά για την Python
Τα Πολύ Βασικά για την PythonΤα Πολύ Βασικά για την Python
Τα Πολύ Βασικά για την PythonMoses Boudourides
 
The Awesome Python Class Part-3
The Awesome Python Class Part-3The Awesome Python Class Part-3
The Awesome Python Class Part-3Binay Kumar Ray
 
python-cheatsheets.pdf
python-cheatsheets.pdfpython-cheatsheets.pdf
python-cheatsheets.pdfKalyan969491
 
python-cheatsheets that will be for coders
python-cheatsheets that will be for coderspython-cheatsheets that will be for coders
python-cheatsheets that will be for coderssarafbisesh
 
Porque aprender haskell me fez um programador python melhor?
Porque aprender haskell me fez um programador python melhor?Porque aprender haskell me fez um programador python melhor?
Porque aprender haskell me fez um programador python melhor?UFPA
 
Python-The programming Language
Python-The programming LanguagePython-The programming Language
Python-The programming LanguageRohan Gupta
 
Computer 10 Quarter 3 Lesson .ppt
Computer 10 Quarter 3 Lesson .pptComputer 10 Quarter 3 Lesson .ppt
Computer 10 Quarter 3 Lesson .pptRedenOriola
 
Learn 90% of Python in 90 Minutes
Learn 90% of Python in 90 MinutesLearn 90% of Python in 90 Minutes
Learn 90% of Python in 90 MinutesMatt Harrison
 
Python language data types
Python language data typesPython language data types
Python language data typesHarry Potter
 
Python language data types
Python language data typesPython language data types
Python language data typesHoang Nguyen
 
Python language data types
Python language data typesPython language data types
Python language data typesFraboni Ec
 

Similar to Tuples (20)

Python 101: Python for Absolute Beginners (PyTexas 2014)
Python 101: Python for Absolute Beginners (PyTexas 2014)Python 101: Python for Absolute Beginners (PyTexas 2014)
Python 101: Python for Absolute Beginners (PyTexas 2014)
 
Lecture-2-Python-Basic-Elements-Sep04-2018.pptx
Lecture-2-Python-Basic-Elements-Sep04-2018.pptxLecture-2-Python-Basic-Elements-Sep04-2018.pptx
Lecture-2-Python-Basic-Elements-Sep04-2018.pptx
 
Python 3.3 チラ見
Python 3.3 チラ見Python 3.3 チラ見
Python 3.3 チラ見
 
Introduction to Python for Bioinformatics
Introduction to Python for BioinformaticsIntroduction to Python for Bioinformatics
Introduction to Python for Bioinformatics
 
Τα Πολύ Βασικά για την Python
Τα Πολύ Βασικά για την PythonΤα Πολύ Βασικά για την Python
Τα Πολύ Βασικά για την Python
 
Python String Revisited.pptx
Python String Revisited.pptxPython String Revisited.pptx
Python String Revisited.pptx
 
Course set three full notes
Course set three full notesCourse set three full notes
Course set three full notes
 
Baabtra.com little coder chapter - 4
Baabtra.com little coder   chapter - 4Baabtra.com little coder   chapter - 4
Baabtra.com little coder chapter - 4
 
The Awesome Python Class Part-3
The Awesome Python Class Part-3The Awesome Python Class Part-3
The Awesome Python Class Part-3
 
python-cheatsheets.pdf
python-cheatsheets.pdfpython-cheatsheets.pdf
python-cheatsheets.pdf
 
python-cheatsheets that will be for coders
python-cheatsheets that will be for coderspython-cheatsheets that will be for coders
python-cheatsheets that will be for coders
 
Porque aprender haskell me fez um programador python melhor?
Porque aprender haskell me fez um programador python melhor?Porque aprender haskell me fez um programador python melhor?
Porque aprender haskell me fez um programador python melhor?
 
2 × 3 = 6
2 × 3 = 62 × 3 = 6
2 × 3 = 6
 
Python-The programming Language
Python-The programming LanguagePython-The programming Language
Python-The programming Language
 
Computer 10 Quarter 3 Lesson .ppt
Computer 10 Quarter 3 Lesson .pptComputer 10 Quarter 3 Lesson .ppt
Computer 10 Quarter 3 Lesson .ppt
 
Learn 90% of Python in 90 Minutes
Learn 90% of Python in 90 MinutesLearn 90% of Python in 90 Minutes
Learn 90% of Python in 90 Minutes
 
Python course
Python coursePython course
Python course
 
Python language data types
Python language data typesPython language data types
Python language data types
 
Python language data types
Python language data typesPython language data types
Python language data types
 
Python language data types
Python language data typesPython language data types
Python language data types
 

Recently uploaded

Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfngoud9212
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsAndrey Dotsenko
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 

Recently uploaded (20)

Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdf
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 

Tuples

  • 1. Python Tuples Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information.
  • 2. A list is a mutable heterogeneous sequence Python Tuples
  • 3. A list is a mutable heterogeneous sequence A tuple is an immutable heterogeneous sequence Python Tuples
  • 4. A list is a mutable heterogeneous sequence A tuple is an immutable heterogeneous sequence I.e., a list that can't be changed aafftteerr ccrreeaattiioonn Python Tuples
  • 5. A list is a mutable heterogeneous sequence A tuple is an immutable heterogeneous sequence I.e., a list that can't be changed aafftteerr ccrreeaattiioonn Why provide a less general type of collection? Python Tuples
  • 6. A list is a mutable heterogeneous sequence A tuple is an immutable heterogeneous sequence I.e., a list that can't be changed aafftteerr ccrreeaattiioonn Why provide a less general type of collection? Full explanation will have to wait for lecture on sets and dictionaries Python Tuples
  • 7. A list is a mutable heterogeneous sequence A tuple is an immutable heterogeneous sequence I.e., a list that can't be changed aafftteerr ccrreeaattiioonn Why provide a less general type of collection? Full explanation will have to wait for lecture on sets and dictionaries Useful even before then Python Tuples
  • 8. Create tuples using () instead of [] Python Tuples
  • 9. Create tuples using () instead of [] Still index using [] (because everything does) Python Tuples
  • 10. Create tuples using () instead of [] Still index using [] (because everything does) >>>>>>>>>>>>>>>>>>>>>>>> pprriimmeess == ((22,, 33,, 55,, 77)) >>>>>>>>>>>> print primes[0], primes[-1] 2 7 >>>>>>>>>>>> Python Tuples
  • 11. Create tuples using () instead of [] Still index using [] (because everything does) >>>>>>>>>>>>>>>>>>>>>>>> pprriimmeess == ((22,, 33,, 55,, 77)) >>>>>>>>>>>> print primes[0], primes[-1] 2 7 >>>>>>>>>>>> empty_tuple = () >>>>>>>>>>>> print len(empty_tuple) 0 >>>>>>>>>>>> Python Tuples
  • 12. Create tuples using () instead of [] Still index using [] (because everything does) >>>>>>>>>>>>>>>>>>>>>>>> pprriimmeess == ((22,, 33,, 55,, 77)) >>>>>>>>>>>> print primes[0], primes[-1] 2 7 >>>>>>>>>>>> empty_tuple = () >>>>>>>>>>>> print len(empty_tuple) 0 >>>>>>>>>>>> Must use (val,) for tuple with one element Python Tuples
  • 13. Create tuples using () instead of [] Still index using [] (because everything does) >>>>>>>>>>>>>>>>>>>>>>>> pprriimmeess == ((22,, 33,, 55,, 77)) >>>>>>>>>>>> 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 Python Tuples
  • 14. Create tuples using () instead of [] Still index using [] (because everything does) >>>>>>>>>>>>>>>>>>>>>>>> pprriimmeess == ((22,, 33,, 55,, 77)) >>>>>>>>>>>> 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… Python Tuples
  • 15. Don't need parentheses if context is enough Python Tuples
  • 16. Don't need parentheses if context is enough >>>>>>>>>>>> primes = 2, 3, 5, 7 >>>>>>>>>>>> print primes (2, 3, 5, 7) >>>>>>>>>>>> Python Tuples
  • 17. 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 Python Tuples
  • 18. 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 >>>>>>>>>>>> Python Tuples
  • 19. 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 >>>>>>>>>>>> Python Tuples
  • 20. 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… Python Tuples
  • 21. Allows functions to return multiple values Python Tuples
  • 22. Allows functions to return multiple values >>>>>>>>>>>> def bounds(values): ............ low = min(values) ........................ hhiigghh == mmaaxx((vvaalluueess)) ............ return (low, high) ............ >>>>>>>>>>>> Python Tuples
  • 23. Allows functions to return multiple values >>>>>>>>>>>> def bounds(values): ............ low = min(values) ........................ hhiigghh == mmaaxx((vvaalluueess)) ............ return (low, high) ............ >>>>>>>>>>>> print bounds([3, -5, 9, 4, 17, 0]) (-5, 17) >>>>>>>>>>>> Python Tuples
  • 24. Allows functions to return multiple values >>>>>>>>>>>> def bounds(values): ............ low = min(values) ........................ hhiigghh == mmaaxx((vvaalluueess)) ............ return (low, high) ............ >>>>>>>>>>>> print bounds([3, -5, 9, 4, 17, 0]) (-5, 17) >>>>>>>>>>>> least, greatest = bounds([3, -5, 9, 4, 17, 0]) >>>>>>>>>>>> pppprrrriiiinnnntttt least 5 >>>>>>>>>>>> pppprrrriiiinnnntttt greatest 17 >>> Python Tuples
  • 25. Sometimes used to return (success, result) pairs Python Tuples
  • 26. Sometimes used to return (success, result) pairs ddddeeeeffff read_if_available(datafile_name): iiiiffff file_exists(datafile_name): ... rrrreeeettttuuuurrrrnnnn (TTTTrrrruuuueeee, data_values) eeeellllsssseeee: rrrreeeettttuuuurrrrnnnn (FFFFaaaallllsssseeee, []) Python Tuples
  • 27. Sometimes used to return (success, result) pairs ddddeeeeffff read_if_available(datafile_name): iiiiffff file_exists(datafile_name): ... rrrreeeettttuuuurrrrnnnn (TTTTrrrruuuueeee, data_values) eeeellllsssseeee: rrrreeeettttuuuurrrrnnnn (FFFFaaaallllsssseeee, []) success, data = read_if_available('mydata.dat') iiiiffff success: ... Python Tuples
  • 28. Sometimes used to return (success, result) pairs ddddeeeeffff read_if_available(datafile_name): iiiiffff file_exists(datafile_name): ... rrrreeeettttuuuurrrrnnnn (TTTTrrrruuuueeee, data_values) eeeellllsssseeee: rrrreeeettttuuuurrrrnnnn (FFFFaaaallllsssseeee, []) success, data = read_if_available('mydata.dat') iiiiffff success: ... We'll meet a better way in the lecture on testing Python Tuples
  • 29. Provides a quick way to swap variables' values Python Tuples
  • 30. Provides a quick way to swap variables' values >>>>>>>>>>>> left, right = 0, 10 >>>>>>>>>>>> Python Tuples
  • 31. Provides a quick way to swap variables' values >>>>>>>>>>>> left, right = 0, 10 >>>>>>>>>>>> right, left = left, right >>>>>>>>>>>>>>>>>>>>>>>> Python Tuples
  • 32. Provides a quick way to swap variables' values >>>>>>>>>>>> left, right = 0, 10 >>>>>>>>>>>> right, left = left, right >>>>>>>>>>>>>>>>>>>>>>>> pprriinntt rriigghhtt 0 >>>>>>>>>>>> print left 10 >>>>>>>>>>>> Python Tuples
  • 33. Provides a quick way to swap variables' values >>>>>>>>>>>> left, right = 0, 10 >>>>>>>>>>>> right, left = left, right >>>>>>>>>>>>>>>>>>>>>>>> pprriinntt rriigghhtt 0 >>>>>>>>>>>> print left 10 >>>>>>>>>>>> Python creates temporaries if needed Python Tuples
  • 34. Provides a quick way to swap variables' values >>>>>>>>>>>> left, right = 0, 10 >>>>>>>>>>>> right, left = left, right >>>>>>>>>>>>>>>>>>>>>>>> pprriinntt rriigghhtt 0 >>>>>>>>>>>> print left 10 >>>>>>>>>>>> Python creates temporaries if needed left right 0 10 Python Tuples
  • 35. Provides a quick way to swap variables' values >>>>>>>>>>>> left, right = 0, 10 >>>>>>>>>>>> right, left = left, right >>>>>>>>>>>>>>>>>>>>>>>> pprriinntt rriigghhtt 0 >>>>>>>>>>>> print left 10 >>>>>>>>>>>> Python creates temporaries if needed left right _tmp_ 0 10 Python Tuples
  • 36. Provides a quick way to swap variables' values >>>>>>>>>>>> left, right = 0, 10 >>>>>>>>>>>> right, left = left, right >>>>>>>>>>>>>>>>>>>>>>>> pprriinntt rriigghhtt 0 >>>>>>>>>>>> print left 10 >>>>>>>>>>>> Python creates temporaries if needed left right _tmp_ 0 10 Python Tuples
  • 37. Provides a quick way to swap variables' values >>>>>>>>>>>> left, right = 0, 10 >>>>>>>>>>>> right, left = left, right >>>>>>>>>>>>>>>>>>>>>>>> pprriinntt rriigghhtt 0 >>>>>>>>>>>> print left 10 >>>>>>>>>>>> Python creates temporaries if needed left right _tmp_ 0 10 Python Tuples
  • 38. Provides a quick way to swap variables' values >>>>>>>>>>>> left, right = 0, 10 >>>>>>>>>>>> right, left = left, right >>>>>>>>>>>>>>>>>>>>>>>> pprriinntt rriigghhtt 0 >>>>>>>>>>>> print left 10 >>>>>>>>>>>> Python creates temporaries if needed left right _tmp_ 0 10 Python Tuples
  • 39. Provides a quick way to swap variables' values >>>>>>>>>>>> left, right = 0, 10 >>>>>>>>>>>> right, left = left, right >>>>>>>>>>>>>>>>>>>>>>>> pprriinntt rriigghhtt 0 >>>>>>>>>>>> print left 10 >>>>>>>>>>>> Python creates temporaries if needed left right 0 10 Python Tuples
  • 40. And an easy way to unpack a list Python Tuples
  • 41. And an easy way to unpack a list >>>>>>>>>>>> colors = ['yellow', 'magenta', 'lavender'] >>>>>>>>>>>> Python Tuples
  • 42. And an easy way to unpack a list >>>>>>>>>>>> colors = ['yellow', 'magenta', 'lavender'] >>>>>>>>>>>> left, middle, right = colors >>>>>>>>>>>>>>>>>>>>>>>> Python Tuples
  • 43. And an easy way to unpack a list >>>>>>>>>>>> colors = ['yellow', 'magenta', 'lavender'] >>>>>>>>>>>> left, middle, right = colors >>>>>>>>>>>>>>>>>>>>>>>> pprriinntt lleefftt yellow >>>>>>>>>>>> print middle magenta >>>>>>>>>>>> print right lavender >>>>>>>>>>>> Python Tuples
  • 44. And an easy way to unpack a list >>>>>>>>>>>> colors = ['yellow', 'magenta', 'lavender'] >>>>>>>>>>>> left, middle, right = colors >>>>>>>>>>>>>>>>>>>>>>>> pprriinntt lleefftt yellow >>>>>>>>>>>> print middle magenta >>>>>>>>>>>> print right lavender >>>>>>>>>>>> Number of values must be the same Python Tuples
  • 45. Often used in loops Python Tuples
  • 46. Often used in loops >>>>>>>>>>>> pairs = ((1, 10), (2, 20), (3, 30), (4, 40)) >>>>>>>>>>>> Python Tuples
  • 47. Often used in loops >>>>>>>>>>>> pairs = ((1, 10), (2, 20), (3, 30), (4, 40)) >>>>>>>>>>>> for p in pairs: ........................ pprriinntt pp[[00]] ++ pp[[11]] Python Tuples
  • 48. Often used in loops >>>>>>>>>>>> pairs = ((1, 10), (2, 20), (3, 30), (4, 40)) >>>>>>>>>>>> for p in pairs: ........................ pprriinntt pp[[00]] ++ pp[[11]] Python Tuples
  • 49. Often used in loops >>>>>>>>>>>> pairs = ((1, 10), (2, 20), (3, 30), (4, 40)) >>>>>>>>>>>> for (low, high) in pairs: ........................ pprriinntt llooww ++ hhiigghh Python Tuples
  • 50. Often used in loops >>>>>>>>>>>> pairs = ((1, 10), (2, 20), (3, 30), (4, 40)) >>>>>>>>>>>> for (low, high) in pairs: ........................ pprriinntt llooww ++ hhiigghh ............ 11 22 33 44 >>>>>>>>>>>> Python Tuples
  • 51. The enumerate function produces (index, value) pairs Python Tuples
  • 52. The enumerate function produces (index, value) pairs >>>>>>>>>>>> colors = ['yellow', 'magenta', 'lavender'] >>>>>>>>>>>> for (i, name) in enumerate(colors): ........................ pprriinntt ii,, nnaammee Python Tuples
  • 53. The enumerate function produces (index, value) pairs >>>>>>>>>>>> colors = ['yellow', 'magenta', 'lavender'] >>>>>>>>>>>> for (i, name) in enumerate(colors): ........................ pprriinntt ii,, nnaammee ............ 0 yellow 1 magenta 2 lavender >>>>>>>>>>>> Python Tuples
  • 54. The enumerate function produces (index, value) pairs >>>>>>>>>>>> colors = ['yellow', 'magenta', 'lavender'] >>>>>>>>>>>> for (i, name) in enumerate(colors): ........................ pprriinntt ii,, nnaammee ............ 0 yellow 1 magenta 2 lavender >>>>>>>>>>>> Prefer this to range(len(values)) Python Tuples
  • 55. created by Greg Wilson October 2010 Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information.