1
00:00:05,120 --> 00:00:08,020
Hi, welcome to this new video.

2
00:00:08,020 --> 00:00:10,260
We're going to start learning

3
00:00:10,260 --> 00:00:12,680
about some variable types

4
00:00:12,680 --> 00:00:14,525
that are slightly more advanced.

5
00:00:14,525 --> 00:00:18,490
We've learned about integers,

6
00:00:18,490 --> 00:00:22,690
floats, strings, but what
about having some collections?

7
00:00:22,690 --> 00:00:25,850
We're going to start introducing
the notion of tuple in

8
00:00:25,850 --> 00:00:27,030
Python which is

9
00:00:27,030 --> 00:00:30,270
the first and most simple
form of a collection.

10
00:00:30,270 --> 00:00:31,790
In Course 2, we're going to

11
00:00:31,790 --> 00:00:33,010
start spending a lot more time

12
00:00:33,010 --> 00:00:35,710
with lists and more
complex data structures.

13
00:00:35,710 --> 00:00:37,810
As a matter of
fact, Course 2 it's

14
00:00:37,810 --> 00:00:40,150
fully dedicated to data
structures but it's

15
00:00:40,150 --> 00:00:41,350
important at this point that we

16
00:00:41,350 --> 00:00:43,250
start introducing ourselves to

17
00:00:43,250 --> 00:00:47,675
the notion of a collection
with the tuple data structure.

18
00:00:47,675 --> 00:00:51,140
What is a tuple? In
Python a tuple it's

19
00:00:51,140 --> 00:00:53,600
an order immutable collection

20
00:00:53,600 --> 00:00:56,920
of elements. Let's
break that down.

21
00:00:58,160 --> 00:01:01,185
Immutable, specifically means

22
00:01:01,185 --> 00:01:03,415
that once you create a tuple,

23
00:01:03,415 --> 00:01:05,980
you cannot alter the
content of the tuple

24
00:01:05,980 --> 00:01:08,960
meaning that you
cannot add elements,

25
00:01:08,960 --> 00:01:11,280
you cannot remove elements
from this collection.

26
00:01:11,280 --> 00:01:13,660
It's a fixed collection.

27
00:01:13,660 --> 00:01:18,180
Order means that the
sequential order

28
00:01:18,180 --> 00:01:20,145
that they have it's important.

29
00:01:20,145 --> 00:01:21,980
You can expect to retrieve

30
00:01:21,980 --> 00:01:26,140
the information in the order
in which they were created.

31
00:01:26,140 --> 00:01:28,840
The information inside
that data structure

32
00:01:28,840 --> 00:01:31,045
is not going to be changed.

33
00:01:31,045 --> 00:01:35,280
Let's see examples
of that in syntax.

34
00:01:35,280 --> 00:01:36,900
As a matter of
fact, we've written

35
00:01:36,900 --> 00:01:38,180
a little bit of tuples,

36
00:01:38,180 --> 00:01:39,900
maybe not really
discussing them so much,

37
00:01:39,900 --> 00:01:41,975
but whenever we use
parentheses and

38
00:01:41,975 --> 00:01:45,200
commas within those parentheses,

39
00:01:45,200 --> 00:01:49,425
we're identifying a
collection of data.

40
00:01:49,425 --> 00:01:52,390
In this case, the parentheses

41
00:01:52,740 --> 00:01:54,820
gives us the clarity to

42
00:01:54,820 --> 00:01:56,930
understand that it
is in fact a tuple.

43
00:01:56,930 --> 00:01:59,000
Let's do an integer tuple.

44
00:01:59,000 --> 00:02:01,190
We can put several

45
00:02:01,190 --> 00:02:02,930
different integers
but you can see that

46
00:02:02,930 --> 00:02:04,280
the tuple can also be done with

47
00:02:04,280 --> 00:02:07,900
different variable types
such as the string.

48
00:02:07,900 --> 00:02:10,200
Once you've created a tuple,

49
00:02:10,200 --> 00:02:14,650
you might want to access
a specific element within

50
00:02:14,650 --> 00:02:19,530
the tuple and this is done
through accessing its index.

51
00:02:19,530 --> 00:02:24,210
The index represents
which is the number or

52
00:02:24,210 --> 00:02:28,950
the location of the element
within that data structure.

