1 00:00:00,000 --> 00:00:02,400 [no audio] 2 00:00:02,400 --> 00:00:07,200 Friends, here we are going to discuss about one of the operation from your 3 00:00:07,200 --> 00:00:13,100 Sys Module, that is called 'sys.argv', 'sys.arguments' 4 00:00:14,200 --> 00:00:21,200 Now what is this? See 'sys.argv' returns a list that consists 5 00:00:21,200 --> 00:00:24,900 of command line arguments which are passed to your Python 6 00:00:24,900 --> 00:00:30,200 script. Simply it is giving a list means it is a variable 7 00:00:30,200 --> 00:00:33,800 now. Actually it is one of the operation from your Sys Module. 8 00:00:33,800 --> 00:00:37,100 We know operation may be a function or maybe a variable. 9 00:00:37,800 --> 00:00:40,500 So 'sys.argv' is giving a list. 10 00:00:41,900 --> 00:00:46,500 Right? So which consists of some command line arguments, which are 11 00:00:46,500 --> 00:00:48,200 passed to your Python script. 12 00:00:48,800 --> 00:00:51,400 So to understand this, first of all you have to know what 13 00:00:51,400 --> 00:00:55,000 is a command line argument, right? 14 00:00:55,500 --> 00:00:57,100 Let me open some script. 15 00:00:57,500 --> 00:00:59,600 Let me open my editor. 16 00:01:00,500 --> 00:01:03,100 So I am going to write a script called 17 00:01:03,100 --> 00:01:05,000 [no audio] 18 00:01:05,099 --> 00:01:15,400 'command_line_arguments', 'command_line_arguments.py', so 19 00:01:15,400 --> 00:01:16,100 something. I'm writing 20 00:01:16,100 --> 00:01:17,700 nothing is there. Simply I am writing, "hello". 21 00:01:17,700 --> 00:01:20,700 [no audio] 22 00:01:20,700 --> 00:01:21,300 That's it. 23 00:01:21,700 --> 00:01:23,800 Now, I am going to run from command line. 24 00:01:23,900 --> 00:01:27,200 So if you want to work, if you want to know command line arguments, 25 00:01:27,200 --> 00:01:31,300 always you have to run your script from command line only. Fine. 26 00:01:32,100 --> 00:01:34,900 See just now we have written a script called, this one. 27 00:01:35,200 --> 00:01:36,800 Nothing is there simply, print('hello'). 28 00:01:37,500 --> 00:01:40,600 Let me run this 'python command_line_arguments', 29 00:01:40,600 --> 00:01:42,000 your script name, '.py' 30 00:01:42,700 --> 00:01:48,000 Yes, you're getting some output. Right. Now while running your 31 00:01:48,000 --> 00:01:51,500 script, while running your script by your Python interpreter, 32 00:01:52,000 --> 00:01:57,700 I am also passing some values called, suppose, "hi how are 33 00:01:57,900 --> 00:02:01,700 you", some 1 3 67, 34 00:02:01,700 --> 00:02:05,200 whatever it may be. See if I pass in this way just observe that do you 35 00:02:05,200 --> 00:02:06,700 have any disturbance with your script? 36 00:02:07,300 --> 00:02:10,199 Is there any wrong output? No, it's working perfectly. 37 00:02:11,400 --> 00:02:15,800 See after your script name if I pass some values 38 00:02:16,000 --> 00:02:17,700 they are not disturbing your script. 39 00:02:17,900 --> 00:02:19,800 First of all in this way 40 00:02:19,800 --> 00:02:23,600 if you pass, yes, actually that is the correct. You can pass in that way. 41 00:02:23,600 --> 00:02:26,500 There is no wrong in that. If it is wrong, then you have to 42 00:02:26,500 --> 00:02:27,500 get some error, right? 43 00:02:27,500 --> 00:02:29,000 You are not getting any error. 44 00:02:29,200 --> 00:02:33,400 That means yes, you can run your script by passing some values 45 00:02:33,400 --> 00:02:38,900 in this way. Now after your script name whatever you're passing, 46 00:02:39,300 --> 00:02:43,500 right, so these all are called command line arguments for 47 00:02:43,500 --> 00:02:46,700 your script. While running your script you're passing some 48 00:02:46,700 --> 00:02:51,400 values. These are called command line arguments, right? 49 00:02:51,500 --> 00:02:54,800 So if you are passing some command line arguments here, if 50 00:02:54,800 --> 00:02:58,200 you're passing some command line arguments here how you can 51 00:02:58,400 --> 00:03:00,900 get them inside of your Python script? 52 00:03:00,900 --> 00:03:02,900 [no audio] 53 00:03:02,900 --> 00:03:04,100 Right. See nothing 54 00:03:04,100 --> 00:03:08,200 is there. What I am doing is simply, now let me remove 'pring('hello')'. 55 00:03:08,900 --> 00:03:09,900 You can write anything, right? 56 00:03:10,000 --> 00:03:18,200 I am writing 'print(sys.argv)', that is a variable but 57 00:03:18,200 --> 00:03:19,300 you are using 'sys', right? 58 00:03:19,600 --> 00:03:21,400 That's why what you have to do? You need to 'import sys'. 59 00:03:21,400 --> 00:03:23,400 [no audio] 60 00:03:23,400 --> 00:03:28,900 Right? Now see. First of all observe your script, what you have inside 61 00:03:28,900 --> 00:03:31,800 of your script. Nothing is there. I am using 'sys'. 62 00:03:31,900 --> 00:03:34,300 That's why I am importing that 'sys' first. Then I am printing 63 00:03:34,300 --> 00:03:35,700 simply 'sys.argv'. 64 00:03:35,700 --> 00:03:39,300 First I am running your script in this way, guys. Observe 65 00:03:39,300 --> 00:03:42,000 the output what you are getting? You are getting a list which 66 00:03:42,000 --> 00:03:46,100 consists of your script name only because after your script 67 00:03:46,100 --> 00:03:49,300 name, you're not passing anything. In case if you pass what 68 00:03:49,300 --> 00:03:52,400 will happen? Just I am passing, suppose 'python', 69 00:03:53,400 --> 00:03:55,900 some 'shell', some 'perl', something like that. 70 00:03:56,000 --> 00:03:59,600 So we'll see the output. Whatever you are passing here, 71 00:03:59,700 --> 00:04:05,300 all these values are coming in this variable called 'sys.argv' as a list. 72 00:04:06,800 --> 00:04:10,700 Not only that, you can pass some values also, see the output. 73 00:04:11,500 --> 00:04:17,300 Now 'sys.argv' is capturing inside of your script 74 00:04:18,399 --> 00:04:21,899 whatever the values, whatever the arguments you are passing 75 00:04:22,000 --> 00:04:25,000 while running your script, right? 76 00:04:25,000 --> 00:04:28,000 So these are the command line arguments you are passing while 77 00:04:28,000 --> 00:04:31,200 running your script and you can handle these values inside 78 00:04:31,200 --> 00:04:34,200 of your script with the help of 'sys.argv'. 79 00:04:34,300 --> 00:04:35,700 It's very, very helpful. 80 00:04:35,700 --> 00:04:37,700 [no audio] 81 00:04:37,700 --> 00:04:41,000 Right? And you need to remember one more thing, whatever the 82 00:04:41,000 --> 00:04:43,600 values you are passing from command lines even though if 83 00:04:43,600 --> 00:04:46,900 it is a number see that it is treating as a string. 84 00:04:46,900 --> 00:04:50,000 That's why you have a quotation here. You know in Python, 85 00:04:50,200 --> 00:04:53,500 right, if I write x equals to some list consists of 4, 5 86 00:04:53,500 --> 00:04:56,800 then here 4 and 5 are values. Observe the output. You are 87 00:04:56,800 --> 00:04:59,800 not having any quotations. But here you having a quotation 88 00:04:59,800 --> 00:05:00,500 for your numbers, 89 00:05:00,500 --> 00:05:04,600 that means whatever you are passing from command line all 90 00:05:04,600 --> 00:05:12,300 will treat as a string by your Python, right. Fine. 91 00:05:12,400 --> 00:05:15,300 Let me take one simple example to understand this example. 92 00:05:15,500 --> 00:05:19,600 Yeah, one more thing guys. See, this is going to give all your 93 00:05:19,600 --> 00:05:21,900 command line arguments, right? 94 00:05:21,900 --> 00:05:25,700 And if you observe your output, the very first value is always 95 00:05:25,700 --> 00:05:28,000 your script. How many values you are passing? 96 00:05:28,000 --> 00:05:30,000 How many command line arguments you are passing? 97 00:05:30,800 --> 00:05:32,700 Right. Whatever you are passing, 98 00:05:32,800 --> 00:05:36,000 the index 0 is always your script name. See that. Now I am passing 99 00:05:36,000 --> 00:05:39,000 only one command line argument. You are getting index 0 100 00:05:39,000 --> 00:05:43,300 as your script name, remaining all are your command line arguments, 101 00:05:43,500 --> 00:05:47,400 right? So if I want to display only script name what I have 102 00:05:47,400 --> 00:05:52,000 to do? 'sys.', if you remember, if you remember list operations, 103 00:05:52,000 --> 00:05:54,800 I want to get only the first index value. 104 00:05:55,300 --> 00:05:58,200 I mean the first value in your list, that is index is always 105 00:05:58,200 --> 00:05:59,800 0, right? 106 00:06:00,500 --> 00:06:04,800 Let me rerun and see the output. Because of, let me open your 107 00:06:04,800 --> 00:06:09,400 script once again here, because of 'prints(sys.argv[0])' you are 108 00:06:09,400 --> 00:06:12,600 getting this command line arguments as a script name. 109 00:06:13,400 --> 00:06:17,000 0 is always script name in your 'sys.argv', 110 00:06:17,000 --> 00:06:19,700 because 'sys.argv' is a list, in that first value is 111 00:06:19,700 --> 00:06:21,000 always your script name. 112 00:06:21,000 --> 00:06:22,800 That's what I am getting with index 0. 113 00:06:24,100 --> 00:06:25,100 That's fine. 114 00:06:25,600 --> 00:06:28,000 Now guys, I have a small requirement. 115 00:06:28,000 --> 00:06:31,600 My requirement is, my script has to read some string, 116 00:06:32,300 --> 00:06:34,300 my script has to read some string. 117 00:06:35,400 --> 00:06:39,600 Then after that, I need to convert, based on my requirement 118 00:06:39,700 --> 00:06:42,100 sometimes I will convert into lowercase, 119 00:06:42,100 --> 00:06:43,900 sometimes I will convert into uppercase. 120 00:06:43,900 --> 00:06:46,000 sometimes I will convert into title format. 121 00:06:47,100 --> 00:06:49,800 Right. Forget about first your command line arguments. 122 00:06:50,500 --> 00:06:53,900 So if you want to provide some string to your script what 123 00:06:53,900 --> 00:06:54,400 you have to do? 124 00:06:55,600 --> 00:06:56,500 Let me do this thing. 125 00:06:56,500 --> 00:07:01,200 [no audio] 126 00:07:01,200 --> 00:07:05,900 Let me take 'usr_str =', if you want to read a string 127 00:07:05,900 --> 00:07:08,900 always you have to take 'input' as a, let me save it. 128 00:07:08,900 --> 00:07:10,900 [no audio] 129 00:07:10,900 --> 00:07:17,400 'actions_on_strings.py', right? See here. 130 00:07:17,900 --> 00:07:21,700 I am reading a string from command line, 'usr_str = input', 131 00:07:21,700 --> 00:07:23,300 enter your string, I am asking. 132 00:07:26,400 --> 00:07:30,200 Then after that, 'usr_action', I am reading. What is that action 133 00:07:30,200 --> 00:07:33,900 means, 'input("Enter your action 134 00:07:33,200 --> 00:07:36,000 [no audio] 135 00:07:36,000 --> 00:07:40,700 on your string", so "Valid actions are", just I am giving the 136 00:07:40,700 --> 00:07:50,200 clue. "Valid actions are:" like "lower/upper/title" format. 137 00:07:52,000 --> 00:07:52,800 That's it. 138 00:07:52,900 --> 00:07:57,000 So these are the values I need to read from user. Now assume 139 00:07:57,000 --> 00:07:59,800 that user is going to enter some string and user is going 140 00:07:59,800 --> 00:08:03,800 to enter suppose lower or upper or title format, now based on 141 00:08:03,800 --> 00:08:05,000 user request. 142 00:08:05,300 --> 00:08:08,800 I need to print your string in lowercase or uppercase or 143 00:08:08,800 --> 00:08:09,900 in the title format. 144 00:08:10,100 --> 00:08:12,400 That's why here I need to take 'if' condition first of all, 145 00:08:12,400 --> 00:08:16,500 right. 'if usr_action == lower', 146 00:08:16,500 --> 00:08:19,900 [no audio] 147 00:08:19,900 --> 00:08:24,600 right, then I have to print your given string, that is 'usr_str' into 148 00:08:24,600 --> 00:08:28,800 lowercase. We have different options here, 149 00:08:28,800 --> 00:08:34,400 right. User may enter, right 'upper' also. So I am checking 150 00:08:34,400 --> 00:08:35,700 'if usr_action == upper', 151 00:08:37,400 --> 00:08:42,400 then I am going to print my 'usr_str' in terms of uppercase. 152 00:08:42,400 --> 00:08:44,799 [no audio] 153 00:08:44,799 --> 00:08:51,900 Right. 'elif 'usr_action', if 'usr_action =="title"' format, 154 00:08:54,600 --> 00:09:00,900 then I am printing 'usr_str.title'. In case by mistake 155 00:09:00,900 --> 00:09:05,100 if user enters other than lower/upper/title, 156 00:09:05,200 --> 00:09:07,100 then I'm writing 'else' block, and I am saying 157 00:09:07,100 --> 00:09:09,900 "Your option is invalid". 158 00:09:09,900 --> 00:09:13,200 New Text 159 00:09:13,200 --> 00:09:21,700 Right, so "Please select valid options from this list". 160 00:09:22,500 --> 00:09:27,700 So what are the valid options? "lower/upper/title" 161 00:09:28,900 --> 00:09:29,600 That's it. Fine. 162 00:09:30,200 --> 00:09:33,300 Right, let me go and run this script from command line. 163 00:09:34,300 --> 00:09:37,400 So first of all your script, this is your script. 164 00:09:37,400 --> 00:09:39,300 Let me run it and see the output first. 165 00:09:40,000 --> 00:09:43,400 I am running. I am providing string called, some randomly 166 00:09:43,400 --> 00:09:47,500 I am providing in this way, "Python scripting", something like 167 00:09:47,500 --> 00:09:51,700 that. Then I want to convert my string into lower or upper 168 00:09:51,700 --> 00:09:52,500 or title. Any option 169 00:09:52,500 --> 00:09:55,200 you can provide. I am giving, suppose title format, then see 170 00:09:55,200 --> 00:09:58,600 the output. You're getting in the title format. Then I'm rerunning 171 00:09:59,000 --> 00:10:00,700 and now I am going to provide 'lower'. 172 00:10:00,900 --> 00:10:03,600 I want to convert my string into lower case. Yes, fine. 173 00:10:03,600 --> 00:10:05,500 [no audio] 174 00:10:05,500 --> 00:10:10,800 But whatever the script you have now your script needs two 175 00:10:10,800 --> 00:10:15,900 inputs - one is string, one more is user action. One is string 176 00:10:15,900 --> 00:10:21,600 one more is user action. But while running script you are 177 00:10:21,600 --> 00:10:23,700 providing, right. 178 00:10:23,800 --> 00:10:26,900 See after running I am interacting with my script. 179 00:10:27,400 --> 00:10:31,000 Yes, you can interact but my intention is I don't want to 180 00:10:31,000 --> 00:10:31,900 provide in this way. 181 00:10:31,900 --> 00:10:34,000 [no audio] 182 00:10:34,000 --> 00:10:35,600 I have to run with one step. 183 00:10:36,300 --> 00:10:39,300 I have to run my script in one step, then how you can do it? 184 00:10:40,200 --> 00:10:41,600 See for that I have a idea. 185 00:10:41,700 --> 00:10:44,100 What I will do is, first command line arguments 186 00:10:44,100 --> 00:10:48,300 I will send as a, I will send a string, required string like 187 00:10:48,300 --> 00:10:51,400 "Python scripting". Then second argument, right, 188 00:10:51,400 --> 00:10:55,200 I will provide action, like lower. Then I will enter so that 189 00:10:55,200 --> 00:10:56,100 it has to work. 190 00:10:56,100 --> 00:10:58,000 [no audio] 191 00:10:58,000 --> 00:11:03,300 Right. Now if you pass in this way you have to use your Sys Module, 192 00:11:03,500 --> 00:11:07,900 and you can handle them - string as your first argument, right 193 00:11:08,900 --> 00:11:10,700 and action as second argument. 194 00:11:11,000 --> 00:11:14,700 Now, what I am doing is, instead of using 'input' now I am going 195 00:11:14,700 --> 00:11:18,700 to comment these two things. Now, I am going to import your 196 00:11:18,700 --> 00:11:25,000 Sys Module, then 'usr_str' I am taking, guys is if you 197 00:11:25,000 --> 00:11:26,700 remember 'sys.argv' 198 00:11:26,700 --> 00:11:30,100 is going to give your all command line arguments, right. 199 00:11:30,100 --> 00:11:34,900 In that zeroth argument is the script name. Then see if you 200 00:11:34,900 --> 00:11:35,900 are running in this way, 201 00:11:35,900 --> 00:11:38,500 if you are running in this way, let me do that thing first, otherwise. 202 00:11:39,800 --> 00:11:42,000 I will comment your entire code. 203 00:11:45,500 --> 00:11:50,100 Right. Now what I am doing is I'm going to print your 204 00:11:50,100 --> 00:11:53,200 'print(sys.argv)' first 205 00:11:54,300 --> 00:11:56,700 Right. Now, let me run this and see the output. 206 00:11:57,700 --> 00:12:01,400 If you run in this way, you're getting a list inside of your 207 00:12:01,400 --> 00:12:06,000 script with the help of 'sys.argv'. In that index 0 is script 208 00:12:06,000 --> 00:12:10,300 name, index 1 is your given string, and index 2 is user 209 00:12:10,300 --> 00:12:12,900 action. That's what you are getting by running in this way 210 00:12:12,900 --> 00:12:14,600 with the help of 'sys.argv'. 211 00:12:16,100 --> 00:12:20,800 Right. So guys whenever if you want to pass an argument, 212 00:12:20,900 --> 00:12:23,400 which consists of some spaces then you have to write your 213 00:12:23,400 --> 00:12:25,100 entire string inside of quotation. 214 00:12:26,500 --> 00:12:31,100 Right. Now see. Now because with the help of 'sys.argv' you're 215 00:12:31,100 --> 00:12:34,900 able to get your command line arguments inside of your script. 216 00:12:34,900 --> 00:12:42,300 That's why what I am doing is, 'usr_str' I can assign index 1, right. 217 00:12:42,600 --> 00:12:50,000 So 'sys.argv' index 1 I am assigning for user string, and user action 218 00:12:51,300 --> 00:12:56,500 'sys.argv' index 2. That's it. 219 00:12:58,000 --> 00:13:01,700 Right. Now, let me run your script and see the magic. Now, 220 00:13:01,700 --> 00:13:06,200 I'm not going to use 'input' command, and I'm not going to provide 221 00:13:06,500 --> 00:13:09,500 multiple lines input for your script, only single step, just 222 00:13:09,500 --> 00:13:11,600 running this way. See the output. You're getting your string 223 00:13:11,600 --> 00:13:14,500 in lower case. Maybe you are getting confusion because the entire 224 00:13:14,500 --> 00:13:16,100 thing is in lowercase. Let me do 225 00:13:16,100 --> 00:13:20,100 [no audio] 226 00:13:20,100 --> 00:13:21,100 in this way. 227 00:13:21,100 --> 00:13:23,000 [no audio] 228 00:13:23,000 --> 00:13:25,900 Right. See the output. You are getting string in lowercase or 229 00:13:25,900 --> 00:13:27,500 let me pass here upper. 230 00:13:27,500 --> 00:13:31,200 [no audio] 231 00:13:31,200 --> 00:13:32,400 Let me past title. 232 00:13:32,400 --> 00:13:34,300 [no audio] 233 00:13:34,300 --> 00:13:35,700 Yes, it's perfectly working. 234 00:13:35,700 --> 00:13:38,000 [no audio] 235 00:13:38,000 --> 00:13:41,200 Right. But here a small drawback 236 00:13:41,200 --> 00:13:44,200 is there. What does that mean? By mistake suppose if I'm not 237 00:13:44,200 --> 00:13:46,300 passing this, you're getting an error. 238 00:13:47,600 --> 00:13:51,400 "List index out of range", because you are passing only script 239 00:13:51,400 --> 00:13:52,500 name and string name. 240 00:13:52,500 --> 00:13:55,700 What about action? Action is always index 2 position you 241 00:13:55,700 --> 00:13:58,100 have taken inside of your script, but you are not passing three 242 00:13:58,100 --> 00:14:00,900 values. If you pass three values, then you can take index 243 00:14:00,900 --> 00:14:04,300 3. You're passing only two values, that is script name and 244 00:14:04,300 --> 00:14:05,200 then string name. 245 00:14:05,200 --> 00:14:06,100 This is zeroth index. 246 00:14:06,100 --> 00:14:08,900 This is first index. Then what about second index? You are 247 00:14:08,900 --> 00:14:11,600 not passing, but you are sending that. You are sending that 248 00:14:11,600 --> 00:14:13,100 index 2 value to this action. 249 00:14:13,600 --> 00:14:16,900 So without having second index value, how can you assign 250 00:14:16,900 --> 00:14:19,200 that value to other variables? That's not possible. 251 00:14:19,300 --> 00:14:20,400 That's why you're getting an error. 252 00:14:21,100 --> 00:14:25,300 Right? So whenever if you are using command-line arguments with 253 00:14:25,300 --> 00:14:28,200 the help of 'sys.argv' you have to take care. 254 00:14:28,800 --> 00:14:33,100 What is that care? How you can take care, right? Nothing 255 00:14:33,100 --> 00:14:37,000 is there. Very simple. Observe that. So guys for our script 256 00:14:37,000 --> 00:14:40,500 for our script as of now, whatever we are working using lower/ 257 00:14:40,500 --> 00:14:41,900 upper/ title format, 258 00:14:42,500 --> 00:14:45,500 how many command line arguments we need? One is script name, 259 00:14:46,400 --> 00:14:50,200 second one is string, third one is action. 260 00:14:50,400 --> 00:14:54,400 So three values you are passing then only it is correct. 261 00:14:54,500 --> 00:14:56,800 If you pass two values, then it is not correct. 262 00:14:57,900 --> 00:15:01,400 Even if you pass more than three, no need of four values. 263 00:15:01,800 --> 00:15:05,700 That's why what I am doing is, before running, before assigning 264 00:15:05,900 --> 00:15:08,000 your command line arguments to your variable, 265 00:15:08,000 --> 00:15:14,400 first of all I am finding, 'print("The number of command line arguments")'. 266 00:15:14,400 --> 00:15:18,100 [no audio] 267 00:15:18,100 --> 00:15:21,100 If you remember, if you have a list, if you have 268 00:15:21,100 --> 00:15:25,400 a list you can find out number of values in that by using 269 00:15:25,400 --> 00:15:26,400 'length' function. 270 00:15:26,600 --> 00:15:30,800 So 'sys.argv' is a list, right and I'm finding number of values. 271 00:15:31,600 --> 00:15:33,500 Let me comment your entire code first. 272 00:15:33,500 --> 00:15:38,200 [no audio] 273 00:15:38,200 --> 00:15:39,800 Now run your script and see the output. 274 00:15:39,900 --> 00:15:42,800 Yes, you are getting three because you're passing script 275 00:15:42,800 --> 00:15:46,100 name, first command line argument, second command line argument. 276 00:15:46,100 --> 00:15:48,800 If I pass in this way, what is that? 277 00:15:48,800 --> 00:15:51,000 [no audio] 278 00:15:51,000 --> 00:15:53,900 Yeah, with only, suppose something 279 00:15:53,900 --> 00:15:55,800 [no audio] 280 00:15:55,800 --> 00:15:59,400 then you're getting two. Whenever if you get two, that is not 281 00:15:59,400 --> 00:16:01,900 correct. You're not running your script in the proper way 282 00:16:02,800 --> 00:16:05,600 because you need three values finally here, including 283 00:16:05,600 --> 00:16:09,400 your script name - script name, your string, and then your action. 284 00:16:10,100 --> 00:16:13,600 That's why what I am doing is whenever if this value is not 285 00:16:13,600 --> 00:16:19,000 equal to 3, right. 'if', see that. 'if length' of your 286 00:16:19,000 --> 00:16:27,400 'sys.rgv', if it is not equal to 3, if it equals 3, no problem. 287 00:16:27,400 --> 00:16:33,200 If it is not equal to 3 then I can't assign second argument 288 00:16:33,200 --> 00:16:35,100 value to your user action. 289 00:16:35,100 --> 00:16:37,000 [no audio] 290 00:16:37,000 --> 00:16:37,800 Not equal to 3 means 291 00:16:37,800 --> 00:16:41,500 assume that you are sending only two values, then it's not correct 292 00:16:41,500 --> 00:16:45,600 no. That's why whenever if your number of arguments are 293 00:16:45,600 --> 00:16:49,000 not equals to three, maybe two or more than three, 294 00:16:49,400 --> 00:16:52,700 I need only exactly three including your script name - script 295 00:16:52,700 --> 00:16:54,600 name, string name, and action. 296 00:16:55,600 --> 00:16:58,700 It should have three only. In case if it is not equal to 3 297 00:16:58,800 --> 00:17:02,100 then I want to stop my script. You know, you can stop your 298 00:17:02,100 --> 00:17:04,098 script using 'sys.exit'. Previously 299 00:17:04,000 --> 00:17:06,200 we discussed this right in previous video. 300 00:17:06,200 --> 00:17:08,000 [no audio] 301 00:17:08,000 --> 00:17:09,098 Now, see the magic. 302 00:17:10,500 --> 00:17:13,700 Now you don't get any error. Previously whenever if you don't 303 00:17:13,700 --> 00:17:14,598 have this logic, 304 00:17:15,598 --> 00:17:18,000 let me cut it and save it and run it 305 00:17:18,000 --> 00:17:21,800 once again. You are getting an error, right? "index range" 306 00:17:21,800 --> 00:17:24,200 "IndexError: list index out of range". 307 00:17:24,500 --> 00:17:28,800 But what I am doing is I am saving this now with the help of this simple 308 00:17:28,800 --> 00:17:32,500 'If' condition. Now, I am running. You're not getting any error. 309 00:17:32,900 --> 00:17:35,800 Simply your Python is stopping your script whenever if you are not 310 00:17:35,800 --> 00:17:40,800 passing proper command line arguments. But simply stopping 311 00:17:40,800 --> 00:17:44,500 is not a good practice. Whenever if you're not passing exactly 312 00:17:44,500 --> 00:17:48,099 three arguments then simply you are stopping, right. Before stopping 313 00:17:48,099 --> 00:17:50,400 just try to write some message. 'print 314 00:17:52,099 --> 00:17:58,300 "Usage of this script". "Usage:". "Usage: is, 'print' your 315 00:17:58,300 --> 00:18:04,700 'sys.argv', 'argv', script name, then after that what you have to do? 316 00:18:05,600 --> 00:18:08,300 Right? Let me do 'f' strings. 317 00:18:08,300 --> 00:18:10,300 [no audio] 318 00:18:10,300 --> 00:18:17,500 So usage I'm writing, so print your script name then, then 319 00:18:18,000 --> 00:18:19,400 here you have to pass, 320 00:18:20,600 --> 00:18:21,700 pass your string. 321 00:18:23,300 --> 00:18:24,200 Your required string 322 00:18:24,200 --> 00:18:27,600 I'm writing suppose, "your required string" here. 323 00:18:28,200 --> 00:18:33,500 Then here you have to write action. Action is like lower or 324 00:18:33,500 --> 00:18:36,600 [no audio] 325 00:18:36,600 --> 00:18:38,100 upper or title. 326 00:18:39,800 --> 00:18:40,800 That's it. 327 00:18:40,800 --> 00:18:45,000 Now, I am saving this and I am going to run. Guys try to understand 328 00:18:45,000 --> 00:18:46,300 this. It's very useful. 329 00:18:47,100 --> 00:18:48,100 I am running in this way. 330 00:18:48,400 --> 00:18:51,100 Then you are getting usage is, you have to run this script 331 00:18:51,100 --> 00:18:55,500 in this way, "action_on_strings.py", then your string, then some action. 332 00:18:55,500 --> 00:18:57,900 you have to provide like lower or upper or title. 333 00:18:58,100 --> 00:19:00,300 Now, I am going to provide in the proper way. Suppose 334 00:19:00,300 --> 00:19:03,100 I am running upper, now it's working properly. 335 00:19:03,600 --> 00:19:07,100 If you are not passing command-line arguments perfectly, 336 00:19:07,300 --> 00:19:08,700 then only are getting usage, 337 00:19:08,700 --> 00:19:11,000 otherwise it's working perfectly, right? 338 00:19:11,500 --> 00:19:13,400 So guys finally here 339 00:19:13,500 --> 00:19:16,500 we got the concept of 'sys.argv', 340 00:19:16,500 --> 00:19:18,500 and 'sys.exit' as well. 341 00:19:19,400 --> 00:19:22,800 So that's why 'sys.exit' is, 'sys.exit' 342 00:19:22,800 --> 00:19:25,900 is very helpful in your real-time. Why we are using? Just 343 00:19:25,900 --> 00:19:27,800 to stop your script. When we are stopping? 344 00:19:28,000 --> 00:19:30,500 If you are not passing proper command line arguments. 345 00:19:30,800 --> 00:19:32,900 If you are not passing proper command line arguments what 346 00:19:32,900 --> 00:19:35,800 will happen? Because of this line you are getting an error. 347 00:19:36,000 --> 00:19:37,200 I don't want to get an error. 348 00:19:38,300 --> 00:19:41,300 Right? That's why. When you will get an error? If you are not passing 349 00:19:41,300 --> 00:19:42,200 proper arguments. 350 00:19:42,500 --> 00:19:45,400 So whenever if you are not passing proper arguments, I am 351 00:19:45,400 --> 00:19:48,600 going to stop my script by simply saying this information to the user. 352 00:19:48,600 --> 00:19:51,800 "Please run this script in this way". Usage of script 353 00:19:51,800 --> 00:19:55,100 I am giving, and then I am stopping. Then next time whenever 354 00:19:55,300 --> 00:19:57,300 the user is going to run your script 355 00:19:57,400 --> 00:20:01,800 he will take care how to run a script, right? Okay. 356 00:20:02,800 --> 00:20:04,900 Okay guys, thank you for watching this video. 357 00:20:05,200 --> 00:20:08,000 It is very, very important while writing your scripts 358 00:20:08,000 --> 00:20:11,100 'sys.argv', right. Fine. 359 00:20:11,100 --> 00:20:19,700 [no audio]