1 00:00:00,000 --> 00:00:01,100 [no audio] 2 00:00:01,100 --> 00:00:02,500 Friends, here we are going to 3 00:00:02,500 --> 00:00:05,700 do one simple practice with your for loop. 4 00:00:05,700 --> 00:00:09,200 I mean, I'm going to write a simple Python Script. The requirement 5 00:00:09,200 --> 00:00:15,600 is, read a string and print characters and their index values. 6 00:00:16,600 --> 00:00:21,200 I mean, suppose if you run your Python Script, it has to ask 7 00:00:21,200 --> 00:00:23,500 'Enter your input'. Then enter some input. 8 00:00:23,500 --> 00:00:27,000 Let's say I am going to enter input as 'python'. Then it has 9 00:00:27,000 --> 00:00:30,410 to give output as, in this format. 10 00:00:30,400 --> 00:00:33,710 'p-y-t-h-o-n'. At the same time 11 00:00:33,700 --> 00:00:37,300 it has to print the index value of your letters 12 00:00:37,300 --> 00:00:39,200 or characters in this way. 13 00:00:39,200 --> 00:00:42,610 [no audio] 14 00:00:42,600 --> 00:00:47,100 Right. So, guys before going to watch the next part in this video 15 00:00:47,100 --> 00:00:51,910 first just pause this video and try to write your script 16 00:00:51,900 --> 00:00:54,500 to read some string and to display your output in this way. 17 00:00:55,600 --> 00:01:00,200 Right. Just, you just give your try, then come back and watch the 18 00:01:00,200 --> 00:01:02,510 remaining part, right? 19 00:01:02,500 --> 00:01:06,000 Okay, but as of now, I am going to write, you just pause it and try. 20 00:01:06,800 --> 00:01:07,900 script from your side. 21 00:01:08,500 --> 00:01:13,200 Okay. Let me save the code as, I mean script name as 22 00:01:14,500 --> 00:01:15,500 'read_string', 23 00:01:15,500 --> 00:01:17,900 [no audio] 24 00:01:17,900 --> 00:01:21,500 then, '_print_with_index_values', 25 00:01:21,500 --> 00:01:23,400 [no audio] 26 00:01:23,400 --> 00:01:28,800 '.py', right. See, you know, if you want to read a string 27 00:01:28,800 --> 00:01:29,800 what you have to do? 28 00:01:30,600 --> 00:01:32,800 Let me take, 'usr_str' 29 00:01:32,800 --> 00:01:37,300 I am taking, equals to, I need to read a string. You know, 30 00:01:37,300 --> 00:01:40,710 if you want to read a string you have to use 'input' syntax, 31 00:01:40,700 --> 00:01:43,600 right? So, what I am doing is "Enter your string :". 32 00:01:43,600 --> 00:01:45,600 [no audio] 33 00:01:45,600 --> 00:01:48,710 Okay. Assume that user is going to enter anything, 34 00:01:48,700 --> 00:01:50,710 any string, right. 35 00:01:50,700 --> 00:01:53,000 Now, what I am doing is, see here 36 00:01:54,500 --> 00:01:59,300 'for each_char in usr_str' 37 00:01:59,300 --> 00:02:02,100 let me print first all characters one by one. 38 00:02:03,700 --> 00:02:07,500 Right. Now, let me my command line because I need to provide 39 00:02:07,500 --> 00:02:09,800 input for script, so I am going with this. 40 00:02:09,800 --> 00:02:12,209 [no audio] 41 00:02:12,199 --> 00:02:14,000 Now, let me open your script. 42 00:02:15,000 --> 00:02:18,209 'read_string_print_with_index_values', 43 00:02:18,200 --> 00:02:19,310 this is your script. 44 00:02:19,300 --> 00:02:25,600 Now, I am going to run that script, right. See that. 'Enter your string :'. 45 00:02:25,600 --> 00:02:28,600 Suppose I am entering 'python'. You can enter anything. 46 00:02:29,600 --> 00:02:30,600 See, what is happening. 47 00:02:32,300 --> 00:02:33,600 You are entering 'python'. 48 00:02:33,600 --> 00:02:36,000 [no audio] 49 00:02:36,000 --> 00:02:37,000 What I am printing? 50 00:02:37,000 --> 00:02:39,200 [no audio] 51 00:02:39,200 --> 00:02:42,300 Oh, I am printing 'usr_str', not 'each_char'. 52 00:02:42,300 --> 00:02:44,600 I want to print, then only I can get one by one character, 53 00:02:44,600 --> 00:02:46,600 right? Okay, let me rerun. 54 00:02:48,000 --> 00:02:50,500 So before going to rerun, so guys, what is the mistake 55 00:02:50,500 --> 00:02:54,200 I did means, in each and every repetition with your loop 56 00:02:54,200 --> 00:02:56,900 I am printing complete string, but I should not print complete 57 00:02:56,900 --> 00:02:59,700 string, I want to print character, whatever the character 58 00:02:59,700 --> 00:03:02,200 you are getting into this from your entire string one by 59 00:03:02,200 --> 00:03:03,200 one that character 60 00:03:03,200 --> 00:03:06,800 I want to print. That's why just now I modified your script. 61 00:03:06,800 --> 00:03:10,300 Now see I am printing each character, right. Now 62 00:03:10,300 --> 00:03:14,709 let me run your script once. I'm entering suppose 'python'. 63 00:03:14,700 --> 00:03:17,600 Yes, you are getting 'p-y-t-h-o-n' separately 64 00:03:17,600 --> 00:03:22,900 in each and every line. But while printing your character, right, 65 00:03:22,900 --> 00:03:26,300 what is our required output? 'p', some '--', arrow mark, 66 00:03:26,300 --> 00:03:27,810 then some integer number. 67 00:03:27,800 --> 00:03:29,300 So before that what I am doing is, 68 00:03:29,300 --> 00:03:31,300 [no audio] 69 00:03:31,300 --> 00:03:33,800 see I am taking 'f' strings, 70 00:03:34,800 --> 00:03:38,800 your variable value, then '--' suppose, 71 00:03:40,200 --> 00:03:41,200 in this way 72 00:03:41,500 --> 00:03:43,800 I am trying. Just see the output first. 73 00:03:43,800 --> 00:03:49,010 [no audio] 74 00:03:49,000 --> 00:03:50,500 Now I'm running this. 75 00:03:51,600 --> 00:03:53,200 I'm providing string as 'python'. 76 00:03:53,200 --> 00:03:56,800 Yes, you are getting 'p-->' symbol, but after 77 00:03:56,800 --> 00:04:00,700 that what you have to print? Here, you have to print 0, 1, 2, 78 00:04:00,700 --> 00:04:05,000 3, 4, 5, likewise based on the number of characters you have. 79 00:04:05,800 --> 00:04:07,100 See that simply what I am doing. 80 00:04:07,100 --> 00:04:10,310 [no audio] 81 00:04:10,300 --> 00:04:11,200 Where is your script? 82 00:04:11,200 --> 00:04:15,900 Yeah. See, what I am doing is I am taking, before your loop 83 00:04:15,900 --> 00:04:21,700 I am taking some index value, or simply 'ix', or simply 'index', 84 00:04:21,700 --> 00:04:23,899 let me take 'index=0'. Initially 85 00:04:23,899 --> 00:04:27,800 I'm taking index value as 0. While printing first variable 86 00:04:27,800 --> 00:04:31,200 value I will print this index as it is. See that. 87 00:04:31,200 --> 00:04:35,300 [no audio] 88 00:04:35,300 --> 00:04:37,000 Right. Now first 89 00:04:37,000 --> 00:04:39,000 let me run it so that you will get idea. 90 00:04:39,000 --> 00:04:41,110 [no audio] 91 00:04:41,100 --> 00:04:42,000 This is your script. 92 00:04:42,000 --> 00:04:44,600 What I am doing is I am printing your character and then 93 00:04:44,600 --> 00:04:46,200 index. What is the index, 0. 94 00:04:46,200 --> 00:04:48,000 [no audio] 95 00:04:48,000 --> 00:04:50,409 See guys, suppose if we enter 'python', 96 00:04:50,400 --> 00:04:53,500 I mean while running your script if you enter any string, 97 00:04:53,500 --> 00:04:55,300 [no audio] 98 00:04:55,300 --> 00:04:56,800 suppose I am entering 'python', 99 00:04:57,600 --> 00:05:01,500 so then if you observe your script, what will happen? This 100 00:05:01,500 --> 00:05:05,310 'p-y-t-h-o-n' will go and replace in this variable in your loop. 101 00:05:05,300 --> 00:05:07,200 Now, this is 'p-y-t-h-o-n' 102 00:05:07,200 --> 00:05:09,210 [no audio] 103 00:05:09,200 --> 00:05:12,300 Now your loop is going to take one by one character, and it 104 00:05:12,300 --> 00:05:15,000 is going to store in this variable and that variable we are 105 00:05:15,000 --> 00:05:16,100 printing. At the same time 106 00:05:16,100 --> 00:05:19,610 we are printing index also, but index is a constant value. 107 00:05:19,600 --> 00:05:22,000 That's why if I run our script, see the result 108 00:05:22,000 --> 00:05:26,400 what it is giving. For all characters, for all characters 109 00:05:26,400 --> 00:05:31,000 it is giving 0, but 0 is not the correct index except your 'p'. 110 00:05:32,400 --> 00:05:38,200 See, what I want to do is while printing 'p' I want to print 111 00:05:38,200 --> 00:05:42,500 initial value 0. So your loop is going to take second time second 112 00:05:42,500 --> 00:05:44,310 character from your given string. 113 00:05:44,300 --> 00:05:49,210 So before going to second, before going to get second character 114 00:05:49,200 --> 00:05:53,310 what I am doing is after your 'print' statement inside of your 115 00:05:53,300 --> 00:05:59,500 code I am increasing index value by one. 'index = index + 1'. 116 00:05:59,500 --> 00:06:01,400 [no audio] 117 00:06:01,400 --> 00:06:03,500 Now let me explain here itself. Suppose assume 118 00:06:03,500 --> 00:06:06,100 that you are entering string as 'python', then definitely your 119 00:06:06,100 --> 00:06:08,300 Python will replace in this way internally. 120 00:06:09,300 --> 00:06:11,310 Now see I am taking first 'p', 121 00:06:11,300 --> 00:06:14,300 I mean your Python and it is storing into this variable. Then 122 00:06:14,300 --> 00:06:17,500 after that whatever the lines you have those lines will repeat once. 123 00:06:19,100 --> 00:06:23,700 Right. So, while printing this index value initially it is 0, that's 124 00:06:23,700 --> 00:06:27,210 why 0 will print. Then your Python will execute next line. 125 00:06:27,200 --> 00:06:29,500 So, what you are doing? You are increasing your index value 126 00:06:29,500 --> 00:06:33,600 0 + 1 = 1. Now in your hand index value is 1. Then your Python 127 00:06:33,600 --> 00:06:37,409 will go and take next character from your given string, 'y'. 128 00:06:37,400 --> 00:06:40,710 That 'y' we are storing into this and then we are printing 129 00:06:40,700 --> 00:06:42,600 'y' and while printing index, 130 00:06:42,600 --> 00:06:46,300 what is the current index value? 0 + 1 = 1. Now, we are printing 131 00:06:46,300 --> 00:06:52,800 1. Right. Now, currently index is 1. Then after this line 132 00:06:52,800 --> 00:06:55,300 what you are doing? Again you are increasing index value 133 00:06:55,300 --> 00:07:00,500 1+1 now, that is 2. That means before going to take third 134 00:07:00,500 --> 00:07:05,000 character you are making your index value as 2, so third character 135 00:07:05,000 --> 00:07:09,700 index is always 2 only, right because 0, 1, 2. So likewise automatically 136 00:07:09,700 --> 00:07:12,600 you are increasing a variable called your required index, 137 00:07:14,200 --> 00:07:20,400 right. So now your required script you got it. Not only 138 00:07:20,400 --> 00:07:22,100 'python', you can take any variable. 139 00:07:22,100 --> 00:07:30,200 Let me run and provide simply "scripting is a good one for 140 00:07:30,200 --> 00:07:33,000 automation". Now see the result. 141 00:07:34,800 --> 00:07:36,500 Space is also one character, 142 00:07:36,500 --> 00:07:37,800 you need to remember that. 143 00:07:38,800 --> 00:07:41,500 Right. See automatically your are getting. For each and every 144 00:07:41,500 --> 00:07:43,100 character you're getting index values. 145 00:07:44,500 --> 00:07:48,900 Right. So, guys if you are able to understand these three lines and 146 00:07:48,900 --> 00:07:53,110 why I have given 'index = 0' before your loop, then 147 00:07:53,100 --> 00:07:54,400 you are good with your loops. 148 00:07:54,400 --> 00:07:56,200 [no audio] 149 00:07:56,200 --> 00:07:59,400 Suppose if I, guys, this logic you can now implement in different 150 00:07:59,400 --> 00:08:04,300 ways, but I am giving just for your, to get your idea on for 151 00:08:04,300 --> 00:08:06,600 loops I am giving in this way this example. 152 00:08:07,900 --> 00:08:13,900 Right. In case if you write 'index = 0' inside of your loop, 153 00:08:15,200 --> 00:08:18,400 then before printing your line always you are making index 154 00:08:18,400 --> 00:08:23,600 value 0. Then even though if you change here, before printing 155 00:08:23,600 --> 00:08:27,200 this line again you are making index value as 0. Always you will get 0. 156 00:08:27,200 --> 00:08:31,409 That's why whenever if you want to initialize any variable 157 00:08:31,400 --> 00:08:35,409 right, initial value, it should be there always before your 158 00:08:35,400 --> 00:08:37,400 loop. That's it. 159 00:08:38,600 --> 00:08:42,000 Now you take even simply some 'python', no problem. 160 00:08:42,900 --> 00:08:45,900 That's it. Right. Okay. 161 00:08:46,900 --> 00:08:48,799 Okay guys, thank you for watching this video. 162 00:08:48,799 --> 00:08:55,799 [no audio]