Lists work well for storing collections of items that can change through the list of a program.
So the ability to modify lists is particularly important when you are working with a list of users on
a website or a list of characters in a game.
So, however, sometimes you will want to create a list of items that cannot change.
So tuples allows you to do just that.
So Python refers to values that cannot change as immutable and immutable.
List is called tuple.
So a tuple is just like a list, except you use parentheses instead of a square braces.
Once you define a tuple, you can access individual elements by using each item's index, just as you
would for a list.
For example, if we have a.
Rectangle.
That should always be a certain size.
We can ensure that it is.
Its size doesn't change by plotting the dimensions into a tuple.
So my dimensions here, we will add 250 here and 100 here.
And here we will print first, we will print dimension one and then we will print.
Oops.
Not like that.
Of course we need to get the variable name.
So my dimensions one here and here.
As you can see here, we define the tuple dimensions using the parentheses instead of a square braces
like we did in lists.
So then we print each element in the tuple individually using the same syntax we have been using to
access elements in a list.
And as you can see here, we first printed to 250 and 200 after that.
So this so and actually, let's see what happens if we try to change the item or change one of the items
in the in the type in the tuple dimensions.
Now here we will write my dimension actually, let's make name it my tuple, right?
So it will more explanatory my tuple dimensions.
And here we will use the my table dimensions equals zero.
Make it.
Let's make zero two.
150 here.
And after that, print this multiple dimensions zero.
And after that my print, my tuple dimensions one.
And here.
So this is the error we got here, actually.
Delete this for now.
So this code tries to change the value of the first dimension, but Python returns a type error because
we're trying to alter a tuple which can't be done to that type of object.
And Python tells us we can't assign a new value to an item in a tuple here.
As you can see here, Tuple object does not support item assignment.
So this is a beneficial because we want to python raise an error when a line of code tries to change
the dimensions of the rectangle and tuples are technically defined by the presence of a comma, so the
parentheses make them look neater and more readable.
So if you want to define a tuple with one element, you need to include trailing comma here.
So if you want to define tuple, just one element, you need to include this trailing comma.
So it doesn't often make sense to build a tuple with one element.
But this can happen when tuples are generated automatically.
So you can also loop through all values in a tuple, so you can loop over all the values in a tuple
using just the for loop as we did in previous lectures with lists.
So here now we will add new tuple.
So my tuple or my car models.
Models.
My favorite, favorite car models that I will like forever.
And this is not the best name for that, but I just explanatory purposes.
And here we will add tuples that for example, I like BMW cars.
I like the here.
For example, I like Volkswagen, but the sporty sporty ones here, I like sports here.
So that's it for.
And here we will loop through all the topics in this lesson.
So for so remember, this is not a list.
This is actually let me here.
So remember, keep in mind that this is not a list.
This is tuple.
This is tuple here.
So here we will loop through all the items in this list.
So for my my fave car.
And here.
We will do.
In my favorite car models that I will like forever.
And here we will print here my my fave car.
And let's print this.
And here, as you can see here, we loop through all the elements here.
So Python returns all the elements in a tuple, just as it would for a list.
So although you can modify a tuple.
So that's it how it how this works here.
So although you can't modify a tuple, you can assign a new value to a variable that represents a tuple.
For example, if you wanted to change the dimensions of a previously created rectangle dimensions here,
for example.
250.
150 and 100.
And we will print the original dimensions here.
Original dimensions.
Dimensions here.
And after that we will create a for loop dimension.
Dimension in dimensions.
And after that we will print.
Here, we will print the dimension.
Parental dimension.
Dimension.
And here we will.
After that we will do again dimensions.
We will change the dimensions here.
Dimensions.
And equals, for example, 502 hundred.
Right.
And after that, we will print this new line, modified dimensions, modified dimensions.
And here we will create a new for loop.
Actually, let's use double dot here.
Make it look nicer.
Right.
So for dimension.
Dimension.
Dimension in dimensions.
And here we will print dimension again.
That's it.
And here we will.
Let's run it.
And here this is our output.
So the first four lines define the original tuple and the initial dimensions, and we then associate
a new tuple with the variable dimensions and print the new value.
And Python doesn't raise any error this time because resigning a variable is valid.
So when compared with list tuples are simple data structures and use them when you want to store a set
of values that should not be changed throughout the life of a program.