if __name__ == "__main__" in python explained | Advanced python

What’s up, guys? In this article, I am taking you towards advanced python! If you have no knowledge about python and you want to start coding in python then you can see this video first:

https://youtu.be/qHJjMvHLJdg

Everything is explained in a simple way. Even if you are new to this line, you would understand it.

Now I am assuming that you know the basics of python. So, let's get started and start writing our first python program. Open Pycharm and create a new file in it

Note: Keep in mind that the file name should not match any module name.

Reason: Basically whatever module we install is stored and recognized as folders/files by the computer. If you want to see these folders/modules then follow this path:

C:\Users\User\AppData\Local\Programs\Python\Python38-32\Lib\site-packages

Tip: AppData is a hidden folder so you may have to change the settings to show hidden files.

When you import a module it is supposed to be imported from this place. That’s why we don’t name any file by module name because then it will just import the local file which you made by the module name.

Why do we use if __name__ == "__main__"?

Now if you have made your own module type-of file which consists of a lot of functions. If you import that file and it contains print statements then those lines will also play, now we don’t want that. Which is why we use if __name__==”__main__”. It is not just for print statements, we can put all the lines we don’t need in that ‘if’ block.

Explanation:

Basically what is an if statement? It means if this statement is true then play this code.

So what we are saying is if its value is __main__ then run the code block.

It is like saying:

__name__ = "__main__"
if __name__ == "__main__":
    print("Not imported!")

where __name__ is just like a variable whose value is __main__. If we are using the original file and not importing it then ‘if’ statement is true and the code in that ‘if’ block runs, otherwise it doesn’t. Simple!

Let me show you some easy ways to understand:

See, it is compiling both lines because nothing is imported and if the statement is true. Whereas,

Now it is compiling only print statement and the whole ‘if’ block is not compiled because it is imported.

But what if there were an else block? Let me show you:

In the main file else block is not compiled because ‘if’ statement is true. But what if we import it?

It will then play else block because ‘if’ is false for this file. Interesting, isn’t it?

#tut1a.py file as described in the video

import os
def mostimpfunction():
    print("harry is a coder")

print(__name__)

def main():
    print(os.listdir("/"))
    print("Harry is great and he is the king of US")

if (__name__=="__main__"):
    main()

#tut1b.py file as described in the video

import tut1a
tut1a.mostimpfunction()
tut1a.main()

Comments(31)

root2 1 year, 2 months ago
this is amazing harry bhai
tejaslade 1 year, 1 month ago
.
Urmil 4 months ago
amazing content harry bhai
milindeac 1 year, 2 months ago
i have a query why from tkinter import * is not working in spyder IDE is ther any solution for this
sg330 6 months ago
nh
pritish0101 5 months, 1 week ago
Simply  use VS Cod
pritish0101 5 months, 1 week ago
*e
tejaslade 1 year, 1 month ago
1
tejaslade 1 year, 1 month ago
1
Anirudh 1 year ago
Harry Bhai Mujhe Python me thode doubt hi Kay me pooch saktha hu
jeevan 12 months ago
demo
RohanDas28 11 months ago
Amazing Bhai
harry 11 months ago
Thanks bhai!
ParisHacker 8 months, 3 weeks ago
Ye Rohan
yogeshsss 5 months, 3 weeks ago
hey
ParisHacker 8 months, 3 weeks ago
<b>Hello Harry Bhai</b>
ParisHacker 8 months, 3 weeks ago
</pre><b>Hello Harry Bhai</b>
ParisHacker 8 months, 3 weeks ago
<img src onerror="alert('hello Harry Bhai')">
bk 7 months, 3 weeks ago
awesome video harry bro..
A.Basit 6 months, 2 weeks ago
gr8 job, keep it up!
sg330 6 months ago
this is awesome
yogeshsss 5 months, 3 weeks ago
well
pritish0101 5 months, 1 week ago
Awesome Course
zeeshan 5 months, 1 week ago
hello
zeeshan 5 months, 1 week ago
zxdfhjsdahf
zeeshan 5 months, 1 week ago
sadasdfsa
zeeshan 5 months, 1 week ago
sdfsdf
aqaaaa 4 months, 2 weeks ago
<
aqaaaa 4 months, 2 weeks ago
<img src onerror='alert('Msg')'/>
iam_imran313 4 months, 1 week ago
bhai mujhe ek programing code milega
Talha1818 4 months ago
sb milega yahan

Resources

  1. File - Download here
  2. File - Download here
  3. File - Download here
  4. File - Download here
  5. File - Download here
  6. File - Download here
  7. File - Download here
  8. File - Download here

Course Announcements

Any Course related announcements will be posted here