1
00:00:01,790 --> 00:00:04,450
Hey, welcome to the bonus example of today.

2
00:00:04,460 --> 00:00:08,570
In this example, you will learn about mutability data.

3
00:00:08,570 --> 00:00:12,320
Mutability in Python, which is a concept you need to know.

4
00:00:12,320 --> 00:00:14,300
So please don't miss this video.

5
00:00:15,240 --> 00:00:16,140
Let's start.

6
00:00:16,500 --> 00:00:20,940
I want you to open a Python script and write this line there.

7
00:00:20,940 --> 00:00:26,570
So this line is a variable declaration, and that variable is equal to a list.

8
00:00:26,580 --> 00:00:34,170
The list itself has three strings, one here, two and three.

9
00:00:35,250 --> 00:00:44,880
So let's suppose that we had a folder here, and that folder has three text files with these names and

10
00:00:44,880 --> 00:00:52,200
we want to change the dot of each file name to a dash.

11
00:00:52,950 --> 00:00:57,690
So we're talking about doing something to each of them repeatedly.

12
00:00:58,170 --> 00:01:02,340
In that case, we're thinking about a for loop.

13
00:01:02,340 --> 00:01:05,099
So for file name in file names.

14
00:01:07,610 --> 00:01:08,570
Press enter.

15
00:01:08,690 --> 00:01:15,260
And so in the first iteration file name would be equal to that string.

16
00:01:16,240 --> 00:01:20,320
So how do we replace the dot with a dash?

17
00:01:20,560 --> 00:01:24,150
Let's open the python console to try things out here first.

18
00:01:24,160 --> 00:01:29,530
So file name in the first iteration is equal to that string, right?

19
00:01:32,710 --> 00:01:34,150
Now full name.

20
00:01:36,110 --> 00:01:37,460
Square brackets.

21
00:01:37,460 --> 00:01:43,430
One gives us access to the DOT, so we access the adult character.

22
00:01:43,520 --> 00:01:47,300
Now in the code that we wrote today in our app.

23
00:01:47,630 --> 00:01:56,510
You saw that we had this syntax to dos was a list and the number was an index, just like this index

24
00:01:56,510 --> 00:01:56,990
here.

25
00:01:57,980 --> 00:02:03,130
And new to do was the new item that replaced that existing item.

26
00:02:03,140 --> 00:02:08,100
So can we use the same syntax here and say file name?

27
00:02:08,120 --> 00:02:14,300
One is equal to a dash and expect that the dash will replace the dot.

28
00:02:14,930 --> 00:02:16,100
Let's try this.

29
00:02:16,580 --> 00:02:17,780
It doesn't work.

30
00:02:19,370 --> 00:02:28,850
We get a type error and the type error says STR does not support item assignment.

31
00:02:29,180 --> 00:02:35,120
So that syntax there and that syntax here is the same.

32
00:02:35,120 --> 00:02:42,830
We are assigning an item, a new item to the lists in this case and to the string in this case.

33
00:02:42,830 --> 00:02:50,300
But it seems like strings don't support item assignment, and that is because strings are immutable,

34
00:02:50,300 --> 00:02:54,640
they cannot be changed and lists are mutable.

35
00:02:54,650 --> 00:02:56,180
You can change the list.

36
00:02:57,170 --> 00:02:59,180
So we had the to do list.

37
00:03:03,580 --> 00:03:07,180
Clean it, throw it, and so on.

38
00:03:07,180 --> 00:03:08,550
So to dos.

39
00:03:10,290 --> 00:03:11,100
One.

40
00:03:13,170 --> 00:03:13,870
Get it.

41
00:03:13,870 --> 00:03:20,050
And so you see that to do is changed so get it is now part of the to do list.

42
00:03:21,290 --> 00:03:26,990
So now the question is, how do we change something from a string?

43
00:03:27,020 --> 00:03:31,580
Is it really impossible because strings are immutable?

