1 00:00:00,000 --> 00:00:01,000 [no audio] 2 00:00:01,000 --> 00:00:04,600 Friends, here we are going to use your for loop 3 00:00:06,000 --> 00:00:10,700 for your strings, list, tuple and dictionaries. 4 00:00:10,700 --> 00:00:15,300 Already we have seen how to use your loop for string, list, tuple, right, 5 00:00:15,300 --> 00:00:18,700 but here I want to show you once again with small 6 00:00:18,700 --> 00:00:20,300 modifications, right? 7 00:00:20,600 --> 00:00:22,900 Anyway, there is no modification with your string. 8 00:00:23,700 --> 00:00:26,800 Let me write a simple Python script just to give overall 9 00:00:26,800 --> 00:00:30,100 idea with your for loop to use for your different data structures 10 00:00:30,100 --> 00:00:31,100 and data types. 11 00:00:32,299 --> 00:00:38,800 Let me take 'loops_for_data', something like you can take any 12 00:00:38,800 --> 00:00:40,900 script name right, '_structures.py'. 13 00:00:41,900 --> 00:00:46,800 First of all, let me take my string as anything, 'my_string' 14 00:00:48,200 --> 00:00:49,300 "working with 15 00:00:49,300 --> 00:00:52,400 [no audio] 16 00:00:52,400 --> 00:00:53,400 for loop". 17 00:00:54,800 --> 00:00:56,400 Directly you can print your string. 18 00:00:57,300 --> 00:00:58,700 Right. You can get your output. 19 00:01:00,200 --> 00:01:01,400 Let me write 'my_string'. 20 00:01:02,700 --> 00:01:03,800 You're getting your output. 21 00:01:05,099 --> 00:01:10,200 Now, I want to print your string characters one by one, 22 00:01:11,500 --> 00:01:14,100 in different lines actually I want to print. I mean suppose 23 00:01:14,100 --> 00:01:16,000 in first line I want to get first character, 24 00:01:16,000 --> 00:01:17,700 second line second character, something like that. 25 00:01:17,700 --> 00:01:19,700 [no audio] 26 00:01:19,700 --> 00:01:22,200 Right. See, what I am doing is simply 27 00:01:23,200 --> 00:01:25,000 'for each_char', 28 00:01:25,000 --> 00:01:28,700 I can separate character one by one from your given string 29 00:01:29,100 --> 00:01:32,900 with the help of your loop, for loop. By default you can use it. 30 00:01:34,000 --> 00:01:36,400 Yes. You are getting one by one character. 31 00:01:37,900 --> 00:01:40,100 Now, if you remember instead of these two lines, 32 00:01:41,800 --> 00:01:46,500 in case if your intention is just to print your character, one by one 33 00:01:46,500 --> 00:01:49,300 characters, right, no need to use for loop for your strings. 34 00:01:49,300 --> 00:01:51,500 You have a 'join' method, if you remember. 35 00:01:51,500 --> 00:01:53,400 [no audio] 36 00:01:53,400 --> 00:01:57,100 'join', let me write first your string as it is, and see the 37 00:01:57,100 --> 00:02:00,600 output, what you are getting. You're getting your string by adding 38 00:02:00,600 --> 00:02:05,800 space, but I am going to add '\n', new line. Now see the 39 00:02:05,800 --> 00:02:10,199 output. No need to write two lines of output to print your 40 00:02:10,199 --> 00:02:11,699 characters line by line. 41 00:02:13,100 --> 00:02:15,100 Right. Just you can use 'join' method. 42 00:02:16,700 --> 00:02:20,500 Right. Fine. Then, let me comment this. 43 00:02:20,500 --> 00:02:22,400 [no audio] 44 00:02:22,400 --> 00:02:26,200 There is not much work with your strings. In case if you want 45 00:02:26,200 --> 00:02:29,500 to simply print your characters one by one on your output 46 00:02:29,500 --> 00:02:32,500 side you can take either print, I mean for loop or 'join' directly. 47 00:02:34,000 --> 00:02:40,800 That's okay. Now what I am doing is, let me take my list. 48 00:02:40,800 --> 00:02:43,500 I am writing '1,2,3,4,5' something like that. 49 00:02:44,800 --> 00:02:49,300 Now, you know how to take your 'each' value from your list 50 00:02:50,000 --> 00:02:51,000 one by one, 51 00:02:51,000 --> 00:02:53,900 [no audio] 52 00:02:53,900 --> 00:02:57,900 and you can print it. Right. Yes, observe your output. 53 00:02:57,900 --> 00:03:00,000 I'm able to print your values one by one. 54 00:03:01,200 --> 00:03:02,200 That's fine. 55 00:03:03,600 --> 00:03:05,600 But assume that inside of a list I have a 56 00:03:07,000 --> 00:03:08,700 'values as', okay 57 00:03:08,700 --> 00:03:10,400 let me keep this as it is. 58 00:03:10,400 --> 00:03:12,700 [no audio] 59 00:03:12,700 --> 00:03:13,800 I want to comment this. 60 00:03:15,100 --> 00:03:16,100 That's fine. 61 00:03:16,100 --> 00:03:18,000 [no audio] 62 00:03:18,000 --> 00:03:21,830 Now actually, now 'my_list' is in this way guys. 'my_list =' 63 00:03:22,400 --> 00:03:27,700 list of list. but each and every value is again a tuple. 64 00:03:27,700 --> 00:03:29,700 Yes, you may have in this way. No problem. 65 00:03:29,700 --> 00:03:31,600 [no audio] 66 00:03:31,600 --> 00:03:36,200 Something like this. Now for this I'm using, now, 67 00:03:36,200 --> 00:03:37,600 this is not like one value, 68 00:03:37,600 --> 00:03:38,800 it is like some items, right, 69 00:03:38,800 --> 00:03:40,900 I mean for each tuple inside of a list. 70 00:03:40,900 --> 00:03:42,800 I am taking simply, you can take any variable. 71 00:03:42,800 --> 00:03:46,500 I am taking simply 'each_item in my_list'. 72 00:03:46,500 --> 00:03:51,400 Now, I am printing 'each_item'. Now, see the output. 73 00:03:51,400 --> 00:03:54,600 Yeah. Let me save it and see the output. 74 00:03:54,600 --> 00:03:56,600 [no audio] 75 00:03:56,600 --> 00:04:00,000 From your list this entire thing is actually one first value. 76 00:04:00,000 --> 00:04:02,400 That's what I am taking. 'each_item' is nothing but first value 77 00:04:02,400 --> 00:04:05,300 you're taking. Then you are printing second value. 78 00:04:05,300 --> 00:04:09,900 See, finally this entire thing is a first value in your list like 79 00:04:09,900 --> 00:04:12,900 one, this entire value is second value, 80 00:04:14,100 --> 00:04:17,000 third value. Now you're taking one by one and your printing them. 81 00:04:18,100 --> 00:04:19,500 Yes, we are doing that. 82 00:04:20,200 --> 00:04:23,500 Now one more thing. Let me open my command line. 83 00:04:24,399 --> 00:04:26,100 It's very important, try to understand. 84 00:04:27,000 --> 00:04:29,000 See guys, I have a tuple called 85 00:04:29,000 --> 00:04:30,900 [no audio] 86 00:04:30,900 --> 00:04:33,000 Some number maybe, in this way, 87 00:04:34,000 --> 00:04:37,700 and I'm going to assign this like 'x,y=(5,6)'. 88 00:04:38,100 --> 00:04:41,400 'print(y)', and see the result what you have. You're going 89 00:04:41,400 --> 00:04:44,600 to get 6, and in 'x' you are going to get 5. 90 00:04:44,600 --> 00:04:46,600 [no audio] 91 00:04:46,600 --> 00:04:50,500 Now what I am doing here is, with the help of your for loop 92 00:04:50,500 --> 00:04:53,600 this entire thing I am storing into 'each_item' variable. 93 00:04:55,100 --> 00:04:58,100 I am going to store this first value into this 'each_item', 94 00:04:58,100 --> 00:05:00,200 then I am printing, the second value 95 00:05:00,200 --> 00:05:01,400 then I am printing, third value 96 00:05:01,400 --> 00:05:03,600 I am taking and then I am printing with the help of your loop. 97 00:05:04,700 --> 00:05:07,800 But while taking, here because we have two values 98 00:05:07,800 --> 00:05:11,700 the inside of this tuple, I can unpack it with two variables. 99 00:05:11,700 --> 00:05:13,900 I mean see, I am taking something like 100 00:05:16,100 --> 00:05:17,300 first value and then 101 00:05:17,300 --> 00:05:19,300 second value, 'f,s'. 102 00:05:19,700 --> 00:05:22,400 Now what I am doing is, I am going to print only 'f'. 103 00:05:22,900 --> 00:05:26,500 I'm going to print only 'f', that means from each and every 104 00:05:26,500 --> 00:05:29,900 item I am taking first value and then I am printing that. 105 00:05:30,800 --> 00:05:33,900 Actually, I am taking two values, but first value, 1 106 00:05:33,900 --> 00:05:37,700 I am assigning to 'f', 2 I am assigning so 's'. But I am printing 107 00:05:37,700 --> 00:05:40,900 only 'f'. That's why what is the output you're going to expect is, 108 00:05:40,900 --> 00:05:44,400 from first item 1, from here 4, from here 6. 109 00:05:45,500 --> 00:05:49,200 See the output. Let me run it by saving this code. 110 00:05:49,200 --> 00:05:51,400 [no audio] 111 00:05:51,400 --> 00:05:52,400 One second. 112 00:05:52,400 --> 00:05:55,700 [no audio] 113 00:05:55,700 --> 00:05:56,700 One second. 114 00:05:56,700 --> 00:06:00,300 Yeah, let me save it and run it. See the output. 115 00:06:00,300 --> 00:06:03,000 [no audio] 116 00:06:03,000 --> 00:06:06,300 1, 4, 6, you are getting. Only first values. Now, I am printing 117 00:06:06,300 --> 00:06:10,500 's'. Now in each and every item you are going to get second value. 118 00:06:12,000 --> 00:06:13,200 I want to print both. 119 00:06:13,300 --> 00:06:14,100 Yes, you can print it, 120 00:06:14,100 --> 00:06:18,900 no problem. 'f,s'. 1,2. I mean '(1, 2), (4, 5), (6, 7) you are 121 00:06:18,900 --> 00:06:20,400 getting. One and two. 122 00:06:21,300 --> 00:06:26,700 I mean while taking value from your list if that value is 123 00:06:26,700 --> 00:06:31,500 a tuple, or maybe a list also, then you can unpack it into two variables. 124 00:06:31,500 --> 00:06:33,700 Let me take now instead of tuple, 125 00:06:33,700 --> 00:06:36,800 I can also take inside of a list a list, 126 00:06:38,600 --> 00:06:41,400 or maybe combination of your lists and tuples also, no problem. 127 00:06:41,400 --> 00:06:44,400 But the thing is whenever if you are taking two variables 128 00:06:44,400 --> 00:06:45,800 inside of your loop, 129 00:06:47,400 --> 00:06:50,600 the value which you are taking from your list, that value 130 00:06:50,600 --> 00:06:53,800 should have two values, then only you can unpack it. 131 00:06:54,200 --> 00:06:57,400 First value to 'f', second value to 's'. That's it. 132 00:06:58,700 --> 00:07:00,900 But see that, suppose if I have a list. 133 00:07:00,900 --> 00:07:02,800 [no audio] 134 00:07:02,800 --> 00:07:03,800 Let me take it. 135 00:07:04,400 --> 00:07:06,300 '[3, 4]'. It's fine. 136 00:07:06,300 --> 00:07:08,700 But if I don't have, only if I have one value 137 00:07:09,700 --> 00:07:12,500 you can't unpack it because 'x,y' equals to, you are assigning 138 00:07:12,500 --> 00:07:16,800 one value. That's not possible. 'x=y=3', you can assign it. '3' value 139 00:07:16,800 --> 00:07:19,100 you can't unpack it to two variables. 140 00:07:19,500 --> 00:07:20,600 That's what I am saying here. 141 00:07:21,200 --> 00:07:24,500 Whenever you are taking one by one value, if you have 142 00:07:24,500 --> 00:07:28,500 two values inside of that value, you can unpack in terms 143 00:07:28,500 --> 00:07:30,800 of two variables. Now see the result. 144 00:07:30,800 --> 00:07:32,000 I am saving it. I'm running it. 145 00:07:32,000 --> 00:07:34,300 Yes, you are getting. The same way 146 00:07:34,300 --> 00:07:37,400 you can write tuple, and inside of a tuple you can take tuple 147 00:07:37,400 --> 00:07:40,600 also, combination of tuples and list, you can take anything. 148 00:07:41,800 --> 00:07:45,400 Actually, up to this it's not that much important, but the 149 00:07:45,400 --> 00:07:50,000 concept is important. Why I am giving this is, you have one 150 00:07:50,000 --> 00:07:53,600 very, very important situation while working with your real 151 00:07:53,600 --> 00:07:55,100 time using dictionaries. 152 00:07:55,600 --> 00:07:57,400 First of all, let me define my dictionary. 153 00:07:57,400 --> 00:07:59,300 [no audio] 154 00:07:59,300 --> 00:08:05,400 I am taking suppose 'a', I am writing 1. Then 'b' I am writing some 2 155 00:08:05,400 --> 00:08:06,400 let me take. Anything 156 00:08:06,400 --> 00:08:10,800 you can take it, right. Key as 'c' and value is 3. 157 00:08:12,100 --> 00:08:15,000 Now, I am taking 'for each in my_dic'. 158 00:08:16,200 --> 00:08:17,200 Now, let me print. 159 00:08:18,800 --> 00:08:20,800 You can guess, or you can, 160 00:08:20,800 --> 00:08:22,200 can you expect what is the output 161 00:08:22,200 --> 00:08:24,200 I am going to get if I run this code? 162 00:08:24,200 --> 00:08:26,700 [no audio] 163 00:08:26,700 --> 00:08:29,000 Only keys you are going to get by default. 164 00:08:29,000 --> 00:08:31,000 [no audio] 165 00:08:31,000 --> 00:08:32,500 If this is your dictionary, 166 00:08:32,500 --> 00:08:34,900 if you use your dictionary inside of your loop, 167 00:08:35,700 --> 00:08:37,500 you're going to get only keys, 168 00:08:38,799 --> 00:08:42,600 or if you remember one of the dictionary operation is like 169 00:08:42,600 --> 00:08:45,500 '.keys' is also there.That is a list. 170 00:08:46,600 --> 00:08:50,500 Anyway, if you take only 'my_dic', your dictionary in your 171 00:08:50,500 --> 00:08:55,600 loop, by default your loop, for loop is going to take only keys, 172 00:08:55,600 --> 00:08:58,100 otherwise your 'dic.keys' operation. 173 00:08:59,700 --> 00:09:01,100 It's very, very important guys. 174 00:09:02,200 --> 00:09:05,000 Let me run it and see the output. You're going to get keys 175 00:09:05,000 --> 00:09:08,000 only. But I want to get only values. Now, 176 00:09:08,000 --> 00:09:09,600 you can apply here 'values'. 177 00:09:10,800 --> 00:09:13,200 Now see the result. You're going to get 1, 2, 3, values. 178 00:09:14,200 --> 00:09:17,700 Now I want to get key, and then value. If you remember, there 179 00:09:17,700 --> 00:09:19,500 is an operation called 'items' 180 00:09:21,100 --> 00:09:23,000 on your dictionary. Now see the result. 181 00:09:24,700 --> 00:09:29,200 'each' is nothing but now an item which consists of key and 182 00:09:29,200 --> 00:09:32,500 value. Key and value, because now in each and every variable 183 00:09:32,700 --> 00:09:36,300 you have a tuple which consists of two values. Now I can take 184 00:09:36,300 --> 00:09:39,700 here two variables called 'key,value'. 185 00:09:40,100 --> 00:09:42,400 I mean you can take any variable name, but because this is 186 00:09:42,400 --> 00:09:45,400 dictionary, from the dictionary you are getting key and then 187 00:09:45,400 --> 00:09:47,800 value, now I am taking 'key' and 'value'. 188 00:09:48,700 --> 00:09:52,500 Now if you want to print only keys, you can print it, only 'key'. 189 00:09:53,600 --> 00:09:56,400 See the output. Or I want to print only 'value'. 190 00:09:58,000 --> 00:10:01,000 See the output. I want to get both, 191 00:10:03,000 --> 00:10:04,100 'key,value'. Yes. 192 00:10:05,500 --> 00:10:11,800 That's it. So taking keys and values from your dictionary using for 193 00:10:11,800 --> 00:10:15,800 loop is very, very important. Try to execute this logic again 194 00:10:15,800 --> 00:10:19,300 and again, and see the result with all operations. 195 00:10:19,700 --> 00:10:23,200 This is very, very important guys, working with your dictionary 196 00:10:23,200 --> 00:10:27,200 with '.items', and what is the output you are getting? 197 00:10:27,200 --> 00:10:30,800 See, if I run simply on my command line, you have already dictionary, 198 00:10:31,200 --> 00:10:34,800 what is the output of '.items'? You're going to get a list which 199 00:10:34,800 --> 00:10:40,200 consists of tuple as a value, and in the tuple you have one is 200 00:10:40,200 --> 00:10:44,700 the 'key' and second value is 'value'. Now using for loop 201 00:10:44,700 --> 00:10:47,300 you can take one by one value from your loop. 202 00:10:48,500 --> 00:10:51,700 And while taking because you have a tuple which consists 203 00:10:51,700 --> 00:10:56,100 of two values now I am trying to unpack in this way, 'x,y=5,6', 204 00:10:56,100 --> 00:10:58,400 you know? So now what I am taking 205 00:10:58,400 --> 00:11:00,900 is, 'key,value' I am taking because it's very suitable 206 00:11:00,900 --> 00:11:04,000 word for your dictionary. This is a 'key' and this is a 'value'. 207 00:11:04,000 --> 00:11:06,300 That's why I'm taking variables as 'key' and 'value'. 208 00:11:06,300 --> 00:11:08,700 [no audio] 209 00:11:08,700 --> 00:11:10,200 Right. Okay. 210 00:11:10,200 --> 00:11:12,400 Okay guys, Thank you for watching this video. 211 00:11:12,400 --> 00:11:16,231 [no audio]