1 00:00:00,850 --> 00:00:03,660 Hello and welcome to this video. 2 00:00:03,700 --> 00:00:11,370 In this video I'm going to show you how to create a tuple using a tuple constructor. 3 00:00:11,380 --> 00:00:20,830 This involves using double parent theses to create a tuple using a tuple constructor or you first need 4 00:00:20,920 --> 00:00:22,450 to give the tuple a name. 5 00:00:22,450 --> 00:00:29,830 So I'm going to create a tuple I'm going to call it animals and then I use the equals to and then I'll 6 00:00:29,830 --> 00:00:36,730 type in the word tuple and then I'll have double parentheses. 7 00:00:37,030 --> 00:00:45,970 And inside the inner parentheses is where I will add the values I want to be in my two pulsars say a 8 00:00:45,990 --> 00:00:49,360 lion under a comma add another one. 9 00:00:49,390 --> 00:00:58,480 I'll say Taiga do comma and add one more let's make this one A. 10 00:01:00,440 --> 00:01:09,520 So that basically creates the tuple using a tuple constructor. 11 00:01:09,530 --> 00:01:17,330 So again if you want to print this out on the screen we can do is use a for loop to look through them 12 00:01:17,390 --> 00:01:27,710 or we can just use the print statement to just print the tuple so we can just say print inside the parentheses 13 00:01:27,710 --> 00:01:29,110 for the print statement. 14 00:01:29,120 --> 00:01:36,680 We just type in the name of the tuple and then we save us the file using control s.. 15 00:01:36,730 --> 00:01:43,130 Now if I open my command prompt if you're on a Mac you need to use the terminal. 16 00:01:43,130 --> 00:01:49,770 So this is my command prompt I'm just going to type the name of a file which is on my desktop. 17 00:01:49,850 --> 00:01:56,050 You can see I've navigated to my desktop so I'll just call the file which is called My on this code 18 00:01:56,090 --> 00:02:05,960 tuples to p y that you just print out the values of the tuple so you can say it's got lion has got Tiger 19 00:02:06,500 --> 00:02:08,130 and it's got the bear. 20 00:02:08,210 --> 00:02:16,550 You can also access the individual values in the tuple again by using the index lion will have an index 21 00:02:16,550 --> 00:02:17,800 of 0. 22 00:02:17,810 --> 00:02:23,480 Tiger will have an index of 1 and they will have an index of. 23 00:02:23,720 --> 00:02:26,230 There is a special method you can use. 24 00:02:26,240 --> 00:02:33,080 If I wanted to find out the length of the items in my tuple I could just do a print statement or a print 25 00:02:34,100 --> 00:02:43,490 inside the parentheses I type in Len method and inside that I will just add the name of my tuple which 26 00:02:43,490 --> 00:02:45,560 is animals. 27 00:02:45,560 --> 00:02:47,390 So I'm just going to comment out. 28 00:02:47,420 --> 00:02:48,560 Line number two. 29 00:02:48,710 --> 00:02:52,340 I don't want line number two to be printed out. 30 00:02:52,340 --> 00:02:55,940 I just want the line number three executed. 31 00:02:55,940 --> 00:03:02,090 So just to control s to save and open up my command prompt. 32 00:03:02,090 --> 00:03:10,790 So I'm just going to clear this screen and now type in the name of the tuple which is my on the score 33 00:03:10,820 --> 00:03:16,460 tuples in the P Y press printer. 34 00:03:16,460 --> 00:03:23,810 I can see he is giving me is return 3 which means the length of the elements in my tuple is 3. 35 00:03:23,810 --> 00:03:27,390 So lie on 1 2 3. 36 00:03:27,560 --> 00:03:31,210 Once you create a true pool you cannot modify it. 37 00:03:31,220 --> 00:03:32,960 The only thing you can do is delete it. 38 00:03:33,440 --> 00:03:42,500 If for example if Pa tried to add to my tuple I will expect to get an error or say if I say animals 39 00:03:42,560 --> 00:03:52,210 which is the name of my tuples and I will use a method called add so to add. 40 00:03:52,490 --> 00:03:58,400 And then inside the parentheses I'll add the value of what I need to add. 41 00:03:58,400 --> 00:04:08,120 So let's say I want to add dog to that to pull just the way I was represented by I expect the code to 42 00:04:08,220 --> 00:04:12,990 error because you cannot change a tuple once you've created it. 43 00:04:13,040 --> 00:04:23,620 So if I do on a print statement here and say print animals I will expect this line of code to error 44 00:04:24,170 --> 00:04:30,820 because you cannot change the tuple once it has been created. 45 00:04:30,830 --> 00:04:33,440 So let me open up my command prompt again. 46 00:04:34,780 --> 00:04:36,860 I just typing. 47 00:04:36,920 --> 00:04:40,020 That's the name of the farm which is this file here. 48 00:04:40,100 --> 00:04:48,320 If I press Penta you can see it will execute this line of code which prints the lamp which is three 49 00:04:48,950 --> 00:04:57,530 but you can see the line for it did not execute it did not execute line 5 because of the error. 50 00:04:57,590 --> 00:05:05,700 Let me show you what the Arrow says is say attribute error tuple object has no attribute at. 51 00:05:05,820 --> 00:05:11,870 Okay so we cannot add to a tuple. 52 00:05:11,870 --> 00:05:14,020 You cannot also change the value. 53 00:05:14,030 --> 00:05:24,290 So let's say 5 put a comment here and then I come here a typing animals animals. 54 00:05:25,580 --> 00:05:26,490 All right. 55 00:05:26,900 --> 00:05:28,480 I say the. 56 00:05:28,520 --> 00:05:30,690 Let's say we have Lion horn in there. 57 00:05:30,710 --> 00:05:31,890 0. 58 00:05:32,020 --> 00:05:39,050 I want to change that to let's say a cheetah 59 00:05:42,690 --> 00:05:44,480 not sure that's the right spelling but. 60 00:05:44,930 --> 00:05:45,310 Okay. 61 00:05:45,330 --> 00:05:47,810 And then I'll save control. 62 00:05:47,930 --> 00:05:48,960 Yes. 63 00:05:49,360 --> 00:05:57,810 Now if I tried to run my python file again I expect there to be an error from line number five and six 64 00:05:58,260 --> 00:06:03,220 because I'm trying to modify an existing value but it's not going to let me do that. 65 00:06:03,240 --> 00:06:04,540 So I press enter. 66 00:06:04,980 --> 00:06:09,570 He can see Prince three here which is line three of the code. 67 00:06:09,600 --> 00:06:10,990 It executes that. 68 00:06:11,010 --> 00:06:11,440 OK. 69 00:06:11,460 --> 00:06:12,770 No problem. 70 00:06:12,840 --> 00:06:17,220 Line 4 I commented out I'm 5 and 6 giving me an error. 71 00:06:17,220 --> 00:06:24,880 He can see it says here type error or tuple object does not support item assignment. 72 00:06:25,020 --> 00:06:30,810 So I'm trying to reassign a different value to an item that's already in the list. 73 00:06:30,840 --> 00:06:32,160 It will not do that. 74 00:06:32,190 --> 00:06:38,700 The only thing you can do to a tuple once you've created a tuple is delete it and you can delete it 75 00:06:38,700 --> 00:06:45,850 just by using the keyword DL followed by space and then the name of the tuple you can see on line six 76 00:06:45,850 --> 00:06:46,470 here. 77 00:06:46,650 --> 00:06:48,460 I am deleting the tuple. 78 00:06:48,460 --> 00:06:51,960 I've also commented out all the other lines of code. 79 00:06:52,140 --> 00:06:56,600 That means those block of code will not be executed. 80 00:06:56,670 --> 00:07:00,530 So I'm just gonna do a control as to save now a file run this file. 81 00:07:00,540 --> 00:07:06,440 I expect to get an error to tell me that that does not exist. 82 00:07:06,480 --> 00:07:06,960 All right. 83 00:07:06,960 --> 00:07:08,940 So you can see here telling me here. 84 00:07:09,000 --> 00:07:17,910 Name arrow animals is not defined and that is because I have deleted the tuple on line six here. 85 00:07:17,970 --> 00:07:20,160 So that's it for this video. 86 00:07:20,160 --> 00:07:22,710 Thanks for watching and bye for now.