Difference between revisions of "Python-3.4.3/C3/Getting-started-with-tuples/English"
(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 | ||
− | |||
− | 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 | + | 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 | + | 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 | + | | 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 ''''' | + | # 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''' | + | # The given operation will give a '''TypeError''' because '''tuples''' are '''immutable'''. |
− | + | ||
− | + | ||
|- | |- |
Latest revision as of 11:09, 19 September 2018
|
|
Show Slide: Title | Welcome to the tutorial on Getting Started with tuples. |
Show Slide
Learning Objectives |
In this tutorial we will:
|
Show Slide
System Specifications |
To record this tutorial, I am using
|
Show Slide
Pre-requisites |
To practise this tutorial, you should know how to
If not, see the relevant Python tutorials on this website. |
Show slide
Tuples in Python |
First we will learn about tuples.
|
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.
The same can be declared as 1 comma
The same can be declared as inside brackets 1 comma and outside bracket a comma. |
Open the terminal | Let us start ipython.
|
Type ipython3 and press Enter | Type ipython3 and press Enter.
|
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
|
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.
|
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.
|
for x in t:
print(x)
|
We can iterate over tuples like lists.
Type, for x in t colon print inside brackets x
|
Pause 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
b |
Type,
a is equal to 5 b is equal to 7
Type, b |
Type,
temp = a a = b b = temp a b
|
We now create a variable say, temp and swap the values using this variable.
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.
|
Type,
a = 5 b = 7 a, b = b, a
b |
Now let us do it in the Python way.
a is equal to 5 b is equal to 7 a, b is equal to b, a
Type, a Type, b We can see that the values are swapped easily. |
Type,
a = 2.5 b = "hello" a, b = b, a
b |
We can also do this for different datatypes.
a is equal to 2.5 b is equal to inside double quotes hello a, b is equal to b, a
Type, b <<PAUSE>> |
Highlight a, b = b, a | This is possible because of the immutability of tuples.
|
5,
|
Let us first see about tuple packing.
Type, 5 comma
|
5, "hello", 2.5 | Type, 5 comma inside double quotes hello comma 2.5
|
Show Slide
Summary slide
|
This brings us to the end of this tutorial. Let us summarize.
|
Show Slide
Evaluation
|
Here are some self assessment questions for you to solve.
|
Show Slide
Solutions
|
And the answers,
|
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. |