1 00:00:00,000 --> 00:00:03,200 [no audio] 2 00:00:03,200 --> 00:00:04,400 Friends, in this video 3 00:00:04,500 --> 00:00:07,300 we are going to discuss about the next data structure, 4 00:00:07,300 --> 00:00:09,900 that is tuple data structure, right. 5 00:00:10,600 --> 00:00:13,400 See let me show you how to work with tuple, 6 00:00:13,400 --> 00:00:16,200 I mean how to define, and how to access your values of your 7 00:00:16,200 --> 00:00:19,400 tuple, right, how to do some operations on your tuple data 8 00:00:19,400 --> 00:00:21,200 structure. First thing 9 00:00:21,200 --> 00:00:22,200 let me define. 10 00:00:22,500 --> 00:00:23,700 So how to define your 11 00:00:23,700 --> 00:00:25,500 [no audio] 12 00:00:25,500 --> 00:00:27,300 tuple, right? 13 00:00:27,300 --> 00:00:30,100 See guys you can define your tuple, you know, 14 00:00:30,100 --> 00:00:33,900 like 'my_tuple', 'my_tuple', any variable name you can 15 00:00:33,900 --> 00:00:38,000 take but I am taking simply 'my_tuple', (3, 4, 5), 16 00:00:38,000 --> 00:00:41,600 and if you want to define any empty tuple, right, 17 00:00:41,600 --> 00:00:44,300 [no audio] 18 00:00:44,300 --> 00:00:46,300 'my_empty_tuple=', this set. 19 00:00:47,800 --> 00:00:51,900 Right. Now, let me save this as some python script, and I want 20 00:00:51,900 --> 00:00:53,100 to run something here, 21 00:00:54,300 --> 00:00:55,400 so 'working_with 22 00:00:55,400 --> 00:01:00,000 [no audio] 23 00:01:00,000 --> 00:01:01,000 _tuple'. 24 00:01:02,000 --> 00:01:03,000 Fine. 25 00:01:03,400 --> 00:01:08,400 So first thing, how you can print your tuple? Simply by variable 26 00:01:08,400 --> 00:01:12,600 name. See the output. Or how you can print your empty tuple? 27 00:01:12,800 --> 00:01:14,700 Whatever the variable you have for that, 28 00:01:14,900 --> 00:01:16,900 that's it. Now see the output. 29 00:01:16,900 --> 00:01:18,900 [no audio] 30 00:01:18,900 --> 00:01:22,500 Right. Now, let me convert your 31 00:01:22,500 --> 00:01:23,500 tuple into Boolean. 32 00:01:23,700 --> 00:01:26,500 So first I am going to convert my empty tuple into Boolean. 33 00:01:26,500 --> 00:01:30,200 You know the empty list if you convert into tuple, what you 34 00:01:30,200 --> 00:01:34,400 are going to get? The Boolean data as 'False'. Same way here 35 00:01:34,700 --> 00:01:37,100 I am converting my empty tuple into Boolean. 36 00:01:37,100 --> 00:01:38,400 Now you're getting 'False'. 37 00:01:39,800 --> 00:01:41,400 Let me convert some non- 38 00:01:41,400 --> 00:01:45,200 empty tuple into Boolean. Non-empty is nothing but already you have 39 00:01:45,200 --> 00:01:46,200 your tuple, right? 40 00:01:47,400 --> 00:01:50,900 See the output. 'True', right. 41 00:01:50,900 --> 00:01:57,200 So if we convert any empty tuple data into a Boolean you 42 00:01:57,200 --> 00:02:01,700 will get 'False', or if you convert any non-empty tuple data into Boolean 43 00:02:01,700 --> 00:02:03,100 you will get always 'True'. 44 00:02:04,000 --> 00:02:05,300 Right. Fine. 45 00:02:05,500 --> 00:02:06,500 The next thing is, 46 00:02:06,500 --> 00:02:08,500 [no audio] 47 00:02:08,500 --> 00:02:10,500 so I want to define a tuple 48 00:02:10,500 --> 00:02:12,699 [no audio] 49 00:02:12,699 --> 00:02:15,900 such that 'my_tuple' consists of different values, 50 00:02:16,000 --> 00:02:18,400 and in that one of the value is a list. 51 00:02:18,400 --> 00:02:23,400 Yes, you can. I mean suppose sometimes if you, based on requirement, 52 00:02:23,400 --> 00:02:25,200 you can also define your tuple in this way. 53 00:02:25,400 --> 00:02:29,800 Let's say (3, 4,), instead of 5, the third element 54 00:02:29,800 --> 00:02:32,400 I am taking as a list, no problem you can take it. 55 00:02:32,400 --> 00:02:35,100 [no audio] 56 00:02:35,100 --> 00:02:36,400 Right. Now, let me define, 57 00:02:36,400 --> 00:02:40,500 let me print my new tuple data. See the output, what you are getting. 58 00:02:40,500 --> 00:02:43,400 [no audio] 59 00:02:43,400 --> 00:02:47,100 Now guys, you know how to access your elements from your 60 00:02:47,100 --> 00:02:51,700 list based on index, the same concept is applicable even while 61 00:02:51,700 --> 00:02:53,000 working with your tuple. 62 00:02:53,500 --> 00:02:56,200 You can access particular value from your tuple. 63 00:02:56,200 --> 00:03:00,300 Let's say I want to access number 4. So, you know, if you 64 00:03:00,300 --> 00:03:03,900 go with the positive index, the number 4 index is 1, right 65 00:03:03,900 --> 00:03:10,100 because 3 is 0, then 4 is 1, then this entire thing is one element guys. 66 00:03:10,500 --> 00:03:13,200 This entire thing is one element for your main tuple. 67 00:03:13,600 --> 00:03:17,500 So that's why for this entire list, index is 2. Now, 68 00:03:17,500 --> 00:03:20,300 this is 3 and this is 4. 69 00:03:21,300 --> 00:03:28,300 Right. Now let me print something, 'print', your tuple with index 0. 70 00:03:28,300 --> 00:03:32,300 [no audio] 71 00:03:32,300 --> 00:03:34,400 See the output, what you are getting. You are getting first 72 00:03:34,400 --> 00:03:40,300 value. Let me print tuple value on index 1. Now see the 73 00:03:40,300 --> 00:03:41,200 output what you are getting. 74 00:03:42,600 --> 00:03:45,800 Let me print the index value 2 from your tuple. 75 00:03:46,100 --> 00:03:49,200 That is the list of value. See the output what you are getting, 76 00:03:49,700 --> 00:03:55,500 [5, 6, 7]. Now from this if you want to get, right, suppose 77 00:03:55,500 --> 00:03:59,600 6, number 6 I need to get. So if you want to get 6, first 78 00:03:59,600 --> 00:04:02,000 thing you have to get this element, that is the list. 79 00:04:02,000 --> 00:04:05,800 Yes, we are getting that with the help of index 2. Now from this, 80 00:04:06,000 --> 00:04:07,800 from this you need 6 value. 81 00:04:07,800 --> 00:04:12,600 That means again you have to take index 1. Now see the output 82 00:04:12,600 --> 00:04:16,000 what you are going to get, 6. So be clear guys. 83 00:04:16,000 --> 00:04:19,500 If you want to access digit 6, first of all from your main 84 00:04:19,500 --> 00:04:22,600 tuple, this is your main tuple, from that tuple first 85 00:04:22,600 --> 00:04:24,600 you have to get this element. To get that element 86 00:04:24,600 --> 00:04:29,100 we are taking index as 2, because positive index 0, 1, 2. Now, 87 00:04:29,100 --> 00:04:32,700 this is like one list, right. From this list 88 00:04:32,700 --> 00:04:35,100 if you want to get second element, you have to take index as 1. 89 00:04:35,300 --> 00:04:36,200 That's what I did. 90 00:04:36,200 --> 00:04:38,100 [no audio] 91 00:04:38,100 --> 00:04:44,900 Fine. Now, let me assign from my tuple, index 0 value something 92 00:04:44,900 --> 00:04:47,100 is there, I want to modify that with some other value 93 00:04:47,100 --> 00:04:50,300 let's say in this way. Observe that whether it is possible or 94 00:04:50,300 --> 00:04:52,300 not. You are getting an error. 95 00:04:53,700 --> 00:04:57,200 'tuple' object does not support item assignment, that means 96 00:04:57,200 --> 00:05:00,800 once if you define the values in your tuple, you cannot 97 00:05:00,800 --> 00:05:04,200 change any values or item in your tuple. 98 00:05:04,400 --> 00:05:09,300 That means tuples are immutable. Assignment modifying the 99 00:05:09,300 --> 00:05:11,100 part of your tuple is not possible. 100 00:05:11,100 --> 00:05:14,000 [no audio] 101 00:05:14,000 --> 00:05:17,900 But modifying the entire data is possible, like your 102 00:05:17,900 --> 00:05:21,100 strings also, right. Even in your strings you can modify entire 103 00:05:21,100 --> 00:05:23,800 string, but you cannot modify the part of your string. 104 00:05:24,300 --> 00:05:27,100 Same rule is applicable with your tuple as well. 105 00:05:27,900 --> 00:05:31,400 Right. So it's very, very important guys. Tuple 106 00:05:32,900 --> 00:05:39,500 and strings are immutable. That means once if you define you 107 00:05:39,500 --> 00:05:45,200 cannot change the part of your tuple or string, that's it. 108 00:05:46,600 --> 00:05:50,100 Now tuple is not able to do, I mean you cannot 109 00:05:50,100 --> 00:05:52,400 perform any operation such that 110 00:05:53,600 --> 00:05:56,500 whatever the operation you are using, right, that operation 111 00:05:56,500 --> 00:05:58,300 is not going to modify your tuple. 112 00:05:59,100 --> 00:06:01,500 That's why you are saying tuples are immutable. 113 00:06:02,100 --> 00:06:04,700 That's why if you see the operations on your tuple, there 114 00:06:04,700 --> 00:06:07,300 are not much operations on your tuple. 115 00:06:07,700 --> 00:06:11,500 Let's say I am defining tuple called (4, 5), and see 116 00:06:11,500 --> 00:06:15,200 the operations on your tuple, only 'count', and 'index' operations. 117 00:06:15,200 --> 00:06:18,400 You don't have like append, insert, update, 118 00:06:18,400 --> 00:06:25,800 I mean top, remove, extend, those all are there with your list, 119 00:06:25,800 --> 00:06:29,000 right? Let's say I have a list of data, 120 00:06:29,000 --> 00:06:31,000 [no audio] 121 00:06:31,000 --> 00:06:33,800 so on that I am trying to find the list of operations. See 122 00:06:33,800 --> 00:06:36,700 that you have index, extend, sorry, index 123 00:06:36,700 --> 00:06:39,500 anyway it is there with you in your tuple also, right, index and count. 124 00:06:39,500 --> 00:06:41,500 Other than that, you don't have any operations like remove, 125 00:06:41,500 --> 00:06:45,700 sort, right, insert, extend, copy. 126 00:06:45,700 --> 00:06:47,500 [no audio] 127 00:06:47,500 --> 00:06:50,500 So that means, once if you define a tuple you cannot modify 128 00:06:50,500 --> 00:06:53,800 it, right. Now, you know how to use your 'count' and 'index'. 129 00:06:53,800 --> 00:06:57,900 What is the purpose of 'count'? If you have a tuple like this, 130 00:06:58,400 --> 00:06:59,300 something like this, 131 00:07:00,300 --> 00:07:03,200 so how many times particular letter is there, particular number 132 00:07:03,200 --> 00:07:05,800 is there, particular data is there in a given tuple? 133 00:07:05,800 --> 00:07:08,200 Let's say I want to find 5, how many times it is there? 134 00:07:09,300 --> 00:07:13,200 Right. Let's say I define one more 5 now, and check it, 2 times. 135 00:07:14,000 --> 00:07:16,000 Then what is the index of number 5? 136 00:07:17,500 --> 00:07:22,800 0. So is there any number 5 after index 1, sorry after 137 00:07:22,800 --> 00:07:25,800 index 0? Because very first index is number 5, right? 138 00:07:26,300 --> 00:07:27,400 I don't want to look for this 139 00:07:27,400 --> 00:07:28,600 other than that. From here 140 00:07:28,600 --> 00:07:31,200 I want to look, is there any 5, then provide the starting 141 00:07:31,200 --> 00:07:32,800 index from where you want to look? 142 00:07:33,800 --> 00:07:35,800 Yes, it is there somewhere in fifth position. 143 00:07:36,800 --> 00:07:39,600 That's it, and you can find length of your tuple, guys. 144 00:07:39,600 --> 00:07:43,100 See that six letters are there, I mean six data are there in your tuple. 145 00:07:44,400 --> 00:07:48,800 So length is applicable to your strings, list, and tuple, right? 146 00:07:48,900 --> 00:07:50,200 Let me show you once again. 147 00:07:50,500 --> 00:07:54,200 If it is a 'list' then length of your, sorry, if it is a string, 148 00:07:54,500 --> 00:07:55,700 then length of your string. 149 00:07:56,400 --> 00:07:58,300 Let's define some list, 150 00:07:59,700 --> 00:08:01,200 then length of your list. 151 00:08:01,800 --> 00:08:03,300 Let's define some tuple, 152 00:08:03,300 --> 00:08:06,200 [no audio] 153 00:08:06,200 --> 00:08:08,600 then what is the length of your tuple? That's it. 154 00:08:08,600 --> 00:08:10,200 [no audio] 155 00:08:10,200 --> 00:08:14,600 Okay. So guys you can access based on index your tuple 156 00:08:14,600 --> 00:08:17,200 values as your list, right? 157 00:08:17,300 --> 00:08:19,700 I mean already we've done this thing 158 00:08:19,700 --> 00:08:22,200 no, let me do it once again. 159 00:08:22,200 --> 00:08:26,900 [no audio] 160 00:08:26,900 --> 00:08:31,200 Come here. Let me define 'my_tuple' in this way. 161 00:08:32,700 --> 00:08:34,700 Any tuple, no problem. Right. 162 00:08:34,700 --> 00:08:37,000 [no audio] 163 00:08:37,000 --> 00:08:41,100 Now, you know what is the use of 'tuple:', 164 00:08:42,700 --> 00:08:46,700 same operation as your list, and string. You're going to get 165 00:08:46,700 --> 00:08:51,100 entire tuple, but I want to get from index 0 to last, same 166 00:08:51,100 --> 00:08:56,500 thing. But I want to get from index, third index to last. Third 167 00:08:56,500 --> 00:09:00,000 index means, 0 index is 3 number, 168 00:09:00,600 --> 00:09:04,700 this is second index, and this is the, sorry 0, 169 00:09:04,700 --> 00:09:10,500 1, 2, 3, 6 is the third index, from 6 to last I want, then 170 00:09:10,500 --> 00:09:11,900 I can take in this way. 171 00:09:13,500 --> 00:09:19,100 Right. So guys, whatever the index operations we performed on your 172 00:09:19,100 --> 00:09:21,100 strings all or applicable here as well. 173 00:09:22,000 --> 00:09:27,800 Let's say I want to get from starting to 5th index value. If 174 00:09:27,800 --> 00:09:30,300 you want to get 5th index value we have to provide 6, right? 175 00:09:31,300 --> 00:09:32,300 See that. 176 00:09:34,300 --> 00:09:39,000 1, first, 0, 1, 2, 3, 4, 5, 6, 177 00:09:39,000 --> 00:09:42,800 [no audio] 178 00:09:42,800 --> 00:09:45,400 if you take 6th index, you're 179 00:09:45,400 --> 00:09:47,400 getting up to five values, 180 00:09:47,500 --> 00:09:49,700 sorry six values up to 5th index, right? 181 00:09:51,400 --> 00:09:52,800 That's it. Now, 182 00:09:52,800 --> 00:09:53,900 let me take one more thing, 183 00:09:54,000 --> 00:09:55,000 last operation, 184 00:09:55,000 --> 00:09:57,500 [no audio] 185 00:09:57,500 --> 00:09:58,500 one second. 186 00:09:58,500 --> 00:10:00,900 [no audio] 187 00:10:00,900 --> 00:10:05,800 So that is simply 'print(my_tuple)' from third 188 00:10:05,800 --> 00:10:10,700 index, 3 values. From third index 3 values. Then 3 plus 3, 6 189 00:10:10,700 --> 00:10:12,700 you have to provide or from third index 190 00:10:12,700 --> 00:10:17,000 I want to get two values only, 3 and 4; 3 and 4. 191 00:10:17,100 --> 00:10:20,600 That's why 3 plus 2, 5. Now 192 00:10:20,600 --> 00:10:23,800 if you observe the output you are getting two values as a tuple. 193 00:10:23,800 --> 00:10:26,800 [no audio] 194 00:10:26,800 --> 00:10:30,700 Now we have last explanation with your tuple, that is, 195 00:10:30,700 --> 00:10:32,700 [no audio] 196 00:10:32,700 --> 00:10:38,500 see suppose if I write 'x=5', then 'print(x)', and then 'type(x)', 197 00:10:39,900 --> 00:10:42,400 see the output. Integer, right? 198 00:10:43,700 --> 00:10:46,100 But I am giving ',' here. Now see the output. 199 00:10:47,300 --> 00:10:52,100 'Tuple'. So if we write 'x=5,' that is a 200 00:10:52,100 --> 00:10:54,400 tuple which consists of only one value. 201 00:10:54,400 --> 00:10:56,300 [no audio] 202 00:10:56,300 --> 00:10:57,900 And also you can use something like this. 203 00:10:57,900 --> 00:11:01,700 This is also valid tuple. 5, 8, 9, means a 204 00:11:01,700 --> 00:11:05,800 tuple which consists of three values. See the output. You're 205 00:11:05,800 --> 00:11:06,700 not getting any error. 206 00:11:08,500 --> 00:11:11,100 If you write in this way, by default it will become a 207 00:11:11,100 --> 00:11:12,600 tuple data structure. 208 00:11:12,600 --> 00:11:14,000 You need to remember this point. 209 00:11:15,500 --> 00:11:19,400 Okay, single data tuple is you can write something in this, 210 00:11:20,800 --> 00:11:24,700 some number, but if you don't write ',' it will become a integer 211 00:11:24,700 --> 00:11:28,900 data, integer variable, but it is not a tuple data structure 212 00:11:28,900 --> 00:11:32,800 variable. If you write ',' then it will become a tuple data 213 00:11:32,800 --> 00:11:34,800 structure variable. Right. 214 00:11:35,200 --> 00:11:38,100 So guys, these are the important points with your tuple. 215 00:11:39,100 --> 00:11:42,500 Guys, as of now we have seen a list data structure and 216 00:11:42,500 --> 00:11:43,500 tuple data structure. 217 00:11:43,700 --> 00:11:47,500 So you need to remember that lists are mutable. Whenever if 218 00:11:47,500 --> 00:11:52,400 you want to change the data of your list, any part of your 219 00:11:52,400 --> 00:11:55,500 list, you can change it, but you cannot change the 220 00:11:55,500 --> 00:11:56,800 part of your tuple. 221 00:11:57,400 --> 00:12:00,600 That's why lists are mutable, 222 00:12:00,600 --> 00:12:02,100 and tuples are immutable, 223 00:12:03,000 --> 00:12:05,400 okay. Then what is the usage of your tuples and 224 00:12:05,400 --> 00:12:06,700 lists in your real life? 225 00:12:06,700 --> 00:12:08,700 [no audio] 226 00:12:08,700 --> 00:12:12,700 See you are designing some python script, 227 00:12:13,800 --> 00:12:16,500 right, and you are taking some data. 228 00:12:17,000 --> 00:12:18,300 So while taking data 229 00:12:20,000 --> 00:12:23,800 by logic if you want to change your data, you have to take 230 00:12:23,800 --> 00:12:25,200 your data as a list. 231 00:12:26,700 --> 00:12:30,700 Even by mistake if you don't want to change your data then 232 00:12:30,700 --> 00:12:32,800 simply consider your data as a tuple. 233 00:12:34,100 --> 00:12:37,100 And we'll see that situation with the help of an example 234 00:12:37,100 --> 00:12:40,500 once if we complete with your basic concepts. 235 00:12:40,900 --> 00:12:42,200 So up till that you have to wait. 236 00:12:42,300 --> 00:12:46,200 So up to that you have to remember simply tuple immutable, 237 00:12:46,300 --> 00:12:48,500 lists are mutable. That's it. 238 00:12:49,800 --> 00:12:53,900 Okay. Okay guys, thank you for watching this video. 239 00:12:53,900 --> 00:12:59,600 [no audio]