44
00:03:31,850 --> 00:03:34,290
No, it is not impossible to change it.

45
00:03:34,310 --> 00:03:37,670
The workaround is to create a new string.

46
00:03:39,730 --> 00:03:49,570
So instead of using file, name one, we use file name that replace a string method.

47
00:03:52,740 --> 00:03:58,140
So file name three replace and you place as the first argument.

48
00:03:58,140 --> 00:04:06,420
You place the character you want to replace and the new character which will replace the old character.

49
00:04:06,690 --> 00:04:08,280
So if you press enter.

50
00:04:10,440 --> 00:04:12,150
We will get this output.

51
00:04:12,180 --> 00:04:16,140
So the character was replaced with a dash.

52
00:04:17,070 --> 00:04:19,800
The other dot also was replaced with a dash.

53
00:04:19,829 --> 00:04:22,530
That's a problem we will solve in a minute from now.

54
00:04:22,530 --> 00:04:25,110
So let's ignore that issue for now.

55
00:04:25,110 --> 00:04:28,110
Let's focus focus on the string mutability.

56
00:04:28,590 --> 00:04:31,410
I know you are confused now because.

57
00:04:32,440 --> 00:04:34,060
We got the new string here.

58
00:04:34,660 --> 00:04:39,550
Yes, but if you check for name, you'll see that it still has the dot.

59
00:04:40,450 --> 00:04:46,750
So what's happening here is that replace is a method which returns a new string.

60
00:04:47,520 --> 00:04:50,160
You see, it returns to the new string.

61
00:04:50,490 --> 00:04:55,640
But if you want to keep that string, you want to store it in a variable.

62
00:04:55,650 --> 00:05:01,740
So you go here and say, file name new is equal to that.

63
00:05:02,700 --> 00:05:03,780
Press enter.

64
00:05:03,780 --> 00:05:08,720
And now that output will be associated with file name new.

65
00:05:08,730 --> 00:05:14,040
So file name new is equal to that new string.

66
00:05:15,420 --> 00:05:19,060
But you don't need to use a new variable.

67
00:05:19,080 --> 00:05:22,650
You can also say file name.

68
00:05:24,690 --> 00:05:32,070
A sequel to File name that's replaced and that variable will be reassigned to the new string.

69
00:05:32,640 --> 00:05:35,610
So if you press that, you check file name.

70
00:05:37,110 --> 00:05:41,130
Now file name has this these dashes instead of dots.

71
00:05:41,580 --> 00:05:46,500
So that is how you change a string.

72
00:05:47,600 --> 00:05:50,300
It's not exactly changing the string.

73
00:05:50,330 --> 00:05:55,940
It's creating a new string and overwriting the variable with a new string.

74
00:05:56,450 --> 00:05:59,220
Now, how to get rid of that second dash.

75
00:05:59,240 --> 00:06:01,160
Well, you'll need to.

76
00:06:03,340 --> 00:06:11,650
Add one here and one means only change the first occurrence of the dot.

77
00:06:12,520 --> 00:06:18,190
If there are other occurrences in the string, don't change them, so that will fix it.

78
00:06:18,460 --> 00:06:23,470
But now file name has the dash already, so that will not do anything.

79
00:06:23,470 --> 00:06:24,940
So I'll not execute that.

80
00:06:25,930 --> 00:06:28,090
I'll reassign.

81
00:06:30,220 --> 00:06:32,440
File name, so I'll get that.

82
00:06:33,190 --> 00:06:33,930
Copy it.

83
00:06:35,140 --> 00:06:37,990
Execute it again and then.

84
00:06:40,540 --> 00:06:41,380
Do that.

85
00:06:41,920 --> 00:06:43,630
So that has the dots.

86
00:06:43,660 --> 00:06:49,420
Now, if I do this and I check file name, I get file name with a dash in front.

87
00:06:50,580 --> 00:06:54,450
So let's do the same here for file name and file names.

