Importing Libraries
- 01:56
Learn how to use third-party packages in Python to leverage functions not usually available in Python, such as importing the math module to use the natural logarithm function.
Downloads
Transcript
In Python, it's also important to be able to use other people's code that they create outside the default functions that you get just in plain vanilla Python. When you open up Jupyter Notebooks, you've probably used Excel add-ins from third parties like Bloomberg or Capital IQ, or maybe FactSet. Similarly, when you're using Python, you're often going to import third party packages containing custom functions. For example, let's say that we were doing some math and we needed to get the natural logarithm of 10, so I'm gonna say log 10. Well, when I execute that, I'm going to get a problem because there is no natural logarithm function function as a default in Python. So what I can do is import the math module and then under that call the natural log function.
And so what this does is it imports a module of code called math, and then it calls the custom log function inside of that module math, and it applies that function to the number 10. You can use this in a lot of different ways. There's a ton of super useful open source code available that you can import into your Python code that lets you use functions that other people have created so you don't have to rewrite the code from scratch yourself. And as a quick note on vocabulary, a module is a collection of functions, and a package is a collection of modules. A library is another word you're going to hear, and a library is essentially the same as a package. So when you hear me talking about a module or a package or a library, I'm talking about code that you are importing so that you can use somebody else's work and leverage it in your own code.