Hello guys and welcome to Python programming
tutorials by amuls academy. So in many Python programs you may have noticed
one condition like this. So if you wonder what is this condition and
why we use this, then you'll get the answer in this tutorial alright. So first we will discuss about the “name”
variable, so we will write this variable as underscore underscore then followed by name
next underscore underscore ok. So this is an built-in variable and it will
return the name of the module ok each module contains one attribute called “__name__”
which will return the name of that module. so module is nothing but a Python file which
contains Python definition and statements ok and if you want to know in which module
you are working then you can use this variable “__name__” and you can find out ok. So now if i enter here we can see we are working
in the module called “__main__”. So “__main__” is a module which will execute
the top level code so this “__name__” variable will be set to “__main__” when
we will execute our program as the main program. If our file being imported by some other module
then name will be assigned to the module name ok i know it is bit confusing so we will it
take one example. So in a Python file, first i will define a
function ok my function name is “mul” okay it contains two parameters and it will
return a*b that's nothing but it will multiply two numbers and it will return the result
so now outside this function i will print that result ok so i will call that function
“mul” and i will pass the number as two and three. Here i want to multiply two and three and
i want result that's why here we are calling this function and we are writing this function
call inside the print. so it will print the result. Next what i'll do is, i want to print the
name of the module ok so i'll use “__name__” variable which is an built in variable ok
so now i will save this and run this and here we can see 6 , so 2* 3 , 6 and here we can
see name of the module is “__main__” so here this program is executed as the main
program OK so the name of the module is “__main__” . So here we can see before this name we will
use two underscore after this name we will use two underscore that is because this convention
is used for special variables or methods because it will do some special things like it will
return the name of the module so it is written like this ok so this convention is used for
Special variables or methods in Python. ok so here we wrote the program to multiply
two numbers right, so next in the another Python file i want to do the same thing that
is i want to multiply two numbers but here i want to multiply two floating point numbers
okay instead of writing this function again what i'll do is, i'll import this Python file
to this file ok so i will write import and here i will write filename that is,
I wrote “multiply” because here we can see name of this file is “multiply” dot
py and i want to import this file so here i wrote import multiply ok. Next what i'll do is, i'll print now i want
to use “mul” function to use that function i need to write like this, multiply dot mul,
ok here i will pass 2.3 and 1.1 ok two floating point numbers and again i want to print the
name of the module ok i want to print the name of the module that's why i wrote this
ok now what i'll do is, i'll save this file and run this and here we can see the output
6, multiply 2.53 and “__main__” . Ok here when i import this multiply Python
file ok it will execute that file ok first it will execute that file ok that's why here
at the beginning we can see 6 and multiply because here we are importing this multiply
module or Python file that's why here we can see we got the name as multiply as i said
if a Python file is imported by some other file then the name will be assigned as the
name of the module name. Next it will execute this multiply 2.3 into
1.1 so answer is 2.53 so it will print that next here we can see we took print name so
now this program is running as a main program ok that's why here we got the module name
as “__main__” ok. So we executed this program and we got the
output like this, we got the output of multiply.py file also but here we don't want that we don't
want the output of imported file ok we only want this output so to get that, in the multiply.py
file what i'll do is, here i’ll write if “__name__ is __main__” then only i want
to Print this so that's nothing but when this file is run as the main program then only
i want to print the output, when this file is imported that time i don't want to print
this result that's why here we wrote the condition like this when name is equal to equal to main
then only i want to print this. Now i will save this ok, ok here this is the
file which is being imported by this file ok so here now i'll execute this file so save
and run this and here we can see we got 2.53 and “__main__” only right when a file
is being imported by some other file it will print the result of imported file also but
we don't want that that's why we will use this condition ok so when __name__ is equal
equal to “__main__” is nothing but when we will run this program as the main program
then only i want to print this result okay. So that's it for now thank you for watching
don't forget to subscribe to my channel i'll meet you in next class till then take care.