88
00:06:54,450 --> 00:06:58,080
File name is equal to file name does replace.

89
00:07:00,260 --> 00:07:05,420
Dot with dash only the first occurrence.

90
00:07:07,890 --> 00:07:10,170
And then you can print the file name.

91
00:07:11,940 --> 00:07:13,170
So run this.

92
00:07:13,170 --> 00:07:16,020
And what we get is the new file names.

93
00:07:16,470 --> 00:07:20,280
So we were able to change the first dot of each file name.

94
00:07:20,430 --> 00:07:27,420
Now, in our example, we are simply printing these file names in the command line, but in a real scenario.

95
00:07:29,090 --> 00:07:37,100
You'd want to use some methods such as file that rename and you place the new file name there and each

96
00:07:37,100 --> 00:07:43,940
of the files, the actual files on disk will be renamed using that new file name, but that's something

97
00:07:43,940 --> 00:07:45,130
we will learn later on.

98
00:07:45,140 --> 00:07:50,480
So for now, let's simply focus on processing strings.

99
00:07:50,840 --> 00:07:57,590
But as you get some clues, you see how strings are useful in real life.

100
00:07:57,590 --> 00:08:05,570
So strings can be used to represent file names, and lists can be used to represent a list of file names.

101
00:08:06,270 --> 00:08:10,170
So each delta type has its own use in real life.

102
00:08:10,200 --> 00:08:18,150
You'll see more and more how all the core data types of python are used in real world applications,

103
00:08:18,150 --> 00:08:20,190
which will build throughout the course.

104
00:08:21,680 --> 00:08:26,180
Before I close this video, I want to mention again the aspect of mutability.

105
00:08:26,210 --> 00:08:32,120
You could ask the question Why are strings immutable and why are lists mutable?

106
00:08:33,600 --> 00:08:39,780
Why did the Python code developers choose to have these two types that way?

107
00:08:41,700 --> 00:08:42,929
And here is the answer.

108
00:08:43,620 --> 00:08:52,110
As a programmer, when you write programs, you will run across scenarios where you want to to be sure

109
00:08:52,110 --> 00:08:55,540
that a string in a variable is not changed.

110
00:08:55,560 --> 00:09:00,600
There could be different reasons why you might want that, depending on how the different parts of the

111
00:09:00,600 --> 00:09:02,090
program are interacting.

112
00:09:02,100 --> 00:09:06,930
So you really, really want that a string is not changed.

113
00:09:07,620 --> 00:09:13,710
Now, that didn't make strings mutable because if you want to mutate to change a string, you have the

114
00:09:13,710 --> 00:09:19,470
workaround that I showed you that you reassign the output of the method.

115
00:09:19,470 --> 00:09:23,010
For example, replace you, reassign it with a new variable.

116
00:09:23,010 --> 00:09:27,000
And so you have basically a new string which is changed.

117
00:09:27,000 --> 00:09:31,860
So you have a workaround to deal with immutable text.

118
00:09:31,860 --> 00:09:39,900
But if strings were mutable and you didn't want them to be mutable, then you wouldn't have any solutions.

119
00:09:40,530 --> 00:09:42,990
And that brings us to the other question.

120
00:09:42,990 --> 00:09:45,960
So why are lists mutable then?

121
00:09:45,960 --> 00:09:48,240
Why didn't they make them immutable?

122
00:09:48,930 --> 00:09:49,860
Well, it's true.

123
00:09:49,860 --> 00:09:53,940
The same problem could exist when you create a program.

124
00:09:53,940 --> 00:09:58,950
Perhaps you really, really want the list not to be changed.

125
00:09:59,070 --> 00:10:02,100
But in that case, you shouldn't use a list.

126
00:10:02,100 --> 00:10:03,810
You should use a triple.

127
00:10:04,230 --> 00:10:08,850
A tuple is the immutable version of a list.

128
00:10:08,850 --> 00:10:12,840
And I'll show you now how a tuple is created in Python.

