<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="https://script.spoken-tutorial.org/skins/common/feed.css?303"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>https://script.spoken-tutorial.org/index.php?action=history&amp;feed=atom&amp;title=Python%2FC3%2FGetting-started-with-tuples_%2FEnglish</id>
		<title>Python/C3/Getting-started-with-tuples /English - Revision history</title>
		<link rel="self" type="application/atom+xml" href="https://script.spoken-tutorial.org/index.php?action=history&amp;feed=atom&amp;title=Python%2FC3%2FGetting-started-with-tuples_%2FEnglish"/>
		<link rel="alternate" type="text/html" href="https://script.spoken-tutorial.org/index.php?title=Python/C3/Getting-started-with-tuples_/English&amp;action=history"/>
		<updated>2026-04-08T20:07:03Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.23.17</generator>

	<entry>
		<id>https://script.spoken-tutorial.org/index.php?title=Python/C3/Getting-started-with-tuples_/English&amp;diff=496&amp;oldid=prev</id>
		<title>Chandrika: Created page with '{| border=1 !Visual Cue !Narration |- | Show Slide 1   Containing title, name of the production team along with the logo of MHRD  | Hello friends and welcome to the tutorial on &quot;…'</title>
		<link rel="alternate" type="text/html" href="https://script.spoken-tutorial.org/index.php?title=Python/C3/Getting-started-with-tuples_/English&amp;diff=496&amp;oldid=prev"/>
				<updated>2012-11-29T06:16:20Z</updated>
		
		<summary type="html">&lt;p&gt;Created page with &amp;#039;{| border=1 !Visual Cue !Narration |- | Show Slide 1   Containing title, name of the production team along with the logo of MHRD  | Hello friends and welcome to the tutorial on &amp;quot;…&amp;#039;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{| border=1&lt;br /&gt;
!Visual Cue&lt;br /&gt;
!Narration&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 1 &lt;br /&gt;
&lt;br /&gt;
Containing title, name of the production team along with the logo of MHRD &lt;br /&gt;
| Hello friends and welcome to the tutorial on &amp;quot;getting started with tuples&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 2 &lt;br /&gt;
&lt;br /&gt;
Learning objectives &lt;br /&gt;
| At the end of the tutorial, you will be able to,&lt;br /&gt;
&lt;br /&gt;
# Understand of what tuples are.&lt;br /&gt;
# Compare them with lists.&lt;br /&gt;
# Know why they are needed and where to use them.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 3 &lt;br /&gt;
&lt;br /&gt;
Pre-requisite slide &lt;br /&gt;
| Before beginning this tutorial,we would suggest you to complete the tutorial on &amp;quot;Getting started with lists&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Open the terminal &lt;br /&gt;
&lt;br /&gt;
 ipython&lt;br /&gt;
| Let us start our ipython interpreter.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  t = (1, 2.5, &amp;quot;hello&amp;quot;, -4, &amp;quot;world&amp;quot;, 1.24, 5)&lt;br /&gt;
 t&lt;br /&gt;
| Let's get started by defining a tuple. A tuple is defined by enclosing parentheses around a sequence of items separated by commas. It is similar to defining a list except that parentheses are used instead of square brackets.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  &amp;lt;nowiki&amp;gt;t[3]&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
| The items in the tuple are indexed using numbers and can be accessed by using their position. For example,&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  &amp;lt;nowiki&amp;gt;t[1:5:2]&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
| It prints -4 which is the fourth item of the tuple Similarly,&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  &amp;lt;nowiki&amp;gt;t[2] = &amp;quot;Hello&amp;quot;&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
| It prints the corresponding slice&lt;br /&gt;
&lt;br /&gt;
This behaviour is similar to that of lists. But the difference can be seen when we try to change an element in the tuple.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
| We can see that, it raises an error saying 'tuple object does not support item assignment'. Tuples are immutable, and hence cannot be changed after creation.&lt;br /&gt;
&lt;br /&gt;
Then, what is the use of tuples? We shall understand that soon. But let us look at a simple problem of swapping values.&lt;br /&gt;
&lt;br /&gt;
Pause the video here, try out the following exercise and resume the video.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 4&lt;br /&gt;
&lt;br /&gt;
Assignment 1 &lt;br /&gt;
| Given, a = 5 and b = 7. Swap the values of a and b.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Continue from paused state Switch to the terminal &lt;br /&gt;
&lt;br /&gt;
 a = 5&lt;br /&gt;
 b = 7&lt;br /&gt;
 &lt;br /&gt;
 a&lt;br /&gt;
 b&lt;br /&gt;