53
00:02:28,950 --> 00:02:32,920
In this case, the
tuple has 4,5, and 7.

54
00:02:32,920 --> 00:02:34,850
If we use the square brackets,

55
00:02:34,850 --> 00:02:36,890
and notice here that we're
using the square bracket

56
00:02:36,890 --> 00:02:41,265
to identify the index
that we want to access

57
00:02:41,265 --> 00:02:43,260
and that would be number 4

58
00:02:43,260 --> 00:02:45,550
and it's important that

59
00:02:45,550 --> 00:02:48,875
the first element in a
tuple will be element 0.

60
00:02:48,875 --> 00:02:50,570
We need to remember that because

61
00:02:50,570 --> 00:02:51,870
that's the way tuples work.

62
00:02:51,870 --> 00:02:53,665
Element 0 would
be the first one,

63
00:02:53,665 --> 00:02:55,130
element 1 would be
the second one,

64
00:02:55,130 --> 00:02:57,415
and element 2 would
be the third one.

65
00:02:57,415 --> 00:02:59,510
In this case, you
can see that we

66
00:02:59,510 --> 00:03:02,070
can access each one
of the elements

67
00:03:02,070 --> 00:03:07,155
within that tuple by using
the according index.

68
00:03:07,155 --> 00:03:09,255
You're going to be
hearing this a lot.

69
00:03:09,255 --> 00:03:12,590
What is the variable?

70
00:03:12,590 --> 00:03:14,890
Sometimes you use a
variable that represents

71
00:03:14,890 --> 00:03:17,070
the index in order

72
00:03:17,070 --> 00:03:19,545
to access the information
within a collection.

73
00:03:19,545 --> 00:03:23,500
Let's jump into processing and
write a little bit of code

74
00:03:23,500 --> 00:03:25,280
using tuples and understand

75
00:03:25,280 --> 00:03:28,460
a few exceptions
that might occur.

76
00:03:28,460 --> 00:03:31,000
Here we are back in processing.

77
00:03:31,000 --> 00:03:33,990
We have our template again.

78
00:03:33,990 --> 00:03:38,925
Let's start by creating
a simple tuple.

79
00:03:38,925 --> 00:03:42,920
I'm going to call this
variable countries and

80
00:03:42,920 --> 00:03:48,000
I'm going to use
parentheses to specify

81
00:03:48,000 --> 00:03:55,270
something like USA, Canada,

82
00:03:55,940 --> 00:04:04,065
and Mexico. This is a tuple.

83
00:04:04,065 --> 00:04:06,790
We have identified
three elements,

84
00:04:06,790 --> 00:04:11,420
three strings within
the tuple and if we

85
00:04:11,420 --> 00:04:18,590
would print the result,

86
00:04:18,590 --> 00:04:22,650
let's evaluate what is in fact

87
00:04:22,650 --> 00:04:26,590
the tuple here we can see
we get the full collection.

88
00:04:26,590 --> 00:04:29,190
All three elements are printed.

89
00:04:29,350 --> 00:04:31,930
But what if we would like to

90
00:04:31,930 --> 00:04:35,960
access the first
element only USA?

91
00:04:35,960 --> 00:04:38,810
We could use the
square brackets just

92
00:04:38,810 --> 00:04:42,380
after creating a
bit of space here.

93
00:04:42,380 --> 00:04:45,910
You can see we can use the
square brackets and put

94
00:04:45,910 --> 00:04:52,520
zero here to access the
first element of the tuple.

95
00:04:52,520 --> 00:04:56,540
We get USA and we
can do that for

96
00:04:56,540 --> 00:05:01,070
number 1 here and
number 2 as well.

97
00:05:01,070 --> 00:05:03,170
You'll see how we can in

98
00:05:03,170 --> 00:05:07,840
fact obtain each one of those
elements independently.

99
00:05:09,040 --> 00:05:14,790
There are some interesting ways

100
00:05:14,790 --> 00:05:18,130
in which we could iterate
or access the information.

101
00:05:18,130 --> 00:05:20,510
For instance, let's imagine
that we have a for loop.

102
00:05:20,510 --> 00:05:24,510
We can say for i and
before we had been

103
00:05:24,510 --> 00:05:26,235
identifying how a for loop

104
00:05:26,235 --> 00:05:28,620
goes between a range
between zero and 20,

105
00:05:28,620 --> 00:05:30,780
zero and 10 we