129
00:10:13,590 --> 00:10:15,630
So let me open a Python console.

130
00:10:17,650 --> 00:10:20,710
So that is a list with square brackets.

131
00:10:20,920 --> 00:10:29,470
If you want to create a tuple instead, what you do is you use round parentheses.

132
00:10:30,560 --> 00:10:32,210
Instead of square brackets.

133
00:10:33,230 --> 00:10:36,410
And then the syntax inside is the same as the list.

134
00:10:37,510 --> 00:10:40,150
So you can just put all that.

135
00:10:41,550 --> 00:10:42,840
Inside the temple here.

136
00:10:44,940 --> 00:10:47,640
So all the items are divided by commas.

137
00:10:47,670 --> 00:10:48,930
You press enter.

138
00:10:50,100 --> 00:10:51,990
File names is now a tuple.

139
00:10:52,020 --> 00:10:53,470
It has these parentheses.

140
00:10:53,490 --> 00:10:54,990
You can check the type as well.

141
00:10:58,150 --> 00:10:59,320
Type file names.

142
00:10:59,560 --> 00:11:00,640
So it's a tuple.

143
00:11:02,110 --> 00:11:06,490
Now it is possible to get the items of the table.

144
00:11:06,490 --> 00:11:08,980
For example, file, name one.

145
00:11:10,140 --> 00:11:10,620
Sorry.

146
00:11:10,620 --> 00:11:12,420
File names, not file name.

147
00:11:12,840 --> 00:11:15,450
File names is the variable holding the tuple.

148
00:11:15,450 --> 00:11:16,860
So file names one.

149
00:11:16,890 --> 00:11:18,510
You get reports The task.

150
00:11:18,510 --> 00:11:25,290
Of course file names is not changed, even if you use the assignment operator.

151
00:11:25,950 --> 00:11:27,000
You say file names.

152
00:11:27,000 --> 00:11:30,510
One is equal to something new.

153
00:11:30,540 --> 00:11:34,290
You press enter and you get the error.

154
00:11:34,290 --> 00:11:39,570
That tuple does not support item assignment because the posts are immutable.

155
00:11:40,410 --> 00:11:50,430
Likewise, the file names tuple doesn't have an append method because append also modifies the list.

156
00:11:50,430 --> 00:11:54,030
So it's a method of lists, not a method of tuples.

157
00:11:54,030 --> 00:11:56,730
The tuple doesn't even have such a method.

158
00:11:57,760 --> 00:11:58,970
So that's the idea.

159
00:11:58,990 --> 00:12:08,110
Strings are immutable and perhaps the Python code developers, maybe they will devise a new type later

160
00:12:08,110 --> 00:12:13,180
in the future, which allows us to create mutable strings.

161
00:12:14,040 --> 00:12:15,750
But for now, that's what it is.

162
00:12:15,990 --> 00:12:18,750
So strings, police and temples.

163
00:12:20,330 --> 00:12:25,790
So should you be using tuples or should you be using lists in your everyday programming?

164
00:12:26,300 --> 00:12:33,290
The short answer is just use lists because they are more flexible so they can easily be changed.

165
00:12:33,410 --> 00:12:36,770
You don't have to reassign them in a new variable.

166
00:12:37,100 --> 00:12:42,390
That's why lists are the most popular when it comes to sequence types.

167
00:12:42,410 --> 00:12:47,180
Types that stored different objects in them.

168
00:12:47,720 --> 00:12:55,430
Tuples are useful in very special cases, and you'll know that when you get more advanced and you'll

169
00:12:55,430 --> 00:13:00,470
see a scenario that, oh, you could say a topic would be useful in this case.

170
00:13:01,190 --> 00:13:04,740
That's why it's good to be aware that tuples exist.

171
00:13:04,760 --> 00:13:09,470
So that was my purpose showing you mutability and tuples in this video.

172
00:13:09,710 --> 00:13:11,960
Thanks a lot for following and I'll talk to you later.

