- 1. if __name__ == "__main__" in python explained | Advanced python Free YouTube Video
- 2. [Hindi] *args and **kwargs in python explained | Advanced python tutorials in Hindi Free YouTube Video
- 3. Try, Except, Else and Finally in python explained | Advanced python tutorials Free YouTube Video
- 4. Virtual Environment (venv) in python explained | Advanced python tutorials Free YouTube Video
- 5. Iterators, Iterables and Generators in python explained | Advanced python tutorials Free YouTube Video
- 6. List Comprehension, Dictionary Comprehension And Generator Comprehension Free YouTube Video
- 7. Map, Filter and Reduce in Python | Advanced Python Free YouTube Video
- 8. Lambda functions in python explained | Advanced python Free YouTube Video
- 9. Advanced List Slicing In Python Explained | Advanced python tutorials Free YouTube Video
- 10. Bisect Module In Python Explained | Advanced python tutorials Free YouTube Video
- 11. Enumerate Function In Python Explained | Advanced python tutorials Free YouTube Video
- 12. Format Function In Python Explained | Advanced python tutorials Free YouTube Video
- 13. Join Function In Python Explained | Advanced python tutorials Free YouTube Video
What’s up, guys? In this article, I am taking you toward advanced python! If you have no knowledge about python and you want to start coding in python then you can see this video first:
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:
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.
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:
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()
You need to be logged in to post a comment!
Any Course related announcements will be posted here