| Switch to terminal of solution&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  temp = a&lt;br /&gt;
 a = b&lt;br /&gt;
 b = temp&lt;br /&gt;
 &lt;br /&gt;
 a&lt;br /&gt;
 b&lt;br /&gt;
| We now create a variable say, temp and swap the values using this variable&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  a&lt;br /&gt;
 b&lt;br /&gt;
 &lt;br /&gt;
 a, b = b, a&lt;br /&gt;
 &lt;br /&gt;
 a&lt;br /&gt;
 b&lt;br /&gt;
| This is the traditional approach Now let us do it the python way&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  a = 2.5&lt;br /&gt;
 b = &amp;quot;hello&amp;quot;&lt;br /&gt;
 a, b = b, a&lt;br /&gt;
 a&lt;br /&gt;
 b&lt;br /&gt;
| We see that the values are swapped. This idiom works for different data-types also.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  5,&lt;br /&gt;
| Moreover this type of behaviour is something that feels natural and you'd expect to happen.&lt;br /&gt;
&lt;br /&gt;
This is possible because of the immutability of tuples. This process is called tuple packing and unpacking.&lt;br /&gt;
&lt;br /&gt;
Let us first see what is tuple packing. Type&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  5, &amp;quot;hello&amp;quot;, 2.5&lt;br /&gt;
| What we see is a tuple with one element.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
| Now it is a tuple with three elements.&lt;br /&gt;
&lt;br /&gt;
So when we are actually typing two or more elements separated by commas, those elements are packed into a tuple.&lt;br /&gt;
&lt;br /&gt;
When you type&lt;br /&gt;
&lt;br /&gt;
a, b = b, a&lt;br /&gt;
&lt;br /&gt;
First the values of b and a are packed into a tuple on the right side and then unpacked into the variables a and b.&lt;br /&gt;
&lt;br /&gt;
Immutability of tuples ensures that the values are not changed during the packing and unpacking.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 5 &lt;br /&gt;
&lt;br /&gt;
Summary slide &lt;br /&gt;
| This brings us to the end of this tutorial. In this tutorial, we have learnt to,&lt;br /&gt;
&lt;br /&gt;
# Define tuples.&lt;br /&gt;
# Understand the similarities of tuples with lists, like indexing and iterability.&lt;br /&gt;
# Know about the immutability of tuples.&lt;br /&gt;
# Swap values, the python way.&lt;br /&gt;
# Understand the concept of packing and unpacking of tuples.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 6 &lt;br /&gt;
&lt;br /&gt;
Self assessment questions slide &lt;br /&gt;
| Here are some self assessment questions for you to solve&lt;br /&gt;
&lt;br /&gt;
# Define a tuple containing two values. The first being integer 4 and second is a float 2.5&lt;br /&gt;
# If &amp;lt;tt&amp;gt;a = 5,&amp;lt;/tt&amp;gt; then what is the type of a ?&lt;br /&gt;
** int&lt;br /&gt;
** float&lt;br /&gt;
** tuple&lt;br /&gt;
** string&lt;br /&gt;
# if &amp;lt;tt&amp;gt;a = (2, 3)&amp;lt;/tt&amp;gt;. What does &amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;a[0], a[1] = (3, 4)&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt; produce&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 7&lt;br /&gt;
&lt;br /&gt;
Solution of self assessment questions on slide &lt;br /&gt;
| And the answers,&lt;br /&gt;
&lt;br /&gt;
# A tuple is defined by enclosing parentheses around a sequence of items separated by commas. Hence, we write our tuple as,&lt;br /&gt;
&lt;br /&gt;
Enumerated list ends without a blank line; unexpected unindent.&lt;br /&gt;
&lt;br /&gt;
 (4, 2.5)&lt;br /&gt;
&lt;br /&gt;
# Since the given data is 5 followed by a comma, it means that it is a tuple&lt;br /&gt;
# &amp;lt;nowiki&amp;gt;The operation a[0], a[1] = (3, 4) will result in an error because tuples are immutable.&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 8 &lt;br /&gt;
&lt;br /&gt;
Acknowledgment slide &lt;br /&gt;
| Hope you have enjoyed this tutorial and found it useful. Thank you!&lt;br /&gt;
&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Chandrika</name></author>	</entry>

	</feed>