WEBVTT

00:00.260 --> 00:06.350
Lists work well for storing collections of items that can change through the list of a program.

00:06.350 --> 00:12.380
So the ability to modify lists is particularly important when you are working with a list of users on

00:12.380 --> 00:14.870
a website or a list of characters in a game.

00:14.900 --> 00:20.300
So, however, sometimes you will want to create a list of items that cannot change.

00:20.300 --> 00:24.770
So tuples allows you to do just that.

00:25.160 --> 00:31.760
So Python refers to values that cannot change as immutable and immutable.

00:31.760 --> 00:33.650
List is called tuple.

00:33.680 --> 00:39.640
So a tuple is just like a list, except you use parentheses instead of a square braces.

00:39.650 --> 00:45.680
Once you define a tuple, you can access individual elements by using each item's index, just as you

00:45.680 --> 00:46.940
would for a list.

00:46.970 --> 00:48.670
For example, if we have a.

00:51.520 --> 00:52.360
Rectangle.

00:52.360 --> 00:54.580
That should always be a certain size.

00:54.580 --> 00:56.470
We can ensure that it is.

00:56.770 --> 01:00.700
Its size doesn't change by plotting the dimensions into a tuple.

01:00.790 --> 01:08.020
So my dimensions here, we will add 250 here and 100 here.

01:08.020 --> 01:13.450
And here we will print first, we will print dimension one and then we will print.

01:13.810 --> 01:14.130
Oops.

01:14.380 --> 01:15.340
Not like that.

01:15.790 --> 01:17.650
Of course we need to get the variable name.

01:17.650 --> 01:22.210
So my dimensions one here and here.

01:22.210 --> 01:27.790
As you can see here, we define the tuple dimensions using the parentheses instead of a square braces

01:27.790 --> 01:29.920
like we did in lists.

01:29.920 --> 01:35.650
So then we print each element in the tuple individually using the same syntax we have been using to

01:35.650 --> 01:37.180
access elements in a list.

01:37.180 --> 01:42.700
And as you can see here, we first printed to 250 and 200 after that.

01:42.700 --> 01:49.900
So this so and actually, let's see what happens if we try to change the item or change one of the items

01:49.900 --> 01:53.690
in the in the type in the tuple dimensions.

01:53.720 --> 01:58.400
Now here we will write my dimension actually, let's make name it my tuple, right?

01:58.400 --> 02:02.810
So it will more explanatory my tuple dimensions.

02:02.810 --> 02:07.820
And here we will use the my table dimensions equals zero.

02:07.850 --> 02:08.450
Make it.

02:08.450 --> 02:11.240
Let's make zero two.

02:12.460 --> 02:14.050
150 here.

02:14.080 --> 02:19.510
And after that, print this multiple dimensions zero.

02:20.770 --> 02:24.070
And after that my print, my tuple dimensions one.

02:25.320 --> 02:26.340
And here.

02:26.990 --> 02:29.270
So this is the error we got here, actually.

02:29.390 --> 02:30.530
Delete this for now.

02:30.530 --> 02:38.210
So this code tries to change the value of the first dimension, but Python returns a type error because

02:38.210 --> 02:43.220
we're trying to alter a tuple which can't be done to that type of object.

02:43.220 --> 02:49.520
And Python tells us we can't assign a new value to an item in a tuple here.

02:49.520 --> 02:53.420
As you can see here, Tuple object does not support item assignment.

02:53.420 --> 02:58.580
So this is a beneficial because we want to python raise an error when a line of code tries to change

02:58.580 --> 03:05.600
the dimensions of the rectangle and tuples are technically defined by the presence of a comma, so the

03:05.600 --> 03:08.540
parentheses make them look neater and more readable.

03:08.540 --> 03:15.350
So if you want to define a tuple with one element, you need to include trailing comma here.

03:15.500 --> 03:21.320
So if you want to define tuple, just one element, you need to include this trailing comma.

03:21.770 --> 03:25.400
So it doesn't often make sense to build a tuple with one element.

03:25.400 --> 03:29.880
But this can happen when tuples are generated automatically.

03:29.880 --> 03:37.050
So you can also loop through all values in a tuple, so you can loop over all the values in a tuple

03:37.050 --> 03:41.370
using just the for loop as we did in previous lectures with lists.

03:41.370 --> 03:46.740
So here now we will add new tuple.

