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.
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:
__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?
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()
import tut1a tut1a.mostimpfunction() tut1a.main()
this is amazing harry bhai
i have a query why from tkinter import * is not working in spyder IDE is ther any solution for this
Harry Bhai Mujhe Python me thode doubt hi Kay me pooch saktha hu
<b>Hello Harry Bhai</b>
Any Course related announcements will be posted here