1 00:00:00,480 --> 00:00:02,900 Hello and welcome to this video. 2 00:00:02,910 --> 00:00:09,780 In this video I'm going to introduce you to what two tuples are in Python. 3 00:00:09,780 --> 00:00:18,310 What is a tuple a tuple is a list that cannot be changed in any way. 4 00:00:18,330 --> 00:00:20,730 It means they are immutable. 5 00:00:20,730 --> 00:00:23,730 They cannot be mutated or changed. 6 00:00:23,730 --> 00:00:29,000 The only thing you can do to a tuple once you've created it is delete it. 7 00:00:29,070 --> 00:00:35,550 You can create it you pulled by giving the tuple a name and just add in parentheses and placing the 8 00:00:35,550 --> 00:00:37,620 values inside parentheses. 9 00:00:37,620 --> 00:00:44,570 You can also use what is called a tuple constructor to create a tuple. 10 00:00:44,790 --> 00:00:52,290 Once you've created a tuple you can access the individual elements within a tuple by using the index 11 00:00:52,560 --> 00:00:53,730 number. 12 00:00:53,730 --> 00:00:56,820 The tuples have a zero based index. 13 00:00:56,820 --> 00:01:02,400 What that means is that the first value in the tuple is given an index of zero. 14 00:01:02,400 --> 00:01:07,230 The second is given an index of 1 and so on. 15 00:01:07,290 --> 00:01:16,230 Let me show you how you can create a very basic tuple with just a name and parentheses. 16 00:01:16,230 --> 00:01:25,740 I am using the text editor or code at Tom and I have created a empty python file on my desktop. 17 00:01:25,740 --> 00:01:31,710 I've coded my on this code tuples and I've given it a dot P Y extension. 18 00:01:31,740 --> 00:01:34,260 That's what makes a python file. 19 00:01:34,320 --> 00:01:39,580 So now let me begin by giving my tuple in name. 20 00:01:39,600 --> 00:01:47,520 Try to give you two pools descriptive name that illustrate or tells you what it is to pull store in. 21 00:01:47,520 --> 00:01:56,760 So I'm going to create a tuple I'm going to call it fruits and you'll see a course to sign and then 22 00:01:56,850 --> 00:01:58,150 add my parents. 23 00:01:58,230 --> 00:02:00,060 Is inside my parentheses. 24 00:02:00,060 --> 00:02:05,790 I will not add elements all the values I want to be in my tuple. 25 00:02:05,940 --> 00:02:11,300 So that is grapes to a comma and add another value. 26 00:02:11,310 --> 00:02:15,560 I'll say apples always separate the values with a comma. 27 00:02:15,630 --> 00:02:25,080 I'll just add one more and I'll make the berries so you can have as many items or elements as you wish 28 00:02:25,080 --> 00:02:25,890 to do so. 29 00:02:26,340 --> 00:02:33,810 So now this basically creates the tuples so I can just do a control s to save this file a feature on 30 00:02:33,810 --> 00:02:34,340 a Mac. 31 00:02:34,350 --> 00:02:38,760 You can do a command and press to save it. 32 00:02:38,760 --> 00:02:46,010 Once you have created it tuple you can use what is called a for loop to loop through. 33 00:02:46,010 --> 00:02:50,210 That is go through the list and then print it out on the screen. 34 00:02:50,220 --> 00:02:51,750 So let's do that. 35 00:02:51,750 --> 00:02:59,640 So to add a for loop so that we can print the elements of our tuple we just type in for. 36 00:03:00,120 --> 00:03:10,380 I'm just going to use x to represent the values inside the tuple so I'll see X in fruits okay. 37 00:03:10,910 --> 00:03:13,310 Now to a colon. 38 00:03:13,410 --> 00:03:15,070 Press enter. 39 00:03:15,240 --> 00:03:20,760 Then I will use the print statement to print the values. 40 00:03:20,810 --> 00:03:27,710 Now just say X and what this should do is to print out the values or the elements on the screen. 41 00:03:27,720 --> 00:03:30,900 I'm just gonna do a control s to save. 42 00:03:30,900 --> 00:03:34,980 Now if I open up my command prompt this is my command prompt. 43 00:03:34,980 --> 00:03:41,490 If you're on a Mac you need to open up your terminal and navigate to where you've told your file I've 44 00:03:41,490 --> 00:03:43,650 stored my file on my desktop. 45 00:03:43,830 --> 00:03:46,910 So I've navigated to it here you can see my desktop. 46 00:03:46,920 --> 00:03:53,580 So all I need to do is just type in the name of the my file which is my on this call. 47 00:03:53,760 --> 00:04:00,750 Pulls the p y and that should print the elements. 48 00:04:00,750 --> 00:04:06,430 You can see here is printed all the elements is looped through and then printed them on the screen. 49 00:04:06,540 --> 00:04:13,320 So that's how you can loop palm print elements from your tuples on the screen. 50 00:04:13,320 --> 00:04:21,690 You can access individual elements within a tuple by using the index number. 51 00:04:22,500 --> 00:04:29,700 So let's say I want to access the element berries from these fruits tuple. 52 00:04:29,880 --> 00:04:31,530 This is how I would do that. 53 00:04:31,560 --> 00:04:37,770 I'm going to use a print statement to print it out on the screen so I'll do print and inside the parentheses 54 00:04:37,770 --> 00:04:46,050 are passed in the name of my tuple and then within that I'll add the square bracket and add the index 55 00:04:46,140 --> 00:04:51,210 because the first element is grapes so that will have an index of 0. 56 00:04:51,210 --> 00:04:56,070 Apples will be 1 and then the berries will have an index of 2. 57 00:04:56,070 --> 00:05:03,900 So I'm targeting the berries so I'm just going to put a comment here on line two because I don't want 58 00:05:03,900 --> 00:05:05,670 that to be printed. 59 00:05:05,730 --> 00:05:08,480 I don't want that line of code to run. 60 00:05:08,640 --> 00:05:14,070 So the only line of code I want to run is lime for on line one. 61 00:05:14,130 --> 00:05:18,630 So once I've done that I just come here and do control s. 62 00:05:18,630 --> 00:05:27,090 Now if I open up my terminal and just let me clear my terminal and then I'll type in the name of the 63 00:05:27,090 --> 00:05:29,720 file which is my on the score. 64 00:05:29,870 --> 00:05:37,430 Pulls the p why I only expect it to print out the value of berries. 65 00:05:37,440 --> 00:05:45,110 As you can see here is printed out varies because that's what's inside my code pedestal. 66 00:05:45,180 --> 00:05:47,600 I've commented out line to line three. 67 00:05:47,610 --> 00:05:50,940 So the interpreter will ignore those two lines. 68 00:05:51,090 --> 00:05:56,930 So that's basically how you can access individual elements within a tuple. 69 00:05:56,940 --> 00:05:58,230 Thanks for watching. 70 00:05:58,230 --> 00:05:58,970 Bye for now.