Hey, welcome to the bonus example of today.
In this example, you will learn about mutability data.
Mutability in Python, which is a concept you need to know.
So please don't miss this video.
Let's start.
I want you to open a Python script and write this line there.
So this line is a variable declaration, and that variable is equal to a list.
The list itself has three strings, one here, two and three.
So let's suppose that we had a folder here, and that folder has three text files with these names and
we want to change the dot of each file name to a dash.
So we're talking about doing something to each of them repeatedly.
In that case, we're thinking about a for loop.
So for file name in file names.
Press enter.
And so in the first iteration file name would be equal to that string.
So how do we replace the dot with a dash?
Let's open the python console to try things out here first.
So file name in the first iteration is equal to that string, right?
Now full name.
Square brackets.
One gives us access to the DOT, so we access the adult character.
Now in the code that we wrote today in our app.
You saw that we had this syntax to dos was a list and the number was an index, just like this index
here.
And new to do was the new item that replaced that existing item.
So can we use the same syntax here and say file name?
One is equal to a dash and expect that the dash will replace the dot.
Let's try this.
It doesn't work.
We get a type error and the type error says STR does not support item assignment.
So that syntax there and that syntax here is the same.
We are assigning an item, a new item to the lists in this case and to the string in this case.
But it seems like strings don't support item assignment, and that is because strings are immutable,
they cannot be changed and lists are mutable.
You can change the list.
So we had the to do list.
Clean it, throw it, and so on.
So to dos.
One.
Get it.
And so you see that to do is changed so get it is now part of the to do list.
So now the question is, how do we change something from a string?
Is it really impossible because strings are immutable?
No, it is not impossible to change it.
The workaround is to create a new string.
So instead of using file, name one, we use file name that replace a string method.
So file name three replace and you place as the first argument.
You place the character you want to replace and the new character which will replace the old character.
So if you press enter.
We will get this output.
So the character was replaced with a dash.
The other dot also was replaced with a dash.
That's a problem we will solve in a minute from now.
So let's ignore that issue for now.
Let's focus focus on the string mutability.
I know you are confused now because.
We got the new string here.
Yes, but if you check for name, you'll see that it still has the dot.
So what's happening here is that replace is a method which returns a new string.
You see, it returns to the new string.
But if you want to keep that string, you want to store it in a variable.
So you go here and say, file name new is equal to that.
Press enter.
And now that output will be associated with file name new.
So file name new is equal to that new string.
But you don't need to use a new variable.
You can also say file name.
A sequel to File name that's replaced and that variable will be reassigned to the new string.
So if you press that, you check file name.
Now file name has this these dashes instead of dots.
So that is how you change a string.
It's not exactly changing the string.
It's creating a new string and overwriting the variable with a new string.
Now, how to get rid of that second dash.
Well, you'll need to.
Add one here and one means only change the first occurrence of the dot.
If there are other occurrences in the string, don't change them, so that will fix it.
But now file name has the dash already, so that will not do anything.
So I'll not execute that.
I'll reassign.
File name, so I'll get that.
Copy it.
Execute it again and then.
Do that.
So that has the dots.
Now, if I do this and I check file name, I get file name with a dash in front.
So let's do the same here for file name and file names.
File name is equal to file name does replace.
Dot with dash only the first occurrence.
And then you can print the file name.
So run this.
And what we get is the new file names.
So we were able to change the first dot of each file name.
Now, in our example, we are simply printing these file names in the command line, but in a real scenario.
You'd want to use some methods such as file that rename and you place the new file name there and each
of the files, the actual files on disk will be renamed using that new file name, but that's something
we will learn later on.
So for now, let's simply focus on processing strings.
But as you get some clues, you see how strings are useful in real life.
So strings can be used to represent file names, and lists can be used to represent a list of file names.
So each delta type has its own use in real life.
You'll see more and more how all the core data types of python are used in real world applications,
which will build throughout the course.
Before I close this video, I want to mention again the aspect of mutability.
You could ask the question Why are strings immutable and why are lists mutable?
Why did the Python code developers choose to have these two types that way?
And here is the answer.
As a programmer, when you write programs, you will run across scenarios where you want to to be sure
that a string in a variable is not changed.
There could be different reasons why you might want that, depending on how the different parts of the
program are interacting.
So you really, really want that a string is not changed.
Now, that didn't make strings mutable because if you want to mutate to change a string, you have the
workaround that I showed you that you reassign the output of the method.
For example, replace you, reassign it with a new variable.
And so you have basically a new string which is changed.
So you have a workaround to deal with immutable text.
But if strings were mutable and you didn't want them to be mutable, then you wouldn't have any solutions.
And that brings us to the other question.
So why are lists mutable then?
Why didn't they make them immutable?
Well, it's true.
The same problem could exist when you create a program.
Perhaps you really, really want the list not to be changed.
But in that case, you shouldn't use a list.
You should use a triple.
A tuple is the immutable version of a list.
And I'll show you now how a tuple is created in Python.
So let me open a Python console.
So that is a list with square brackets.
If you want to create a tuple instead, what you do is you use round parentheses.
Instead of square brackets.
And then the syntax inside is the same as the list.
So you can just put all that.
Inside the temple here.
So all the items are divided by commas.
You press enter.
File names is now a tuple.
It has these parentheses.
You can check the type as well.
Type file names.
So it's a tuple.
Now it is possible to get the items of the table.
For example, file, name one.
Sorry.
File names, not file name.
File names is the variable holding the tuple.
So file names one.
You get reports The task.
Of course file names is not changed, even if you use the assignment operator.
You say file names.
One is equal to something new.
You press enter and you get the error.
That tuple does not support item assignment because the posts are immutable.
Likewise, the file names tuple doesn't have an append method because append also modifies the list.
So it's a method of lists, not a method of tuples.
The tuple doesn't even have such a method.
So that's the idea.
Strings are immutable and perhaps the Python code developers, maybe they will devise a new type later
in the future, which allows us to create mutable strings.
But for now, that's what it is.
So strings, police and temples.
So should you be using tuples or should you be using lists in your everyday programming?
The short answer is just use lists because they are more flexible so they can easily be changed.
You don't have to reassign them in a new variable.
That's why lists are the most popular when it comes to sequence types.
Types that stored different objects in them.
Tuples are useful in very special cases, and you'll know that when you get more advanced and you'll
see a scenario that, oh, you could say a topic would be useful in this case.
That's why it's good to be aware that tuples exist.
So that was my purpose showing you mutability and tuples in this video.
Thanks a lot for following and I'll talk to you later.