Python/C3/Getting-started-with-tuples /English

From Script | Spoken-Tutorial
Jump to: navigation, search
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 "getting started with tuples".
Show Slide 2

Learning objectives

At the end of the tutorial, you will be able to,
  1. Understand of what tuples are.
  2. Compare them with lists.
  3. Know why they are needed and where to use them.


Show Slide 3

Pre-requisite slide

Before beginning this tutorial,we would suggest you to complete the tutorial on "Getting started with lists".
Open the terminal
ipython
Let us start our ipython interpreter.
t = (1, 2.5, "hello", -4, "world", 1.24, 5)
t
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.
t[3] The items in the tuple are indexed using numbers and can be accessed by using their position. For example,
t[1:5:2] It prints -4 which is the fourth item of the tuple Similarly,
t[2] = "Hello" It prints the corresponding slice

This behaviour is similar to that of lists. But the difference can be seen when we try to change an element in the tuple.

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.

Then, what is the use of tuples? We shall understand that soon. But let us look at a simple problem of swapping values.

Pause the video here, try out the following exercise and resume the video.

Show Slide 4

Assignment 1

Given, a = 5 and b = 7. Swap the values of a and b.
Continue from paused state Switch to the terminal
a = 5
b = 7

a
b
Switch to terminal of solution
temp = a
a = b
b = temp

a
b
We now create a variable say, temp and swap the values using this variable
a
b

a, b = b, a

a
b
This is the traditional approach Now let us do it the python way
a = 2.5
b = "hello"
a, b = b, a
a
b
We see that the values are swapped. This idiom works for different data-types also.
5, Moreover this type of behaviour is something that feels natural and you'd expect to happen.

This is possible because of the immutability of tuples. This process is called tuple packing and unpacking.

Let us first see what is tuple packing. Type

5, "hello", 2.5 What we see is a tuple with one element.
Now it is a tuple with three elements.

So when we are actually typing two or more elements separated by commas, those elements are packed into a tuple.

When you type

a, b = b, a

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.

Immutability of tuples ensures that the values are not changed during the packing and unpacking.

Show Slide 5

Summary slide

This brings us to the end of this tutorial. In this tutorial, we have learnt to,
  1. Define tuples.
  2. Understand the similarities of tuples with lists, like indexing and iterability.
  3. Know about the immutability of tuples.
  4. Swap values, the python way.
  5. Understand the concept of packing and unpacking of tuples.


Show Slide 6

Self assessment questions slide

Here are some self assessment questions for you to solve
  1. Define a tuple containing two values. The first being integer 4 and second is a float 2.5
  2. If a = 5, then what is the type of a ?
    • int
    • float
    • tuple
    • string
  1. if a = (2, 3). What does a[0], a[1] = (3, 4) produce


Show Slide 7

Solution of self assessment questions on slide

And the answers,
  1. A tuple is defined by enclosing parentheses around a sequence of items separated by commas. Hence, we write our tuple as,

Enumerated list ends without a blank line; unexpected unindent.

(4, 2.5)
  1. Since the given data is 5 followed by a comma, it means that it is a tuple
  2. The operation a[0], a[1] = (3, 4) will result in an error because tuples are immutable.


Show Slide 8

Acknowledgment slide

Hope you have enjoyed this tutorial and found it useful. Thank you!

Contributors and Content Editors

Chandrika