Difference between revisions of "Python-3.4.3/C3/Getting-started-with-tuples/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
Line 267: Line 267:
 
Type, '''b'''
 
Type, '''b'''
  
We see that the '''values''' are '''swapped''' easily.
+
We can see that the '''values''' are '''swapped''' easily.
  
 
|-
 
|-
Line 366: Line 366:
 
# Define a '''tuple''' containing two values as given below. The first value is '''integer''' 4 and the second value is '''float''' 2.5.  
 
# Define a '''tuple''' containing two values as given below. The first value is '''integer''' 4 and the second value is '''float''' 2.5.  
 
# If we type, '''a''' ''is equal to''''' 5''' ''comma ''then what is the '''datatype''' of '''a'''?  
 
# If we type, '''a''' ''is equal to''''' 5''' ''comma ''then what is the '''datatype''' of '''a'''?  
# If we type, '''a''' ''is equal to inside brackets '''''2''' ''comma '''''3a''' ''inside square brackets '''''0''' ''comma'' '''a''' ''inside square brackets '''''1''' ''is equal to inside brackets '''''3''' ''comma'' '''4'''What is the output?
+
# If we type, '''a''' ''is equal to inside brackets '''''2''' ''comma '''''3 a''' ''inside square brackets '''''0''' ''comma'' '''a''' ''inside square brackets '''''1''' ''is equal to inside brackets '''''3''' ''comma'' '''4''' What is the output?
  
 
|-
 
|-

Latest revision as of 11:09, 19 September 2018

Visual Cue
Narration
Show Slide: Title Welcome to the tutorial on Getting Started with tuples.
Show Slide

Learning Objectives

In this tutorial we will:
  1. Understand what tuples are
  2. Compare tuples with lists
  3. Know why they are needed and
  4. Learn to use them.
Show Slide

System Specifications

To record this tutorial, I am using
  • Ubuntu Linux 16.04 operating system
  • Python 3.4.3 and
  • IPython 5.1.0
Show Slide

Pre-requisites

To practise this tutorial, you should know how to
  • run basic Python commands on the ipython console
  • use lists

If not, see the relevant Python tutorials on this website.

Show slide

Tuples in Python

First we will learn about tuples.
  • Tuple is a collection of elements similar to a list.
  • Tuple uses parentheses, whereas list uses square brackets.
  • Elements of a tuple cannot be changed once it is assigned.
  • But in a list, elements can be changed.
Show slide

Tuple declaration

(1, 2.5) and 1, 2.5 are same

(1, ) and 1, are same

((1,),) and (1,), are same

Here are few examples for declaring tuples.

Inside brackets 1, 2.5 is a tuple with two elements.

The same can be declared as 1, 2.5.


Inside brackets 1 comma is a tuple with one element.

The same can be declared as 1 comma


Inside brackets again inside brackets 1 comma and outside bracket a comma is a tuple with one tuple as element.

The same can be declared as inside brackets 1 comma and outside bracket a comma.

Open the terminal Let us start ipython.


Open the terminal.

Type ipython3 and press Enter Type ipython3 and press Enter.


From here onwards, remember to press the Enter key after typing every command on the terminal.

Type, t = (1, 2.5, "hello", -4, "world", 1.24, 5) Let's learn to create a tuple.

Type, t is equal to inside brackets 1 comma 2.5 comma inside double quotes hello comma -4 comma inside double quotes world comma 1.24 comma 5

Type, t


Highlight , and parentheses

Type, t

It is similar to list except that parentheses are used instead of square brackets.

At least one comma is mandatory for a tuple.

The brackets are optional, but should be added for clarity.

t[3] The items in the tuple can be accessed by their index positions.


Type, t inside square brackets 3

t[1:5:2] Type, t inside square brackets 1 colon 5 colon 2

It prints the corresponding slice.

t[2] = "Hello"


Now we try to change an element in the tuple.


Type, t inside square brackets 2 is equal to inside double quotes Hello


We can see that, it raises an error saying 'tuple object does not support item assignment'.


It shows, elements of a tuple cannot be changed after it is created.


This is called immutability.

for x in t:

print(x)


We can iterate over tuples like lists.

Type,

for x in t colon

print inside brackets x


It prints each element of the tuple t.


<<PAUSE>>

Pause the video.


Try this exercise and then resume the video.

Show Slide

Exercise 1

Let us look at a simple problem of swapping values.

Given, a is equal to 5 and b is equal to 7, swap the values of a and b.

Switch terminal Switch to the terminal for the solution.
Type,

a = 5

b = 7


a

b

Type,

a is equal to 5

b is equal to 7


Then type, a

Type, b

Type,

temp = a

a = b

b = temp

a

b


Highlight the outputs

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


Type

temp is equal to a

a is equal to b

b is equal to temp

Then type, a

Type, b

We can see that the values are successfully swapped now.


But this is the traditional approach.

Type,

a = 5

b = 7

a, b = b, a


a

b

Now let us do it in the Python way.


Type

a is equal to 5

b is equal to 7

a, b is equal to b, a


Now we will check whether the values are swapped.

Type, a

Type, b

We can see that the values are swapped easily.

Type,

a = 2.5

b = "hello"

a, b = b, a


a

b

We can also do this for different datatypes.


Type,

a is equal to 2.5

b is equal to inside double quotes hello

a, b is equal to b, a


Now to check the values, type, a

Type, b

<<PAUSE>>

Highlight a, b = b, a This is possible because of the immutability of tuples.


This is called tuple packing and unpacking.


When you type, a comma b is equal to b comma a


First the values of b and a are packed into a tuple on the right side. Then it is unpacked into the variables a and b.

5,


Highlight the output

Let us first see about tuple packing.

Type, 5 comma


We can see a tuple with one element.

5, "hello", 2.5 Type, 5 comma inside double quotes hello comma 2.5


Now it is a tuple with three elements.


When we type two or more elements separated by commas, those elements are packed into a tuple.


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

Show Slide

Summary slide


This brings us to the end of this tutorial. Let us summarize.


In this tutorial, we have learnt about,

  • Tuples
  • Similarities of tuples with lists
  • Immutability of tuples and
  • Concept of Packing and unpacking of tuples.
Show Slide

Evaluation


Here are some self assessment questions for you to solve.


  1. Define a tuple containing two values as given below. The first value is integer 4 and the second value is float 2.5.
  2. If we type, a is equal to 5 comma then what is the datatype of a?
  3. If we type, a is equal to inside brackets 2 comma 3 a inside square brackets 0 comma a inside square brackets 1 is equal to inside brackets 3 comma 4 What is the output?
Show Slide

Solutions


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, inside brackets 4 comma 2.5
  2. Since the given data is 5 followed by a comma, it is a tuple
  3. The given operation will give a TypeError because tuples are immutable.
Show Slide Forum Please post your timed queries in this forum.
Show Slide Fossee Forum Please post your general queries on Python in this forum.
Slide Textbook Companion FOSSEE team coordinates the TBC project.
Show Slide Acknowledgement Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India.

For more details, visit this website.

Show Slide Thank You This is Priya from IIT Bombay signing off.

Thanks for watching.

Contributors and Content Editors

Nancyvarkey, Nirmala Venkat, Priyacst