Excerpt
The beauty of Python is that we have a collection of modules and packages which have been created for a certain purpose and the fact that it is open-source makes it incredibly easy for one individual to build on top of another person’s work and create something useful. Thus, you can get a simple code for performing simple arithmetic operations or a group of code, called modules and packages, which can help perform data analysis, all on the internet.
What are Modules and Packages?
Consider writing code directly on the Python or IPython console. The definitions that we create(functions and variables) will be lost if we quit the console and enter it again. Therefore, in order to write a longer program, we might consider switching to a text editor to prepare an input for the interpreter and running it with that file as an input instead. This is known as writing a script.
As a program gets longer, we may want it to split it into several small files for easier maintenance. Also, we may want to use a handy function that we have written in several programs without copying its definition into each program. To support this, Python has a way to put a code definition in a file and use them in another script or directly in an interactive instance of the interpreter. Such a file is called a module; definitions from a module can be imported into other modules or in the program that we code.
Packages can be considered as a collection of modules. It is a way of structuring Python’s module namespace by using “dotted module names”. For example, the module name matplotlib.pyplot designates a submodule named pyplot in a package named matplotlib.
Packaging modules in such a way saves the author of different modules from having to worry about each other’s global variable names and the use of dotted module names saves the author of multi-module packages from having to worry about each other’s module names.
Stay tuned for the next installment in this series. The authors will discuss Importing Python packages.
Disclosure: Interactive Brokers
Information posted on IBKR Campus that is provided by third-parties and not by Interactive Brokers does NOT constitute a recommendation by Interactive Brokers that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.
This material is from QuantInsti and is being posted with permission from QuantInsti. The views expressed in this material are solely those of the author and/or QuantInsti and IBKR is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation to buy, sell or hold such security. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.
In accordance with EU regulation: The statements in this document shall not be considered as an objective or independent explanation of the matters. Please note that this document (a) has not been prepared in accordance with legal requirements designed to promote the independence of investment research, and (b) is not subject to any prohibition on dealing ahead of the dissemination or publication of investment research.
Any trading symbols displayed are for illustrative purposes only and are not intended to portray recommendations.