Python Tutorial for Beginners 40 - Idea behind : if __name__ == "__main__"

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome to the next video on Python to troll for beginners in this video we will talk about this special keyword which is underscore underscore name underscore underscore and we will see in which situations we use this keyword and why do we use this special if statement which is name is equal to underscore underscore main underscore underscore so let's get started so to start with you can see I have two files here one is my math dot py and other is test dot py both the files are in the same directory in my project so now what I will do here is I will define a very simple method which is ad which takes two argument a and B and it's going to return the sum of a and B so we have already seen how to create these kind of simple functions and now what I'm going to do is I'm going to use this print function to call this add function and here I'm going to just give two numbers here so nothing special about this code now what I'm going to do next is I'm going to import this file which is my math dot py file into that test dot py and now I'm going to just use print and then I'm going to use this module name which is my math to call this function which is there inside this my math dot py file and then I'm going to provide two values here once again into this add function so let's say seven and six now let me run this test dot py file first in which I'm importing this my math dot py file so right click and then run test which is going to run this program and what you will see here is it prints 26 which is the sum of 10 and 16 we just printed using this print statement and then you will see 13 which is printed using this print statement which is the sum of these two numbers which we have provided in test dot py now I have a simple question is this a correct response because I just want to call the add method from my math dot py file into the test dot py and I don't want to print any result which is coming from this print statement because I just want to use my math dot py as a module and we just want to use the functions inside this my math dot py file not any special code which is written here so that's where we use this special condition which is if underscore underscore name underscore underscore is equal to underscore underscore main underscore underscore and under this condition we use whatever code we want to execute and this name is a special built-in keyword in Python whose value is equal to either the module name which is my math or its value is equal to underscore underscore main underscore underscore so under what condition the value of underscore underscore name becomes the module name and in what condition underscore underscore name underscore underscore contains this value which is mean so whenever you run this my math dot py file as the main Python file so whenever I run this file which is my math dot py file then the value of name becomes this which is underscore underscore main underscore underscore let's print the value of this variable and then we will see how this value changes in different conditions so once again I am going to run this code and you will see the value of this special variable becomes underscore underscore main underscore underscore so whenever you will run this my math dot py the main file then only the value of this special keyword underscore underscore name underscore underscore becomes this mean value now if I import this my math module into some other file for example test dot py and then use the functions from this module then let's see what happens so I'm going to just right click on the test dot py now and then I'm going to run it and you will see the value of this underscore underscore name underscore underscore is now is equal to the name of your module okay so my module name is my math dot py so here it just prints the module name and you will also see that because we have provided this condition this statement is not executed and only the statement which we have written here is executed because this condition checks for the name value which checks whether we are running this my math dot py file as the main file or we are importing this file into the other file and we are just using the functions inside that module so this condition is similar to for example main method in the C++ or the Java programming language and what does the main method do in those two languages the main function is the main entry point of your program so this condition is performing the same operations we can run these two files using the terminal also so in PyCharm you can just click on this option which says terminal which opens the terminal in your pycharm ide in the same folder in which you are currently working in and then I can use this Python command to call this my math dot py file first of all so I'm going to just use my math dot py which is going to run this my math dot py and you can see if we run my math dot py as the main file then the value of this special keyword name becomes mean otherwise when we are going to run this test dot py file in which we are just importing this my math dot P I file and then when I press Enter you will see that now the value of this name variable becomes the name of your module which is my math so in this way you can use the special condition in Python I hope you have enjoyed this video and I will see you in the next video you
Info
Channel: ProgrammingKnowledge
Views: 18,615
Rating: 4.9334722 out of 5
Keywords: Python (Programming Language), Python 3.6, Python, Python 3.x.x, Programming Language (Software Genre), Python Tutorial, Python Tutorial for Beginners, Python for Beginners, Python course, python tutorial, python scripting tutorial, Online Course, Python Guru, Learn Python, Python 3, Functions in Python, Python Class, Python Class examples, python 3 class, Python Exception handling, Raising Exceptions, __name__, Python main
Id: EJHeSRKWtfM
Channel Id: undefined
Length: 7min 27sec (447 seconds)
Published: Thu Sep 20 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.