KEMBAR78
Python Programming Essentials - M13 - Tuples | PPTX
http://www.skillbrew.com
/SkillbrewTalent brewed by the
industry itself
Python Tuples
Pavan Verma
@YinYangPavan
Python Programming Essentials
© SkillBrew http://skillbrew.com
What is a Tuple
 Tuple is a sequence of items, similar to a list
 The difference between list and tuple is:
tuple is immutable while list is mutable
2
© SkillBrew http://skillbrew.com
Tuple example
 Lets say we want to represent number of
hits on blog on a particular date
3
© SkillBrew http://skillbrew.com
Lets create a tuple
Myblog.com 100 20-Oct-2013
4
blog_hits = ['Myblog.com', 50, '20-Oct-2013']
blog_hits = ('Myblog.com', 50, '20-Oct-2013')
list
tuple
© SkillBrew http://skillbrew.com
Packing a Tuple
5
blog_hits = 'Myblog.com', 50, '20-Oct-2013'
print blog_hits
('Myblog.com', 50, '20-Oct-2013')
© SkillBrew http://skillbrew.com
Unpacking a Tuple
6
>> blog_name, hits, date = blog_hits
>> print blog_name, hits, date
'Myblog.com' 50 '20-Oct-2013'
© SkillBrew http://skillbrew.com
Accessing Values in Tuples
Myblog.com 100 20-Oct-2013
7
>>> blog_hits = ('Myblog.com', 50, '20-Oct-2013')
>>> blog_hits[0]
Myblog.com
>>> blog_hits[2]
20-Oct-2013
0 1 2
© SkillBrew http://skillbrew.com
Tuple with a single element
>>> blog_hits = ('Myblog.com')
>>> type(blog_hits)
<type 'str'>
>>> blog_hits = ('Myblog.com', )
>>> type(blog_hits)
<type 'tuple'>
8
Must have this comma
© SkillBrew http://skillbrew.com
Common Operations on Tuples
9
Python Expression Results Description
len(1, 2, 3) 3 Length
(1, 2, 3) + (4, 5, 6) (1, 2, 3, 4, 5, 6) Concatenation
a = (1, 2, 3, 4, 5, 6)
a[2:5]
(3, 4, 5) Slicing
© SkillBrew http://skillbrew.com
How are Tuples different from Lists
 Apart from syntactical difference ( ) vs [ ], the real
difference is that lists are mutable whereas tuples
are not
 Tuples (generally) are sequences of different kinds
of stuff, and you deal with the tuple as a coherent
unit
 Lists (generally) are sequences of the same kind of
stuff, and you deal with the items individually
10
© SkillBrew http://skillbrew.com
Tuples are Immutable
>>> blog_hits[0] = 'yourblog.com'
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
blog_hits[0] = 'yourblog.com'
TypeError: 'tuple' object does not support item assignment
11
© SkillBrew http://skillbrew.com
Example of tuple and list usage
>>> colors = ['red', 'green', 'blue']
>>> colors = [
('red', '#FF0000'),
('green', '#00FF00'),
('blue', '#0000FF')
]
12
• tuples are used in Python where a struct would be
used in C/C++
• Tuples usually heterogeneous data whereas lists
usually contain homogenous data
© SkillBrew http://skillbrew.com
Converting a tuple to list
>>> blog_hits = ('Myblog.com', 50, '20-Oct-2013')
>>> list(blog_hits)
['Myblog.com', 50, '20-Oct-2013']
13
Use list() function to convert
a tuple to a list
© SkillBrew http://skillbrew.com
Use tuple to return multiple values from functions
Tuples are used to return multiple values from a function
14
>>> def points(x):
y = 1 - x
return x, y
>>> points(.5)
(0.5, 0.5)
>>> t = points(.3)
>>> t
(0.3, 0.7)
>>> t[0]
0.3
>>> t[1]
0.7
>>>
© SkillBrew http://skillbrew.com
Summary
 What is a tuple
 Packing/unpacking a tuple
 Accessing values in tuple
 Tuple with a single element
 Tuples are immutable
 How are tuples different form lists
 Converting between tuple and list
 Returning multiple values from functions
15
© SkillBrew http://skillbrew.com
References
 Tutorial on tuples
http://www.tutorialspoint.com/python/python_tuples.htm
 Basics tuple operation python documentation
http://docs.python.org/2/tutorial/datastructures.html#tuples-
and-sequences
16
17

