LEARN PYTHON NOW! Book: The Pillars of Python. #16 — The libraries

“Even if tomorrow were the day of the end of the world, I would still plant apple trees today.” — Martin Luthe

The libraries or modules in the programming languages are code packages already written by someone with the aim of solving a particular problem.
For example, let’s imagine that we are working with areas of geometry and have to calculate the area of thousands of geometric shapes. If we want to do this task we will have to write the functions of the different geometric bodies such as the square, triangle, cube, cylinder…

“A library with filled bookshelves and blurred light bulbs hanging from the ceiling” by Janko Ferlič on Unsplash
def triangle(b,h): 
return (b*h)/2
def cube(l): 
return l**3

Once we have these functions written down, all we have to do is call these functions. For example:


But what if someone had already done that work for us and saved all those functions in a file or set of files. Well, that’s exactly the point of libraries, to reuse the code and not to do what is commonly called programming like reinventing the wheel.

Although what we have just done is very simple, there are many libraries that cover more complex aspects of programming and are even highly optimized with other low-level languages such as the Machine Learning library created by Google, Tensorflow.

How could we create a library and reuse the code we just created?
It’s very easy, all you have to do is save the file you just created with the name you want. For example, geometric and then call our library from the same code.

There are three ways to call a library, the latter being the most widely used.

1/ This form is not recommended because we are importing all the functions that we had created previously and it could happen that if we continue importing libraries the functions could overlap causing an error or what is worse still that it does not warn us that there is an error but the result is totally different from what we expected.

from geometric import *

2/ The second way is to call the library using the “import” command.

import geometric

3/ The third way is to assign a “nickname or alias” to the library in this way, every time we use a function we will have to call that alias and follow it with a period.

import geometric as geo

But the interesting thing is that you can import many more libraries from third parties. In addition, after having installed anaconda we already have more than 100 widely used libraries installed.

Do I have to learn the functions or methods of each of the libraries?

No, it’s impossible.

What are the most important python libraries?

It depends on what you plan to develop or specialize in. For example, Pandas, numpy and SciPy are used in Data Science. In Machine Learning, tensonrfow, scikit-learn and seaborn are used for data visualization. If you use python as a scrapping tool on websites then you will use scrapy and pandas.

As advice, don’t obsess over libraries or learn all their functions, but learn their logic behind them, understand the basics and even try to do your own functions in order to keep learning.

— — — — — — — — — — — — — — The End — — — — — — — — — — — — —

If you like this small and free magazine you can help us by simply sharing it or subscribing to the publication. My name is Rubén Ruiz and I work in Artificial Intelligence in the financial industry and as a personal project I run this little magazine where we experiment with Artificial Intelligence… until the computer explodes :) You can follow me on:

Instagram (Personal life, it’s fun) => @rubenruiz_t

Youtube (Channel about AI, try to make it fun )=> Rubén Ruiz A.I.

Github (Where I upload my code, this is not so much fun anymore) => RubenRuizT

Source: Deep Learning on Medium