03:46.740 --> 03:51.270
So my tuple or my car models.

03:51.300 --> 03:52.230
Models.

03:53.360 --> 04:01.160
My favorite, favorite car models that I will like forever.

04:01.160 --> 04:05.850
And this is not the best name for that, but I just explanatory purposes.

04:05.870 --> 04:10.400
And here we will add tuples that for example, I like BMW cars.

04:10.820 --> 04:13.520
I like the here.

04:13.520 --> 04:19.610
For example, I like Volkswagen, but the sporty sporty ones here, I like sports here.

04:19.610 --> 04:20.870
So that's it for.

04:21.990 --> 04:24.960
And here we will loop through all the topics in this lesson.

04:24.990 --> 04:28.110
So for so remember, this is not a list.

04:28.760 --> 04:31.170
This is actually let me here.

04:31.280 --> 04:34.340
So remember, keep in mind that this is not a list.

04:34.340 --> 04:36.350
This is tuple.

04:37.480 --> 04:38.890
This is tuple here.

04:39.070 --> 04:42.040
So here we will loop through all the items in this list.

04:42.130 --> 04:47.650
So for my my fave car.

04:49.680 --> 04:50.820
And here.

04:53.710 --> 04:54.310
We will do.

04:55.660 --> 05:00.580
In my favorite car models that I will like forever.

05:00.580 --> 05:04.810
And here we will print here my my fave car.

05:04.810 --> 05:06.370
And let's print this.

05:08.000 --> 05:10.760
And here, as you can see here, we loop through all the elements here.

05:10.760 --> 05:15.690
So Python returns all the elements in a tuple, just as it would for a list.

05:15.710 --> 05:18.350
So although you can modify a tuple.

05:18.350 --> 05:20.900
So that's it how it how this works here.

05:20.900 --> 05:28.070
So although you can't modify a tuple, you can assign a new value to a variable that represents a tuple.

05:28.070 --> 05:35.390
For example, if you wanted to change the dimensions of a previously created rectangle dimensions here,

05:35.390 --> 05:36.110
for example.

05:36.140 --> 05:37.250
250.

05:38.690 --> 05:40.340
150 and 100.

05:41.450 --> 05:45.230
And we will print the original dimensions here.

05:46.300 --> 05:48.310
Original dimensions.

05:48.340 --> 05:49.630
Dimensions here.

05:49.630 --> 05:53.890
And after that we will create a for loop dimension.

05:54.580 --> 05:57.430
Dimension in dimensions.

05:57.430 --> 06:01.900
And after that we will print.

06:02.740 --> 06:05.740
Here, we will print the dimension.

06:07.050 --> 06:08.190
Parental dimension.

06:09.360 --> 06:10.110
Dimension.

06:10.260 --> 06:12.870
And here we will.

06:12.930 --> 06:15.960
After that we will do again dimensions.

06:15.960 --> 06:17.460
We will change the dimensions here.

06:17.490 --> 06:19.260
Dimensions.

06:20.420 --> 06:24.500
And equals, for example, 502 hundred.

06:24.500 --> 06:25.010
Right.

06:25.220 --> 06:34.580
And after that, we will print this new line, modified dimensions, modified dimensions.

06:35.000 --> 06:37.700
And here we will create a new for loop.

06:37.700 --> 06:39.920
Actually, let's use double dot here.

06:40.700 --> 06:41.900
Make it look nicer.

06:42.030 --> 06:42.560
Right.

06:42.560 --> 06:44.820
So for dimension.

06:45.370 --> 06:46.430
Dimension.

06:47.510 --> 06:50.300
Dimension in dimensions.

06:50.300 --> 06:54.260
And here we will print dimension again.

06:54.290 --> 06:55.250
That's it.

06:55.490 --> 06:56.930
And here we will.

06:56.930 --> 06:58.670
Let's run it.

07:02.160 --> 07:04.530
And here this is our output.

07:04.530 --> 07:12.810
So the first four lines define the original tuple and the initial dimensions, and we then associate

07:12.810 --> 07:18.090
a new tuple with the variable dimensions and print the new value.

07:18.090 --> 07:24.390
And Python doesn't raise any error this time because resigning a variable is valid.

07:24.540 --> 07:31.980
So when compared with list tuples are simple data structures and use them when you want to store a set

07:31.980 --> 07:36.630
of values that should not be changed throughout the life of a program.