Python Programming Essentials - M13 - Tuples

  • 1.
    http://www.skillbrew.com /SkillbrewTalent brewed bythe industry itself Python Tuples Pavan Verma @YinYangPavan Python Programming Essentials
  • 2.
    © SkillBrew http://skillbrew.com Whatis a Tuple  Tuple is a sequence of items, similar to a list  The difference between list and tuple is: tuple is immutable while list is mutable 2
  • 3.
    © SkillBrew http://skillbrew.com Tupleexample  Lets say we want to represent number of hits on blog on a particular date 3
  • 4.
    © SkillBrew http://skillbrew.com Letscreate a tuple Myblog.com 100 20-Oct-2013 4 blog_hits = ['Myblog.com', 50, '20-Oct-2013'] blog_hits = ('Myblog.com', 50, '20-Oct-2013') list tuple
  • 5.
    © SkillBrew http://skillbrew.com Packinga Tuple 5 blog_hits = 'Myblog.com', 50, '20-Oct-2013' print blog_hits ('Myblog.com', 50, '20-Oct-2013')
  • 6.
    © SkillBrew http://skillbrew.com Unpackinga Tuple 6 >> blog_name, hits, date = blog_hits >> print blog_name, hits, date 'Myblog.com' 50 '20-Oct-2013'
  • 7.
    © SkillBrew http://skillbrew.com AccessingValues in Tuples Myblog.com 100 20-Oct-2013 7 >>> blog_hits = ('Myblog.com', 50, '20-Oct-2013') >>> blog_hits[0] Myblog.com >>> blog_hits[2] 20-Oct-2013 0 1 2
  • 8.
    © SkillBrew http://skillbrew.com Tuplewith a single element >>> blog_hits = ('Myblog.com') >>> type(blog_hits) <type 'str'> >>> blog_hits = ('Myblog.com', ) >>> type(blog_hits) <type 'tuple'> 8 Must have this comma
  • 9.
    © SkillBrew http://skillbrew.com CommonOperations on Tuples 9 Python Expression Results Description len(1, 2, 3) 3 Length (1, 2, 3) + (4, 5, 6) (1, 2, 3, 4, 5, 6) Concatenation a = (1, 2, 3, 4, 5, 6) a[2:5] (3, 4, 5) Slicing
  • 10.
    © SkillBrew http://skillbrew.com Howare Tuples different from Lists  Apart from syntactical difference ( ) vs [ ], the real difference is that lists are mutable whereas tuples are not  Tuples (generally) are sequences of different kinds of stuff, and you deal with the tuple as a coherent unit  Lists (generally) are sequences of the same kind of stuff, and you deal with the items individually 10
  • 11.
    © SkillBrew http://skillbrew.com Tuplesare Immutable >>> blog_hits[0] = 'yourblog.com' Traceback (most recent call last): File "<pyshell#3>", line 1, in <module> blog_hits[0] = 'yourblog.com' TypeError: 'tuple' object does not support item assignment 11
  • 12.
    © SkillBrew http://skillbrew.com Exampleof tuple and list usage >>> colors = ['red', 'green', 'blue'] >>> colors = [ ('red', '#FF0000'), ('green', '#00FF00'), ('blue', '#0000FF') ] 12 • tuples are used in Python where a struct would be used in C/C++ • Tuples usually heterogeneous data whereas lists usually contain homogenous data
  • 13.
    © SkillBrew http://skillbrew.com Convertinga tuple to list >>> blog_hits = ('Myblog.com', 50, '20-Oct-2013') >>> list(blog_hits) ['Myblog.com', 50, '20-Oct-2013'] 13 Use list() function to convert a tuple to a list
  • 14.
    © SkillBrew http://skillbrew.com Usetuple to return multiple values from functions Tuples are used to return multiple values from a function 14 >>> def points(x): y = 1 - x return x, y >>> points(.5) (0.5, 0.5) >>> t = points(.3) >>> t (0.3, 0.7) >>> t[0] 0.3 >>> t[1] 0.7 >>>
  • 15.
    © SkillBrew http://skillbrew.com Summary What is a tuple  Packing/unpacking a tuple  Accessing values in tuple  Tuple with a single element  Tuples are immutable  How are tuples different form lists  Converting between tuple and list  Returning multiple values from functions 15
  • 16.
    © SkillBrew http://skillbrew.com References Tutorial on tuples http://www.tutorialspoint.com/python/python_tuples.htm  Basics tuple operation python documentation http://docs.python.org/2/tutorial/datastructures.html#tuples- and-sequences 16
  • 17.

Editor's Notes

  • #9 It is mandatory to have a , in tuple
  • #13 If you have to represent lets say colors a list would be fine But if you have to represent a color and its code it makes more sense to pack it into a tuple