Motive There are different types of modules in python which not everyone knows so we thought of introducing them to you guys. Let's start! Note: This is not a tutorial of how to use these but is to show that these modules also exist and if you want to know more you can visit there own page and if you want us to explain leave a comment below. Links to there page are also given below. Hope you found something useful. 1.Wikipedia Yes, there is a module called Wikipedia in python, you can easily install Wikipedia by pip install wikipedia . We cannot rely on this module for advanced use. It was designed for ease of use and simplicity. Learn More Here . Here is a simple example: import wikipedia wikiPython = wikipedia.summary("Python", sentences=2) print(wikiPython) One big disadvantage is that it works only if the internet is connected to your device. You can even control the number of sentences that you
Motive
When I was a beginner I had this big doubt while watching tutorials on YouTube. I saw people using this if __name__ == '__main__' even though it was not required. So, I thought of learning the special variables in python. So I thought that this will be useful for you guys also.
Let's Learn
First I created a folder named special variables and then created a new python file called mainProgram and tried printing __name__ in this file like this:
I got the result as __main__ in the python shell. Then I tried printing its type like so:
Now I got <class 'str'> in the python shell. So that's why we use inverted commas for __main__ (since it is a string).
Now let's create a new python file called importedProgram, in the same folder and import the file python file while updating the mainProgram to print(__name__) instead of print(type(__name__)) and try running the importedProgram with the following code:
Now if we run this file the code in the mainProgram gets executed, so print(__name__) runs. In the mainProgram, we got the result as __main__, but if we execute the importedProgram we get mainProgram in the python shell, the name of the program that we imported.
Now let's make some changes in the mainProgram to see how if __name__ == '__main__' works so now let's change the code.
Here we edited the mainProgram so that if the __name__ variable gives __main__, we print This is not imported as we know that this program was not imported since it gives __main__. And if __name__ == 'mainProgram' we can say that this was imported and thus print 'This is imported'.
So now if we run the mainProgram we get __main__ and This is not imported in the python shell. And if the run the importedProgram we get mainProgram and This is imported in the python shell. This line of code is used if we do not want something to run if it is imported as a library and to run only in the main file.
If you have any doubts or errors in the program feel free to comment down below and I will respond as early as possible. And if I have made any mistake please correct me in the comment box below. Don't forget to follow us for such python related content.
Comments
Post a Comment