37 Implementation of if__name__ == __main__ in Python main File.

Опубликовано: 23 Февраль 2026
на канале: Free Tutorial TV
5
1

Hey guys,

welcome to Free Tutorial TV and Python tutorial for beginner.

and in this video we will talk about this

special keyword which is underscored 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 P Y and the other is test dot P Y.

Both of 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 add and takes two arguments

A and B and it's going to return some of A and B.

So we have already seen how to create these kind of four 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 P Y file into the

test dot P Y file.

and now I'm going to just use print. and then I'm going to use this model name which is my math to

call this function. which is there inside this my math dot P Y file.

And then I'm going to provide two values here once again into this add function.

So let's say 7 and 6.

Now let me run this test dot P Y file first in which I'm importing this my math dot P Y files.

so right click

then run test which is going to run this program.

And what you will see here is a prints 26 which is the sum of 10 and 16 is 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 the test dot P Y.

Now I have a simple question.

Is this a correct response.

Because I just want to call the add metod from my mad dot P Y file into the test dot P Y. 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 P Y as a module.

And we just want to use the functions inside my math dot P Y file not any special code which is written

here.

So that's where we use the special condition which is if underscore the underscore name underscore underscore

is equal to underscore underscore the main underscore underscore.

And under this condition we use whatever code we want to execute. and this name is our special built in

key word in Python whose value is equal to either them or doing them.

Which is my math or it's 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 is this value which is main.

So whenever you run this my Mad dot P Y file as the main python file.

So whenever I run this file which is my math P Y 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'm going run this code and you will see the value of this special variable becomes

underscore.

Underscore main underscore underscore..

So whenever you run this my math dot P Y as the main file . then only the value of the special keyword

underscore underscore name underscore underscore becomes this main value.

Now if I import this my math module into some other file for example test dot P Y and then use the function's

from this module then let's see what happens. so I'm going to just right click on the test dot P Y .

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.

OK.

So my model name is my math dot P Y.

So here I just print the model name and you will also see that because we have provided this condition

this statement is not executed . and only that 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 P Y 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.



**************************************************************************
please like and follow us in Facebook and Instagram.
facebook:   / technologytv  


Instagram:   / heikhamsingh  


Twitter:   / hanandkumarsing