106
00:05:30,780 --> 00:05:33,010
go through numbers but
when we have a collection

107
00:05:33,010 --> 00:05:38,800
such as tuple we can
say for i in countries.

108
00:05:42,440 --> 00:05:48,250
I'm sorry, in countries.

109
00:05:48,740 --> 00:05:51,255
What we can get here,

110
00:05:51,255 --> 00:05:54,015
let's print i,

111
00:05:54,015 --> 00:05:56,260
so we're identifying
that the variable

112
00:05:56,260 --> 00:06:01,985
i is each one of the
elements within countries.

113
00:06:01,985 --> 00:06:03,695
If we print this,

114
00:06:03,695 --> 00:06:07,090
we would get a
first line of USA,

115
00:06:07,090 --> 00:06:08,530
a second line of Canada,

116
00:06:08,530 --> 00:06:10,015
a third line of Mexico.

117
00:06:10,015 --> 00:06:13,795
This is a very easy way of using

118
00:06:13,795 --> 00:06:16,730
a tuple it would

119
00:06:16,730 --> 00:06:19,090
also be a case when
we learn about lists,

120
00:06:19,090 --> 00:06:21,090
but in this case,

121
00:06:21,090 --> 00:06:22,670
how a collection is used

122
00:06:22,670 --> 00:06:26,895
as the source of the
domain for an iterator.

123
00:06:26,895 --> 00:06:32,015
We're looping through the
elements within the tuple.

124
00:06:32,015 --> 00:06:37,160
Let's also understand how
this tuple is immutable.

125
00:06:37,440 --> 00:06:43,290
I will actually delete this
for a moment but let's

126
00:06:43,290 --> 00:06:48,640
imagine that we want to add
an element for some reason,

127
00:06:48,640 --> 00:06:52,230
or we want to alter
some of the elements.

128
00:06:52,230 --> 00:06:57,465
Let's say countries
and we say element 0.

129
00:06:57,465 --> 00:06:59,230
What if I thought, well,

130
00:06:59,230 --> 00:07:02,400
maybe I want to assign
a new value and I

131
00:07:02,400 --> 00:07:06,525
would like to say Brazil,

132
00:07:06,525 --> 00:07:11,250
the first element of this tuple.

133
00:07:11,250 --> 00:07:14,850
I changed my mind I
want it to be Brazil.

134
00:07:14,850 --> 00:07:18,070
Well, let's print

135
00:07:21,230 --> 00:07:26,470
our new tuple just to see the
result of that operation.

136
00:07:27,500 --> 00:07:29,730
You'll see that we actually

137
00:07:29,730 --> 00:07:31,910
get nothing in the console here.

138
00:07:31,910 --> 00:07:34,510
We get in fact, a blank canvas

139
00:07:34,510 --> 00:07:36,685
because we're hitting an error.

140
00:07:36,685 --> 00:07:39,660
What is happening behind
the scenes is that, well,

141
00:07:39,660 --> 00:07:41,680
because this data
structure is immutable,

142
00:07:41,680 --> 00:07:43,805
it doesn't take changes.

143
00:07:43,805 --> 00:07:47,160
It was declared
originally as USA,

144
00:07:47,160 --> 00:07:48,520
Canada, and Mexico.

145
00:07:48,520 --> 00:07:51,060
If we try to do
operations of this type,

146
00:07:51,060 --> 00:07:52,800
we will run into a problem.

147
00:07:52,800 --> 00:07:56,140
These operations are allowed
for a different kind

148
00:07:56,140 --> 00:07:57,560
of data structure
that we would learn

149
00:07:57,560 --> 00:07:59,140
in course 2 which is a list.

150
00:07:59,140 --> 00:08:00,760
We're going to spend
a lot of time in

151
00:08:00,760 --> 00:08:02,940
list because lists
are very powerful.

152
00:08:02,940 --> 00:08:05,480
But as we understand tuples

153
00:08:05,480 --> 00:08:07,940
as our first more
advanced data structure,

154
00:08:07,940 --> 00:08:10,600
we need to be aware that
there's some restrictions

155
00:08:10,600 --> 00:08:13,815
on how we can use the
data containing them.

156
00:08:13,815 --> 00:08:15,700
With that in mind, we're

157
00:08:15,700 --> 00:08:17,920
going to move to
the next lesson.