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

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with " {| style="border-spacing:0;" | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| <ce...")
 
 
(One intermediate revision by one other user not shown)
Line 11: Line 11:
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Show Slide  
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Show Slide  
  
Objectives  
+
Learning Objectives  
 
+
 
+
  
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| In this tutorial we will:  
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| In this tutorial we will:  
 
 
 
# Understand what '''tuples''' are
 
# Understand what '''tuples''' are
 
# Compare '''tuples''' with '''lists'''
 
# Compare '''tuples''' with '''lists'''
 
# Know why they are needed and
 
# Know why they are needed and
 
# Learn to use them.  
 
# Learn to use them.  
 
 
  
 
|-
 
|-
Line 30: Line 24:
 
System Specifications  
 
System Specifications  
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| To record this tutorial, I am using  
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| To record this tutorial, I am using  
 
 
* '''Ubuntu Linux 16.04''' operating system
 
* '''Ubuntu Linux 16.04''' operating system
 
* '''Python 3.4.3 '''and
 
* '''Python 3.4.3 '''and
 
* '''IPython 5.1.0'''
 
* '''IPython 5.1.0'''
 
 
  
 
|-
 
|-
Line 42: Line 33:
 
Pre-requisites
 
Pre-requisites
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| To practise this tutorial, you should know how to
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| To practise this tutorial, you should know how to
 
 
* run basic '''Python '''commands on the '''ipython console'''
 
* run basic '''Python '''commands on the '''ipython console'''
 
* use '''lists'''
 
* use '''lists'''
Line 52: Line 42:
  
 
Tuples in Python
 
Tuples in Python
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| First we will learn about tuples.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| First we will learn about '''tuples'''.
 
+
* '''Tuple''' is a collection of '''elements''' similar to a '''list'''.
 
+
* '''Tuple''' uses '''parentheses''', whereas list uses '''square brackets.'''
* Tuple is a collection of elements similar to a list.
+
* '''Elements''' of a '''tuple''' cannot be changed once it is assigned.
* Tuple uses parentheses, whereas list uses square brackets.
+
* But in a '''list, elements''' can be changed.
* Elements of a tuple cannot be changed once it is assigned.
+
* But in a list, elements can be changed.
+
 
+
 
+
  
 
|-
 
|-
Line 72: Line 58:
  
 
((1,),) and (1,), are same
 
((1,),) and (1,), are same
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Here are few examples for declaring tuples.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Here are few examples for declaring '''tuples'''.
  
''Inside brackets'' 1, 2.5 is a tuple with two elements.
+
''Inside brackets'' 1, 2.5 is a '''tuple''' with two '''elements'''.
  
 
The same can be declared as 1, 2.5.  
 
The same can be declared as 1, 2.5.  
  
''Inside brackets ''1 comma is a tuple with one element
+
 
 +
''Inside brackets ''1 comma is a '''tuple''' with one '''element'''.
  
 
The same can be declared as 1 comma  
 
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  
+
''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.
  
 
|-
 
|-
Line 113: Line 101:
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type, '''t'''
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type, '''t'''
  
It is similar to '''list''' except that '''parentheses''' are used instead of '''square''' '''brackets'''.
+
It is similar to '''list''' except that '''parentheses''' are used instead of '''square brackets'''.
  
At least one comma is mandatory for a tuple.  
+
At least one comma is mandatory for a '''tuple'''.  
  
 
The brackets are optional, but should be added for clarity.
 
The brackets are optional, but should be added for clarity.
Line 137: Line 125:
  
  
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Now we try to change an element in the '''tuple'''.  
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Now we try to change an '''element''' in the '''tuple'''.  
  
  
Line 146: Line 134:
  
  
It shows, elements of a tuple cannot be changed after it is created.''' '''
+
It shows, '''elements''' of a '''tuple''' cannot be changed after it is created.  
  
  
Line 158: Line 146:
  
  
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| We can iterate over tuples like '''lists'''.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| We can iterate over '''tuples''' like '''lists'''.
  
 
Type,
 
Type,
Line 167: Line 155:
  
  
It prints each elements of the tuple''' t .'''
+
It prints each '''element''' of the '''tuple t.'''
  
  
<nowiki><<PAUSE>></nowiki>''' '''
+
<nowiki><<PAUSE>></nowiki>  
  
 
|-
 
|-
Line 189: Line 177:
 
|-
 
|-
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch terminal
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch terminal
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch to the terminal for the solution  
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch to the '''terminal''' for the solution.
  
 
|-
 
|-
Line 261: Line 249:
  
 
'''b'''
 
'''b'''
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Now let us do it in the python way.''' '''
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Now let us do it in the '''Python''' way.
  
  
Line 279: Line 267:
 
Type, '''b'''
 
Type, '''b'''
  
We see that the '''values''' are '''swapped''' easily.
+
We can see that the '''values''' are '''swapped''' easily.
  
 
|-
 
|-
Line 294: Line 282:
  
 
'''b'''
 
'''b'''
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| We can also do this for different datatypes.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| We can also do this for different '''datatypes'''.
  
  
Line 360: Line 348:
  
  
In this tutorial, we have learnt about,''' '''
+
In this tutorial, we have learnt about,  
 
+
 
+
 
* '''Tuples'''  
 
* '''Tuples'''  
 
* Similarities of '''tuples''' with '''lists'''
 
* Similarities of '''tuples''' with '''lists'''
 
* '''Immutability''' of '''tuples''' and
 
* '''Immutability''' of '''tuples''' and
 
* Concept of '''Packing''' and '''unpacking''' of '''tuples'''.  
 
* Concept of '''Packing''' and '''unpacking''' of '''tuples'''.  
 
 
  
 
|-
 
|-
Line 380: Line 364:
  
  
# 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?
 
+
 
+
  
 
|-
 
|-
Line 395: Line 377:
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| And the answers,''' '''
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| And the answers,''' '''
  
# 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
+
# 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
 
# Since the given data is 5 followed by a comma, it is a '''tuple'''  
 
# Since the given data is 5 followed by a comma, it is a '''tuple'''  
# The given operation will give a '''TypeError'''. Because '''tuples''' are '''immutable'''.  
+
# The given operation will give a '''TypeError''' because '''tuples''' are '''immutable'''.  
 
+
 
+
  
 
|-
 
|-

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