1 00:00:00,000 --> 00:00:00,800 [no audio] 2 00:00:00,800 --> 00:00:02,100 Friends, here we are going to 3 00:00:02,100 --> 00:00:04,800 see how to you use functions of one Python 4 00:00:04,800 --> 00:00:08,000 Script into another Python Script, and what is 5 00:00:08,000 --> 00:00:10,000 '__', name variable, 6 00:00:11,100 --> 00:00:15,100 and finally we are going to see how to create user-defined modules. 7 00:00:16,300 --> 00:00:19,400 First, let me start with a simple Python Script. 8 00:00:19,400 --> 00:00:21,400 [no audio] 9 00:00:21,400 --> 00:00:25,200 Let me create a simple Python Script called 'script1.py'. 10 00:00:26,400 --> 00:00:29,900 Now here I am going to define two functions, one is for addition. 11 00:00:29,900 --> 00:00:32,200 [no audio] 12 00:00:32,299 --> 00:00:33,800 Now let me 'print(" 13 00:00:35,300 --> 00:00:36,300 The addition of 14 00:00:39,300 --> 00:00:45,400 and is", simply 'a + b'. 15 00:00:45,400 --> 00:00:48,200 [no audio] 16 00:00:48,200 --> 00:00:49,800 Then let me write 17 00:00:49,800 --> 00:00:51,200 'return None'. 18 00:00:52,700 --> 00:00:56,900 And let me write one more function, 'subtraction(a,b)', 19 00:00:58,600 --> 00:00:59,600 so 'print 20 00:00:59,600 --> 00:01:02,000 [no audio] 21 00:01:02,000 --> 00:01:10,800 ("The subtraction of and is", so simply 22 00:01:10,800 --> 00:01:13,400 . 23 00:01:14,400 --> 00:01:15,400 That's it. 24 00:01:15,600 --> 00:01:18,100 Now, 'None'. Fine. 25 00:01:18,700 --> 00:01:20,300 Now, these are the two functions. 26 00:01:20,300 --> 00:01:22,500 Now, let us assume that we have two variables. 27 00:01:22,500 --> 00:01:26,500 Let's say 'x' equals to some number, 28 00:01:28,800 --> 00:01:30,900 and 'y' equals to some number. 29 00:01:31,400 --> 00:01:35,200 Now, I want to get the addition and subtraction for these two 30 00:01:35,200 --> 00:01:37,400 numbers. So already we defined functions. 31 00:01:37,400 --> 00:01:41,500 That's why simply I am calling 'addition' function by passing 32 00:01:41,500 --> 00:01:44,500 (x,y), and 33 00:01:44,500 --> 00:01:46,400 [no audio] 34 00:01:46,400 --> 00:01:48,500 next time I'm calling 'subtraction' function 35 00:01:48,700 --> 00:01:51,100 simply by calling your 'x' and 'y'. Now 36 00:01:51,100 --> 00:01:52,500 let me save it and run it. 37 00:01:54,100 --> 00:01:55,100 What happened? 38 00:01:55,100 --> 00:01:59,300 [no audio] 39 00:01:59,300 --> 00:02:00,300 Okay. 40 00:02:00,300 --> 00:02:02,400 [no audio] 41 00:02:02,400 --> 00:02:03,500 Spelling mistake. 42 00:02:03,500 --> 00:02:06,300 [no audio] 43 00:02:06,300 --> 00:02:10,100 Now see the result. You're getting 11 and 3. 7 + 4, 11; 44 00:02:10,100 --> 00:02:12,500 7 - 4, 3 you're getting. That's fine. 45 00:02:13,699 --> 00:02:15,800 Now this is your 'script1'. Now 46 00:02:15,800 --> 00:02:18,800 actually I want to write one more script, 47 00:02:20,000 --> 00:02:24,300 but there I want to find addition, subtraction, and multiplication 48 00:02:24,300 --> 00:02:26,600 of two numbers, right? 49 00:02:27,000 --> 00:02:29,800 So what I can do, one thing directly I can copy these two 50 00:02:29,800 --> 00:02:36,600 functions here and I can write one more function simply for multiplication. 51 00:02:36,600 --> 00:02:40,500 [no audio] 52 00:02:40,500 --> 00:02:41,500 Right. 53 00:02:46,500 --> 00:02:51,800 "The multiplication of and is", 54 00:02:53,200 --> 00:02:55,400 simply ''. 55 00:02:55,600 --> 00:02:56,400 That's fine. 56 00:02:57,900 --> 00:03:01,300 But as of now just for your addition and subtraction you 57 00:03:01,300 --> 00:03:02,800 are having very simple lines. 58 00:03:02,900 --> 00:03:05,700 Just assume that you are writing a code in real time. 59 00:03:05,700 --> 00:03:07,600 [no audio] 60 00:03:07,600 --> 00:03:09,100 Right. So there 61 00:03:10,300 --> 00:03:12,700 you have for a function 62 00:03:12,700 --> 00:03:16,600 some hundred lines. First for 'addition' function just assume 63 00:03:16,600 --> 00:03:19,800 that. First 'addition' function you have hundred lines. For 64 00:03:19,800 --> 00:03:21,600 'subtraction' function you have hundred lines. 65 00:03:21,900 --> 00:03:24,900 Then unnecessarily you are going to copy two hundred lines 66 00:03:24,900 --> 00:03:28,100 of code into your current script from your previous script. 67 00:03:28,100 --> 00:03:30,100 [no audio] 68 00:03:30,100 --> 00:03:33,900 Now what I want to do is, I don't want to copy these two functions 69 00:03:33,900 --> 00:03:38,300 here, but I want to use directly from 'script1'. Your 70 00:03:38,300 --> 00:03:39,900 'addition' and 'subtraction' functions 71 00:03:39,900 --> 00:03:42,300 I want to use directly from your 'script1'. 72 00:03:43,000 --> 00:03:47,000 Now, first of all let me give the second script name as 73 00:03:47,000 --> 00:03:48,800 'script2.py'. 74 00:03:50,200 --> 00:03:56,800 Right. Fine. Now, first of all let me define any two variables. 75 00:03:56,800 --> 00:04:01,300 So I am defining 10 and 20, right? 76 00:04:01,300 --> 00:04:05,400 First of all simply I'm finding multiplication of your 'x' 77 00:04:05,400 --> 00:04:07,600 and 'y' from your current script itself. 78 00:04:08,400 --> 00:04:09,900 Yes, you are getting your result. 79 00:04:10,700 --> 00:04:16,399 But now I need to find addition and subtraction of this 'x,y', 80 00:04:16,399 --> 00:04:19,700 I mean 'x' and 'y' variables. So directly I can write 81 00:04:19,700 --> 00:04:23,399 the logic but my intention is if already function is there 82 00:04:23,399 --> 00:04:27,700 in any other script how we can use that in the current script? 83 00:04:28,600 --> 00:04:31,700 Right. So first of all what I am doing is, I am going to comment 84 00:04:31,700 --> 00:04:34,000 these lines, why I will tell you. 85 00:04:35,200 --> 00:04:37,700 But actually you should not disturb other's script, right? 86 00:04:38,000 --> 00:04:41,100 But as of now just I'm commenting. Fine. 87 00:04:41,900 --> 00:04:46,100 Now we are under 'script2', but we want to use the functions 88 00:04:46,100 --> 00:04:47,100 from 'script1'. 89 00:04:47,200 --> 00:04:50,500 So in that case, what you have to do is first of all at very 90 00:04:50,500 --> 00:04:54,500 first line simply you have to write 'import script1'. 91 00:04:55,500 --> 00:04:58,600 Don't write '.py'. 'import script1'. 92 00:04:59,700 --> 00:05:01,000 So this is 'script1', right? 93 00:05:01,000 --> 00:05:02,900 So we are importing 'script1'. 94 00:05:03,800 --> 00:05:04,800 That's it. 95 00:05:05,600 --> 00:05:09,100 Now, let me run it and see the output. There is no error. 96 00:05:10,300 --> 00:05:15,100 Right. Now in 'script1' you have functions anyway, 'addition' 97 00:05:15,100 --> 00:05:17,100 and 'subtraction', right? 98 00:05:17,100 --> 00:05:19,600 So in case if you want to know what are the functions are 99 00:05:19,600 --> 00:05:24,000 there in 'script1' directly you have a logic called 'dir'. 100 00:05:24,900 --> 00:05:28,800 See that 'addition' and 'subtraction' functions are there. 101 00:05:29,900 --> 00:05:34,000 Right. Anyway, we know that what are the functions are there in 'script1'. 102 00:05:34,000 --> 00:05:36,200 [no audio] 103 00:05:36,200 --> 00:05:39,300 Now what I am doing is from 'script1' 104 00:05:39,500 --> 00:05:42,300 I want to call 'addition'. Now 105 00:05:42,300 --> 00:05:47,900 my values are 10, 20. Then what is the result? 10 + 20, addition. 106 00:05:47,900 --> 00:05:48,900 30, you're getting. 107 00:05:50,500 --> 00:05:51,900 Now from 'script1' 108 00:05:51,900 --> 00:05:53,800 I need to call subtraction as well. 109 00:05:56,500 --> 00:05:57,900 Then you are going to get -10. 110 00:05:58,600 --> 00:05:59,600 That's it. 111 00:06:00,000 --> 00:06:04,200 Right. Now, we are not writing any logic for addition and subtraction 112 00:06:04,300 --> 00:06:06,900 in the current script. What we are doing? Already 113 00:06:06,900 --> 00:06:09,800 we have functions for addition and subtraction in another 114 00:06:09,800 --> 00:06:13,400 script, we are trying to use that logic by importing your 'script1'. 115 00:06:14,700 --> 00:06:19,100 So first of all, this is the way how we can use other script 116 00:06:19,100 --> 00:06:20,900 functions in the current script. 117 00:06:20,900 --> 00:06:23,000 [no audio] 118 00:06:23,000 --> 00:06:28,100 That's fine. But while doing this process we did, we commented this code, 119 00:06:28,100 --> 00:06:30,900 but actually you should not comment because that is someone's 120 00:06:31,200 --> 00:06:33,500 code, right? You should not go and disturb that. 121 00:06:35,200 --> 00:06:38,200 Be clear. Now I am going to run our code. 122 00:06:38,200 --> 00:06:39,500 Let me run it only for 123 00:06:39,500 --> 00:06:41,800 [no audio] 124 00:06:41,800 --> 00:06:43,400 'addition', okay. 125 00:06:43,400 --> 00:06:44,900 First of all let me comment it. 126 00:06:44,900 --> 00:06:47,500 [no audio] 127 00:06:47,500 --> 00:06:49,800 So why I'm commenting, you need to get clarity. 128 00:06:51,200 --> 00:06:52,600 Actually, you should not comment. 129 00:06:52,700 --> 00:06:54,700 First of all, let me run this and see the result. 130 00:06:55,100 --> 00:07:00,500 So addition of 10 and 20 is 30 you're getting. Now, this is 'script1'. 131 00:07:01,200 --> 00:07:02,300 Right. Now your script 132 00:07:02,300 --> 00:07:06,000 is there as it is. We uncommented that code, right? 133 00:07:06,000 --> 00:07:09,300 We don't know what is the logic there in 'script1', but we know only 134 00:07:09,500 --> 00:07:11,800 'addition' function is needed from 'script1'. 135 00:07:11,800 --> 00:07:14,800 That's why simply I'm importing 'script1', and I am calling 'addition' 136 00:07:14,800 --> 00:07:17,900 function from that. Now, let me run it and see the result. 137 00:07:19,500 --> 00:07:22,300 Actually, you need only this output but why you are getting 138 00:07:22,300 --> 00:07:23,400 these two lines? 139 00:07:24,400 --> 00:07:26,800 This is for 'script1' purpose, not for us, right. 140 00:07:27,900 --> 00:07:29,700 Whatever the logic, you are getting output, right. 141 00:07:29,700 --> 00:07:32,000 This is for 'script1' purpose, not for us. 142 00:07:32,200 --> 00:07:35,700 We need only 'addition' for 10 and 20. 143 00:07:35,700 --> 00:07:37,600 We need only this output, why you are getting 144 00:07:37,600 --> 00:07:38,900 unnecessarily these two lines? 145 00:07:40,200 --> 00:07:45,000 Right. So that's why generally while implementing Python 146 00:07:45,000 --> 00:07:46,800 Script you need to follow some 147 00:07:48,000 --> 00:07:53,700 procedure, some steps. They are, let me write it. See first of 148 00:07:53,700 --> 00:07:56,600 all guys, what will happen whenever if you try to import any 149 00:07:56,600 --> 00:07:57,900 script or any module? 150 00:07:58,000 --> 00:07:59,600 This is like importing module, right? 151 00:08:01,000 --> 00:08:04,500 See, whenever if you import 'script1' your Python will 152 00:08:04,500 --> 00:08:06,600 go and execute your 'script1' first. 153 00:08:08,000 --> 00:08:10,700 So while executing 'script1 what will happen? This is the 154 00:08:10,700 --> 00:08:13,500 'script1' code. Your Python is going to execute from top to 155 00:08:13,500 --> 00:08:18,000 down. While executing it from top to down in 'script1' also 156 00:08:18,000 --> 00:08:20,000 we have 'addition' and 'subtraction' calling, 157 00:08:20,700 --> 00:08:22,800 so that's why you're going to get output for these two. 158 00:08:23,900 --> 00:08:26,500 But we don't want that. In our current script 159 00:08:26,500 --> 00:08:28,500 we don't want that, right? 160 00:08:29,100 --> 00:08:35,100 So that's why generally while implementing any script, right, 161 00:08:35,100 --> 00:08:38,700 you have to write some 'main' function, inside of that 'main' function 162 00:08:38,700 --> 00:08:40,700 [no audio] 163 00:08:40,700 --> 00:08:43,700 you need to execute your main logic. 164 00:08:43,700 --> 00:08:48,900 [no audio] 165 00:08:48,900 --> 00:08:52,700 So this is first of all standard way to write scripts 166 00:08:53,100 --> 00:08:55,200 using Python. Right. Now 167 00:08:55,200 --> 00:08:59,200 we are calling 'main'. Even though if you do this still you're 168 00:08:59,200 --> 00:09:00,800 going to get same output. Observe that. 169 00:09:02,000 --> 00:09:05,600 Now I am running, rerunning. See the result. Unnecessarily you're 170 00:09:05,600 --> 00:09:10,100 getting two lines of code, extra code, but I want to get only this line. 171 00:09:11,000 --> 00:09:13,800 I don't want these two lines, right? 172 00:09:14,100 --> 00:09:17,500 So that's why what I am doing is, while calling 'main' function in 173 00:09:17,500 --> 00:09:19,600 'script1' I am writing a simple logic. 174 00:09:19,600 --> 00:09:24,000 [no audio] 175 00:09:24,000 --> 00:09:25,800 Don't worry, first observe the output. 176 00:09:25,800 --> 00:09:32,300 [no audio] 177 00:09:32,300 --> 00:09:33,900 Right. Now see the result. 178 00:09:33,900 --> 00:09:37,800 I am running. Now, you are not getting your 'script1' output. 179 00:09:37,900 --> 00:09:41,500 You are getting only from 'script2' output. This is what we need. 180 00:09:41,500 --> 00:09:44,100 [no audio] 181 00:09:44,100 --> 00:09:47,300 Right. The same way whenever if you are writing code, you 182 00:09:47,300 --> 00:09:50,200 need to follow this structure for your Python, 183 00:09:50,800 --> 00:09:53,100 then that is a good Python script. That's it. 184 00:09:53,100 --> 00:09:54,100 Nothing is there. 185 00:09:54,100 --> 00:09:57,500 [no audio] 186 00:09:57,500 --> 00:10:01,500 So how you are going to overcome getting output from 'script1'? 187 00:10:01,500 --> 00:10:03,400 I will show you, I will explain that. 188 00:10:03,400 --> 00:10:09,500 [no audio] 189 00:10:09,500 --> 00:10:10,500 Now, 190 00:10:17,100 --> 00:10:18,100 that's it. 191 00:10:18,100 --> 00:10:21,200 [no audio] 192 00:10:21,200 --> 00:10:24,800 See that. And go to your 'script1', and run your 'script1'. 193 00:10:24,800 --> 00:10:26,900 So you're going to get your 'script1' output as well. 194 00:10:28,200 --> 00:10:30,700 There is no change in the output. It's working perfectly. 195 00:10:32,200 --> 00:10:37,800 Right. So now you need to understand about name variable, 196 00:10:37,800 --> 00:10:40,300 '__name__'. 197 00:10:41,700 --> 00:10:48,700 Right. Fine. Now, see. First let me comment entire code in your 198 00:10:48,700 --> 00:10:51,200 'script1' as well as in 'script2'. 199 00:10:51,200 --> 00:11:00,300 [no audio] 200 00:11:00,300 --> 00:11:03,200 Now what I am doing is simply I am printing 201 00:11:03,200 --> 00:11:06,600 [no audio] 202 00:11:06,600 --> 00:11:11,000 '__name', that is a variable like 'x', predefined 203 00:11:11,000 --> 00:11:12,100 variable, right? 204 00:11:12,200 --> 00:11:15,200 I am running. See the result what you are getting. '__main__'. The 205 00:11:15,200 --> 00:11:19,500 same way that variable, even I am trying to print from 206 00:11:19,500 --> 00:11:21,000 your second script as well. 207 00:11:21,000 --> 00:11:23,600 [no audio] 208 00:11:23,600 --> 00:11:25,400 You're getting '__main__', right. 209 00:11:25,800 --> 00:11:27,100 Now, what I am doing is, 210 00:11:28,800 --> 00:11:29,800 this is 211 00:11:31,000 --> 00:11:35,500 from 'script1', right. Now, let me run it. 212 00:11:35,900 --> 00:11:36,900 Yes, that's fine. 213 00:11:37,300 --> 00:11:40,700 Now, same way I am trying to write here, 214 00:11:42,200 --> 00:11:45,600 "This is from script2". 215 00:11:51,800 --> 00:11:53,900 Now, let me run it. Fine. 216 00:11:53,900 --> 00:11:58,400 So whenever if you're running your 'script2', and 217 00:11:58,400 --> 00:12:01,000 if you are printing '__name' variable value 218 00:12:01,000 --> 00:12:04,000 you're getting '__main__', or in 'script1' 219 00:12:04,000 --> 00:12:06,400 also whenever if you try to print '__name' 220 00:12:06,400 --> 00:12:08,300 '__name' variable value 221 00:12:08,300 --> 00:12:10,400 you are getting '__main__' only with underscores. 222 00:12:11,600 --> 00:12:12,600 Right. That's fine. 223 00:12:13,200 --> 00:12:17,300 Now, what I am doing is, I am trying to 'import script1'. 224 00:12:17,300 --> 00:12:22,500 [no audio] 225 00:12:22,500 --> 00:12:27,600 Right. See whenever if you 'import script1' first what your 226 00:12:27,600 --> 00:12:31,000 Python will do means, your Python will go and execute completely 227 00:12:31,000 --> 00:12:35,100 'script1' because of this line. Because of this line your 228 00:12:35,100 --> 00:12:38,400 Python will go and execute your 'script1' first completely. 229 00:12:39,700 --> 00:12:41,500 Right. First observe the output. 230 00:12:41,500 --> 00:12:43,500 [no audio] 231 00:12:43,500 --> 00:12:45,500 "This is from script1", but 232 00:12:45,500 --> 00:12:48,900 what you are getting? You are getting 'script1' not 'main' 233 00:12:50,000 --> 00:12:53,900 And because of this line, this is from 'script2', you 234 00:12:53,900 --> 00:13:00,700 are getting 'main'. That means whenever if you run any script 235 00:13:00,700 --> 00:13:05,400 directly, then 'name' variable value equals to 'main' with underscores. 236 00:13:06,600 --> 00:13:08,300 The variable name is 'main'. 237 00:13:09,400 --> 00:13:14,600 But we are running this 'name', this code indirectly from 'script2' 238 00:13:14,600 --> 00:13:18,500 right. 'script1' means, your 'import script1' means 239 00:13:18,500 --> 00:13:20,700 you are running 'script1' code, but you are running 240 00:13:20,700 --> 00:13:24,700 indirectly from 'script2', you're not running directly 'script1'. 241 00:13:25,100 --> 00:13:28,200 We are not running this code directly. 'script2' is running 242 00:13:28,200 --> 00:13:31,800 'script1' code. In that case that 'name' variable value 243 00:13:31,800 --> 00:13:35,100 is equals to 'script1', not 'main. 244 00:13:36,300 --> 00:13:37,300 Be clear. 245 00:13:38,500 --> 00:13:43,300 If you run simply your code directly, and in that case your 246 00:13:43,300 --> 00:13:50,300 variable value is 'main', but if you run any code indirectly, 247 00:13:50,500 --> 00:13:53,600 if you observe here 'import script1' is running because 248 00:13:53,600 --> 00:13:56,100 of 'script2', while running 'script2' you're also running 249 00:13:56,100 --> 00:13:59,200 'script1'. That means 'script2' is running 'script1'. 250 00:13:59,400 --> 00:14:02,900 You're not directly running 'script1'. So at that time whatever 251 00:14:02,900 --> 00:14:06,300 the variable is there inside of 'script1' that variable, 252 00:14:06,300 --> 00:14:11,600 'name' variable value will become the name of that script, right. 253 00:14:12,100 --> 00:14:14,300 Now, observe here. 254 00:14:14,300 --> 00:14:17,600 [no audio] 255 00:14:17,600 --> 00:14:19,200 Now, let me comment this code. 256 00:14:20,900 --> 00:14:26,000 Now because of 'import script1' what will happen? Your code 257 00:14:26,000 --> 00:14:27,400 will run from top to down. 258 00:14:27,400 --> 00:14:29,600 [no audio] 259 00:14:29,600 --> 00:14:35,400 Because of 'script1', 'import script1', I mean from 'script2' 260 00:14:35,400 --> 00:14:38,800 you are importing 'script1'. Whenever if you do 'import 261 00:14:38,800 --> 00:14:41,400 script1', at the time what will happen? Your Python will 262 00:14:41,400 --> 00:14:46,100 run first entire 'script1', right. Now observe that. In 263 00:14:46,100 --> 00:14:49,600 'script1' what we have? So while running your 'script1' 264 00:14:49,600 --> 00:14:50,600 from top to down 265 00:14:51,800 --> 00:14:55,300 function, anyway your Python won't execute as of now, function, 266 00:14:55,700 --> 00:14:59,100 your Python won't execute as of now, function again, 267 00:14:59,100 --> 00:15:00,900 your Python won't execute as of now. Simply 268 00:15:00,900 --> 00:15:03,600 it will remember all these three functions. Now, 269 00:15:03,600 --> 00:15:08,200 this is the 'main' line, 'if __name', so you 270 00:15:08,200 --> 00:15:09,400 are not running directly 271 00:15:09,400 --> 00:15:11,800 this 'script1'. Your 'script2' is running, 272 00:15:11,800 --> 00:15:15,000 that's why 'name' value is equal to 'script1'. 273 00:15:16,100 --> 00:15:21,700 Now this is equals to 'script1', not 'main' now, right. If it is 'script1' 274 00:15:21,700 --> 00:15:24,300 then they are not equal, right? This value and this value, 275 00:15:24,300 --> 00:15:28,000 they are not equal. If they are not equal then 'if' doesn't 276 00:15:28,000 --> 00:15:29,600 allow to execute 'main' function. 277 00:15:30,000 --> 00:15:32,100 It doesn't allow to call 'main' function. 278 00:15:32,400 --> 00:15:35,900 If you are not calling 'main' function, then you are not calling these lines. 279 00:15:35,900 --> 00:15:38,000 If you are not calling these lines, then you are not getting 280 00:15:38,000 --> 00:15:40,400 any output from function one, 281 00:15:40,400 --> 00:15:43,000 sorry from 'script1' in 'script2'. 282 00:15:43,800 --> 00:15:45,400 That's it. So that you are going to, 283 00:15:45,400 --> 00:15:47,400 [no audio] 284 00:15:47,400 --> 00:15:48,600 what you're doing? So that 285 00:15:48,600 --> 00:15:52,400 you are going to avoid getting output from 'script1'. 286 00:15:53,200 --> 00:15:56,700 So you are going to get output only whenever if you call functions. 287 00:15:56,700 --> 00:15:59,100 [no audio] 288 00:15:59,100 --> 00:16:01,500 Right. Now, see that. 289 00:16:02,700 --> 00:16:07,300 Now we are on 'script2', so whenever if you run your Python 290 00:16:07,300 --> 00:16:10,600 Script, what will happen? Very first line 'import'. Yes, that will 291 00:16:10,600 --> 00:16:14,200 execute. 'import script1' is nothing but you are going to run 'script1'. 292 00:16:14,800 --> 00:16:16,000 Anyway, you know in 'script1' 293 00:16:16,000 --> 00:16:17,100 what is the logic there, 294 00:16:18,400 --> 00:16:19,400 right? 295 00:16:21,600 --> 00:16:25,800 Then after execution of all the code from top to down 296 00:16:25,800 --> 00:16:28,200 what your Python will do? Simply your Python will remember 297 00:16:28,200 --> 00:16:29,900 all the functions names. 298 00:16:30,800 --> 00:16:32,700 That's it. Then it will come back to 'script1'. 299 00:16:33,700 --> 00:16:35,000 Now in 'script1' 300 00:16:35,000 --> 00:16:36,600 we defined one multiple, 301 00:16:36,600 --> 00:16:40,900 I mean 'multiplication' function, one 'main' function, right but 302 00:16:40,900 --> 00:16:44,900 be clear, already you know 'main' function from here, but that 303 00:16:44,900 --> 00:16:48,300 is from 'script1', not from 'script2'. Now this is from 'script2'. 304 00:16:48,300 --> 00:16:50,000 Your Python can distinguish that. 305 00:16:51,700 --> 00:16:53,700 Then after that you have 306 00:16:53,700 --> 00:16:57,000 'main'. 'if __name__ == "__main__"'. 307 00:16:57,000 --> 00:17:00,200 [no audio] 308 00:17:00,200 --> 00:17:06,300 Right. Now you are running 'script2' directly, that's why 'name' 309 00:17:06,300 --> 00:17:09,098 variable if it is equal to 'main', both are equal. 310 00:17:09,098 --> 00:17:12,700 If both are equal your 'if' condition will allow to execute 311 00:17:12,700 --> 00:17:18,098 this 'main'. Then you are coming to here. Then 'x = 10', 312 00:17:18,098 --> 00:17:21,900 'y = 10'. Then from 'script1' you're calling 'addition'. 313 00:17:22,598 --> 00:17:26,000 Now your Python will go to 'script1', and there it will try to 314 00:17:26,000 --> 00:17:29,599 execute 'addition' function, then we'll come back to here. That's it. 315 00:17:29,599 --> 00:17:34,599 [no audio] 316 00:17:34,599 --> 00:17:36,400 That's it. So now, 317 00:17:37,700 --> 00:17:40,099 by doing in this way what we are doing? 318 00:17:40,099 --> 00:17:42,200 [no audio] 319 00:17:42,200 --> 00:17:46,099 If you observe, 'script1' is now a module for your 'script2'. 320 00:17:46,099 --> 00:17:48,099 That's why you are importing 'script1' here. 321 00:17:48,099 --> 00:17:50,099 [no audio] 322 00:17:50,099 --> 00:17:51,300 Right. So 323 00:17:51,300 --> 00:17:55,600 guys, now, from now onwards while going forward whenever if 324 00:17:55,600 --> 00:17:57,900 you're trying to import your, sorry whenever if you are 325 00:17:57,900 --> 00:18:01,700 trying to create any Python Script, now you need to follow 326 00:18:01,700 --> 00:18:05,300 this structure. That is in case if you want to import anything 327 00:18:05,300 --> 00:18:06,400 you can import here, 328 00:18:06,600 --> 00:18:12,000 some 'sys', 'os', or 'time', some modules, or you can also import 329 00:18:12,000 --> 00:18:19,800 line-by-line. 'import os'. 'import time'. Right. 'import datetime' module. 330 00:18:20,200 --> 00:18:21,200 Let me save it. 331 00:18:21,200 --> 00:18:24,400 [no audio] 332 00:18:24,400 --> 00:18:36,700 'newstructureforpythonscript.py'. Right. Then after 333 00:18:36,700 --> 00:18:42,500 that very first line is always you try to import, you try to write in this way. 334 00:18:42,500 --> 00:18:48,800 'If __name__ == "__main__"underscore 335 00:18:49,000 --> 00:18:52,400 then call 'main' function. Now try to define somewhere your 336 00:18:52,400 --> 00:18:56,400 'main' function. Now from here onwards try to write your main 337 00:18:56,400 --> 00:18:59,900 logic. Suppose I want to find the addition of any numbers, 338 00:18:59,900 --> 00:19:03,500 let's say 'x = 1', 'y = 2'. Now directly 339 00:19:03,500 --> 00:19:06,600 I don't want to find, or if you want to directly find, yes 340 00:19:07,000 --> 00:19:10,400 simply write 'x + y'. That's it. 341 00:19:10,400 --> 00:19:12,000 Sorry 'x + y'. 342 00:19:12,000 --> 00:19:14,400 [no audio] 343 00:19:14,400 --> 00:19:16,200 Then simply, this is a function 344 00:19:16,200 --> 00:19:17,000 that's why at the end 345 00:19:17,000 --> 00:19:18,100 just write 'return None'. 346 00:19:19,000 --> 00:19:20,000 That's it. 347 00:19:20,800 --> 00:19:23,500 But I don't want to directly write my addition here, then 348 00:19:23,500 --> 00:19:27,700 just try to define one function for that, 'addition(x,y)'. 349 00:19:27,700 --> 00:19:33,700 Somewhere just write 'addition(a,b)'. Then try 350 00:19:33,700 --> 00:19:45,200 to print, "The addition of your and is simply 351 00:19:45,200 --> 00:19:47,800 ''. That's it. 352 00:19:48,200 --> 00:19:49,500 So I'm not returning anything. 353 00:19:49,500 --> 00:19:51,700 Simply write 'return None'. 354 00:19:52,000 --> 00:19:53,500 That's it. Now see the result. 355 00:19:53,500 --> 00:19:54,700 We are getting same output. 356 00:19:54,700 --> 00:19:56,900 [no audio] 357 00:19:56,900 --> 00:20:01,200 Right. So guys while going forward, please follow this structure. 358 00:20:01,300 --> 00:20:04,700 Always try to start with your code with 'if' condition, 359 00:20:04,700 --> 00:20:10,800 'if __name__== "__main__", then call 'main' function, and 360 00:20:10,800 --> 00:20:13,000 try to write your logic from 'main' function. 361 00:20:14,200 --> 00:20:17,600 So what is the advantage by writing this means, whenever if 362 00:20:17,600 --> 00:20:21,800 you are trying to import this code into other scripts at 363 00:20:21,800 --> 00:20:25,000 that time you are avoiding execution of your 'main' block. 364 00:20:24,800 --> 00:20:26,800 [no audio] 365 00:20:26,800 --> 00:20:30,100 In case if you don't write this logic, unnecessarily you are 366 00:20:30,100 --> 00:20:32,900 going to execute your code in other scripts. 367 00:20:32,900 --> 00:20:36,200 [no audio] 368 00:20:36,200 --> 00:20:40,500 Right. So, so that's why while going forward we are trying to follow 369 00:20:40,500 --> 00:20:43,000 this structure while writing Python Scripts. 370 00:20:44,200 --> 00:20:46,500 Right. If you write in this way, then this is nothing but 371 00:20:46,700 --> 00:20:51,700 creation of your own modules, or you are defining your required 372 00:20:51,700 --> 00:20:54,400 modules, or this is called user-defined module. 373 00:20:54,700 --> 00:20:56,200 That's it. Simple way 374 00:20:56,200 --> 00:20:57,200 I'm explaining here. 375 00:21:00,900 --> 00:21:02,900 Okay guys, thank you for watching this video. 376 00:21:02,900 --> 00:21:10,400 [no audio]