1 00:00:00,260 --> 00:00:06,350 Lists work well for storing collections of items that can change through the list of a program. 2 00:00:06,350 --> 00:00:12,380 So the ability to modify lists is particularly important when you are working with a list of users on 3 00:00:12,380 --> 00:00:14,870 a website or a list of characters in a game. 4 00:00:14,900 --> 00:00:20,300 So, however, sometimes you will want to create a list of items that cannot change. 5 00:00:20,300 --> 00:00:24,770 So tuples allows you to do just that. 6 00:00:25,160 --> 00:00:31,760 So Python refers to values that cannot change as immutable and immutable. 7 00:00:31,760 --> 00:00:33,650 List is called tuple. 8 00:00:33,680 --> 00:00:39,640 So a tuple is just like a list, except you use parentheses instead of a square braces. 9 00:00:39,650 --> 00:00:45,680 Once you define a tuple, you can access individual elements by using each item's index, just as you 10 00:00:45,680 --> 00:00:46,940 would for a list. 11 00:00:46,970 --> 00:00:48,670 For example, if we have a. 12 00:00:51,520 --> 00:00:52,360 Rectangle. 13 00:00:52,360 --> 00:00:54,580 That should always be a certain size. 14 00:00:54,580 --> 00:00:56,470 We can ensure that it is. 15 00:00:56,770 --> 00:01:00,700 Its size doesn't change by plotting the dimensions into a tuple. 16 00:01:00,790 --> 00:01:08,020 So my dimensions here, we will add 250 here and 100 here. 17 00:01:08,020 --> 00:01:13,450 And here we will print first, we will print dimension one and then we will print. 18 00:01:13,810 --> 00:01:14,130 Oops. 19 00:01:14,380 --> 00:01:15,340 Not like that. 20 00:01:15,790 --> 00:01:17,650 Of course we need to get the variable name. 21 00:01:17,650 --> 00:01:22,210 So my dimensions one here and here. 22 00:01:22,210 --> 00:01:27,790 As you can see here, we define the tuple dimensions using the parentheses instead of a square braces 23 00:01:27,790 --> 00:01:29,920 like we did in lists. 24 00:01:29,920 --> 00:01:35,650 So then we print each element in the tuple individually using the same syntax we have been using to 25 00:01:35,650 --> 00:01:37,180 access elements in a list. 26 00:01:37,180 --> 00:01:42,700 And as you can see here, we first printed to 250 and 200 after that. 27 00:01:42,700 --> 00: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 28 00:01:49,900 --> 00:01:53,690 in the in the type in the tuple dimensions. 29 00:01:53,720 --> 00:01:58,400 Now here we will write my dimension actually, let's make name it my tuple, right? 30 00:01:58,400 --> 00:02:02,810 So it will more explanatory my tuple dimensions. 31 00:02:02,810 --> 00:02:07,820 And here we will use the my table dimensions equals zero. 32 00:02:07,850 --> 00:02:08,450 Make it. 33 00:02:08,450 --> 00:02:11,240 Let's make zero two. 34 00:02:12,460 --> 00:02:14,050 150 here. 35 00:02:14,080 --> 00:02:19,510 And after that, print this multiple dimensions zero. 36 00:02:20,770 --> 00:02:24,070 And after that my print, my tuple dimensions one. 37 00:02:25,320 --> 00:02:26,340 And here. 38 00:02:26,990 --> 00:02:29,270 So this is the error we got here, actually. 39 00:02:29,390 --> 00:02:30,530 Delete this for now. 40 00:02:30,530 --> 00:02:38,210 So this code tries to change the value of the first dimension, but Python returns a type error because 41 00:02:38,210 --> 00:02:43,220 we're trying to alter a tuple which can't be done to that type of object. 42 00:02:43,220 --> 00:02:49,520 And Python tells us we can't assign a new value to an item in a tuple here. 43 00:02:49,520 --> 00:02:53,420 As you can see here, Tuple object does not support item assignment. 44 00:02:53,420 --> 00:02:58,580 So this is a beneficial because we want to python raise an error when a line of code tries to change 45 00:02:58,580 --> 00:03:05,600 the dimensions of the rectangle and tuples are technically defined by the presence of a comma, so the 46 00:03:05,600 --> 00:03:08,540 parentheses make them look neater and more readable. 47 00:03:08,540 --> 00:03:15,350 So if you want to define a tuple with one element, you need to include trailing comma here. 48 00:03:15,500 --> 00:03:21,320 So if you want to define tuple, just one element, you need to include this trailing comma. 49 00:03:21,770 --> 00:03:25,400 So it doesn't often make sense to build a tuple with one element. 50 00:03:25,400 --> 00:03:29,880 But this can happen when tuples are generated automatically. 51 00:03:29,880 --> 00: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 52 00:03:37,050 --> 00:03:41,370 using just the for loop as we did in previous lectures with lists. 53 00:03:41,370 --> 00:03:46,740 So here now we will add new tuple. 54 00:03:46,740 --> 00:03:51,270 So my tuple or my car models. 55 00:03:51,300 --> 00:03:52,230 Models. 56 00:03:53,360 --> 00:04:01,160 My favorite, favorite car models that I will like forever. 57 00:04:01,160 --> 00:04:05,850 And this is not the best name for that, but I just explanatory purposes. 58 00:04:05,870 --> 00:04:10,400 And here we will add tuples that for example, I like BMW cars. 59 00:04:10,820 --> 00:04:13,520 I like the here. 60 00:04:13,520 --> 00:04:19,610 For example, I like Volkswagen, but the sporty sporty ones here, I like sports here. 61 00:04:19,610 --> 00:04:20,870 So that's it for. 62 00:04:21,990 --> 00:04:24,960 And here we will loop through all the topics in this lesson. 63 00:04:24,990 --> 00:04:28,110 So for so remember, this is not a list. 64 00:04:28,760 --> 00:04:31,170 This is actually let me here. 65 00:04:31,280 --> 00:04:34,340 So remember, keep in mind that this is not a list. 66 00:04:34,340 --> 00:04:36,350 This is tuple. 67 00:04:37,480 --> 00:04:38,890 This is tuple here. 68 00:04:39,070 --> 00:04:42,040 So here we will loop through all the items in this list. 69 00:04:42,130 --> 00:04:47,650 So for my my fave car. 70 00:04:49,680 --> 00:04:50,820 And here. 71 00:04:53,710 --> 00:04:54,310 We will do. 72 00:04:55,660 --> 00:05:00,580 In my favorite car models that I will like forever. 73 00:05:00,580 --> 00:05:04,810 And here we will print here my my fave car. 74 00:05:04,810 --> 00:05:06,370 And let's print this. 75 00:05:08,000 --> 00:05:10,760 And here, as you can see here, we loop through all the elements here. 76 00:05:10,760 --> 00:05:15,690 So Python returns all the elements in a tuple, just as it would for a list. 77 00:05:15,710 --> 00:05:18,350 So although you can modify a tuple. 78 00:05:18,350 --> 00:05:20,900 So that's it how it how this works here. 79 00:05:20,900 --> 00:05:28,070 So although you can't modify a tuple, you can assign a new value to a variable that represents a tuple. 80 00:05:28,070 --> 00:05:35,390 For example, if you wanted to change the dimensions of a previously created rectangle dimensions here, 81 00:05:35,390 --> 00:05:36,110 for example. 82 00:05:36,140 --> 00:05:37,250 250. 83 00:05:38,690 --> 00:05:40,340 150 and 100. 84 00:05:41,450 --> 00:05:45,230 And we will print the original dimensions here. 85 00:05:46,300 --> 00:05:48,310 Original dimensions. 86 00:05:48,340 --> 00:05:49,630 Dimensions here. 87 00:05:49,630 --> 00:05:53,890 And after that we will create a for loop dimension. 88 00:05:54,580 --> 00:05:57,430 Dimension in dimensions. 89 00:05:57,430 --> 00:06:01,900 And after that we will print. 90 00:06:02,740 --> 00:06:05,740 Here, we will print the dimension. 91 00:06:07,050 --> 00:06:08,190 Parental dimension. 92 00:06:09,360 --> 00:06:10,110 Dimension. 93 00:06:10,260 --> 00:06:12,870 And here we will. 94 00:06:12,930 --> 00:06:15,960 After that we will do again dimensions. 95 00:06:15,960 --> 00:06:17,460 We will change the dimensions here. 96 00:06:17,490 --> 00:06:19,260 Dimensions. 97 00:06:20,420 --> 00:06:24,500 And equals, for example, 502 hundred. 98 00:06:24,500 --> 00:06:25,010 Right. 99 00:06:25,220 --> 00:06:34,580 And after that, we will print this new line, modified dimensions, modified dimensions. 100 00:06:35,000 --> 00:06:37,700 And here we will create a new for loop. 101 00:06:37,700 --> 00:06:39,920 Actually, let's use double dot here. 102 00:06:40,700 --> 00:06:41,900 Make it look nicer. 103 00:06:42,030 --> 00:06:42,560 Right. 104 00:06:42,560 --> 00:06:44,820 So for dimension. 105 00:06:45,370 --> 00:06:46,430 Dimension. 106 00:06:47,510 --> 00:06:50,300 Dimension in dimensions. 107 00:06:50,300 --> 00:06:54,260 And here we will print dimension again. 108 00:06:54,290 --> 00:06:55,250 That's it. 109 00:06:55,490 --> 00:06:56,930 And here we will. 110 00:06:56,930 --> 00:06:58,670 Let's run it. 111 00:07:02,160 --> 00:07:04,530 And here this is our output. 112 00:07:04,530 --> 00:07:12,810 So the first four lines define the original tuple and the initial dimensions, and we then associate 113 00:07:12,810 --> 00:07:18,090 a new tuple with the variable dimensions and print the new value. 114 00:07:18,090 --> 00:07:24,390 And Python doesn't raise any error this time because resigning a variable is valid. 115 00:07:24,540 --> 00:07:31,980 So when compared with list tuples are simple data structures and use them when you want to store a set 116 00:07:31,980 --> 00:07:36,630 of values that should not be changed throughout the life of a program.