Which Python Library Should You Learn First

Introduction

Which Python Library Should You Learn First
Which Python Library Should You Learn First

We will go over Which Python Library Should You Learn First.

Did you know that you can use Python is a versatile language and can help you cover any domain?

Below we will go over Which Python Library Should You Learn First from different perspectives.

We will break down this in the following sections:

  • Network Engineer
  • Machine Learning Specialist
  • Generalist
  • System Engineer

I have used Python since its inception and continue to do so even up to date. It’s been a long-term relationship with me and Python and I believe I know well the ins and outs to get you started if you are now beginning your journey you landed in the right spot.

I will go point by point here to guide you depending on what your destination is, if you don’t know this yet don’t worry I have a generalist section for you that is perfect for every beginner.

Which Python Library To Learn First As a Generalist

In this section, we will go over some libraries that you must know as your first library to use in Python. They are generally very versatile and useful and can make a difference for your initial stages with Python.

Random

The random library is one of the handiest examples of how you can cover all your needs to generate random numbers, sequences and character buffers. As you know in programming it’s almost unavoidable that you will hit an RNG situation of something happens. What’s more important is to have a toolkit in your armor that helps you do this. Take this example below on how to generate a random number in the range of 1-10 using the random library:

>>> import random
>>> random.randint(1,10)
2
>>> random.randint(1,10)
5

If we had to implement this from scratch it would have taken a significant amount of coding to do it right as there are a lot of factors that go into it such as seeding, selecting a proper random source etc. With the help of the random library this was a breeze.

Itertools

Another useful library you will want to have in your arsenal is itertools. Generally, this helps you create a lot of combination and statistical type sequences that may be required to solve a problem. The advantage of itertools is that you can surely implement for example all the permutations or combinations of a sequence of numbers but can you do it efficiently? Well itertools is here to save you to ensure you have that Big-O optimization in your code to make it run fast without issues. Let’s take an example of generating all permutations of a given list as shown below:

>>> import itertools
>>> perms = itertools.permutations([4,11,-1,3])
>>> for perm in perms: print(perm)
...
(4, 11, -1, 3)
(4, 11, 3, -1)
(4, -1, 11, 3)
(4, -1, 3, 11)
....

With just a few lines of code we were able to efficiently and fast generate a list of permutations of a given list. This shows how powerful of a tool itertools is and how much its feature set expands in your general day-to-day statistical problem-solving.

Statistics

A very useful library that you may be using in your day-to-day coding is the statistics library. This library is packed with useful functions related to mathematics and general arithmetic when trying to pull statistical data. It helps you calculate averages, find the middle value high or low and a lot of other handy things that will definitely come to use when needed. As an example, we will be demonstrating how you can find the average on a sequence of numbers that you are processing.

>>> import statistics
>>> statistics.median([1,9,100,3,5])
5

As shown above, this was easily achievable using the median function of the statistics library. If you had to do this manually you would have had to write an optimized loop along with some buffer storage for the current average.

Which Python Library To Learn First As A System Engineer

In this section, we will go over which Python library you must learn first as a System engineer. Basically, when you are doing system development there are a lot of things that you do with the filesystem but also with running processes in your system so we will focus on those two sections and outline which libraries Python offers us here to get the job done.

OS

Start with commands it’s a must-learn library as it basically lets you get an interface between Python and your system. OS system for example allows you to execute things in your running shell environment whether it is Linux, Mac or Windows, and get the output of those. Essentially it’s like middleware between your system and Python in order to interpret the command output that you have received. In the example below we will use the library to execute a system command and see the output that we get more specifically we will run the uptime to see how long the system has been up and running. Do note that if the command is not available in your system it will simply fail. So in Windows you can’t run the uptime command as it does not exist.

>>> import os
>>> os.system('uptime')
10:41  up 1 day, 11:45, 3 users, load averages: 1.35 1.89 1.82
0

As shown above the uptime for my system currently is 1 day and a few hours. It must be noted here that OS offers a lot more cool features such as getting system paths, changing directories and generally anything related you may want to do on interacting with your operating system.

PSUtil

PSUtil is another useful library that may be useful to learn as your first Python library. It basically offers a way for you to getting information on your system running processes along with specific information that has to do with them. More specifically you can see memory usage, CPU, Identifiers and a lot of other useful information. Let’s look how easily we can do this:

>>> import psutil
>>> for process in psutil.process_iter(): print(process)
...
psutil.Process(pid=0, name='kernel_task', status='running', started='2022-10-12 22:56:52')
psutil.Process(pid=1, name='launchd', status='running', started='2022-10-12 22:56:52')
psutil.Process(pid=309, name='logd', status='running', started='2022-10-12 22:57:09')
psutil.Process(pid=310, name='UserEventAgent', status='running', started='2022-10-12 22:57:09')
psutil.Process(pid=312, name='uninstalld', status='running', started='2022-10-12 22:57:09')
psutil.Process(pid=313, name='fseventsd', status='running', started='2022-10-12 22:57:09')

As you can see the above command started listing processes running into my system, along with their name pid and start time. You can use the same module to monitor completely your system. In fact I have written an extensive article on this which you may find here:

How To Use Python To Monitor Your Mac Resources

I believe PSUtil is a must library if you are going to be doing System development and want to learn it as your first Python library.

Pathlib

Moving on to Pathlib is another useful library that’s more related to low system stuff regarding your filesystem. More specifically it gives you an interface between Python and your filesystem. It essentially lets you handle files in an agnostic way from what operating system you are using. If you are on Windows which uses forward instead of backslash that is not an issue for Pathlib it can handle both and all normalizing your function flow and code to make it more readable with less boilerplate and also letting it run in all operating systems.

>>> import pathlib
>>> pathlib.os.chdir('..')
>>> pathlib.os.getcwd()
'/Users/alex/code'

In the code above we are using Pathlib in two ways, first to descend a directory and then print out the running Path. Note in this case I was using a Mac but that same code will also work on Windows, Linux and BSD variants. Pathlib is great because with a few lines you can have cross-compatible running code.

Which Python Library To Learn First As a Machine Learning Specialist

If you are in the market to become a machine learning specialist I have assembled a list below for you which Python library to learn first. In the list I tried to split it up based on your specialization of Machine learning you will be doing.

spaCy / NLTK

Starting with Natural Language Processing I think spaCy or NLTK are both very good options if you are a beginner and want to do work with understanding text and identifying entities or even classifying documents. I have written extensive articles on both of these libraries which you can find below so I’m not going to go into extensive details here:

The articles above are complete guides with examples to get you started.

Tensorflow / Pytorch

Moving on to more data analytics, prediction and generally identification of patterns in pictures is Tensorflow and Pytorch. Both of those variants are very powerful with a great community and let you do wonders with just a few lines of code. Training your models to do what you want and setting up your false positive functions to handle and improve them is a breeze with just a few lines of code.

Numpy

Numpy is another great tool that lets you perform operations in numbers in a very fast pace. It basically outperforms all of the Python built-in mathematical functions as it’s generally optimized for speed and speed alone. This essentially lets you get things done if you are in the machine learning world without waiting and scaling them to a great extent.

Lets see an example of how we create an array in numpy and sort it.

>>> import numpy
>>> np_array = np.array([3,4,55,11])
>>> np.sort(np_array)
array([ 3,  4, 11, 55])

If you were to do the same thing using the Python sort the speed will not be as fast this is a given. Basically numpy excells at performing all those operations at great speed greatly improving your code.

Pandas

Pandas basically goes hand in hand with Numpy I’d say the two complement each other. It essentially lets you create and process files such as CSV, Excel spreadsheets with a lot of machine learning data in them into two dimensional arrays which then can be processed using Numpy or other Machine learning tools to create datasets from them.

>>> import pandas
>>> csv_data = pandas.read_csv('seed.csv')
>>> csv_data
   Key     Start Date  End Date       Location               Count
0  stocks  2022-01-01  2022-02-01     local-test             2
1  stocks  2022-02-02  2022-03-01     local-test             2

In the example above we easily read a CSV file that contained some seeded data from a machine learning test we did on the stock exchange. This Pandas now datatype object can be processed as a two dimensional array in an easier and faster manner.

Which Python Library To Learn First As a Network Engineer

In this last section I want to talk about which Python library to learn first if you are considering a career in network engineering. Basically as a network engineer you will need to do a lot of web stuff along with lower level socket code which works with TCP daemons and what not. So lets dive right into it.

Requests

Requests is probably one if not the most popular web networking library for Python. It basically allows you to interact with web services and make requests such as retrieving a page, sending and receiving responses from an API or communicating with a graphql endpoint. As you can see it’s very versatile and has a lot of possibilities on what you can do. In the example below I will show you a very simple scenario on how I can download the google.com web page and view the HTML code.

>>> import requests
>>> resp = requests.get('https://www.google.com')
>>> print(resp)
<Response [200]>
>>> print(resp.text)
<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="el"><head><meta content="text/html; charset=UTF-8" http-equiv="Content-Type"><meta content="/images/branding/googleg/1x/googleg_standard_color_128dp.png" itemprop="image"><title>Google</title><script nonce="Rv4srqZORxKiHt1dVZgQ9Q">(function(){window.google={kEI:'9WBJY9rOI7DM1sQPpLaS6AM',kEXPI:'0,1302536,56873,6058,207,2414,2390,2316,383,246,5,5367,1123753,1197695,706,380090,16114,28684,22431,1361,12316,17583,4998,13228,3847,10622,22741,2370,2711,1593,1279,2743,148,561,542,840,2196,410
..

As you can see above we retrieved the google web page code along with the HTTP code which is 200 in this case. With just 3 lines the power of doing this manually with lets say a socket code which we will talk about below would have been much harder. So make sure you learn requests as it will help you a great deal in your beginnings with Python.

BeautifulSoup

BeautifulSoup is basically a library that should not be missing from your arsenal. It can be one of the best toolkits for you to process HTML content and tokenized it into a Python object that’s easy to handle. I have written an extensive articles on this which you can find in the link below:

Socket

Sockets are basically a low level library that lets you create custom servers and clients. Those could be TCP, UDP or other forms that you defined. Since you may be tasked to do this in your network engineering career or to modify one you need to familiarize yourself with the socket library as this is the mother of all network code. Basically it lets you do all the things we mentioned earlier and also create custom clients that do more advanced things.

I want to quickly show you how powerful that is by creating a custom TCP server that listens on port 6333. To do this we use the example below.

import socket

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as mysocket:
    mysocket.bind(('127.0.0.1', 6333))
    mysocket.listen()
    conn, addr = mysocket.accept()

As demonstrated above with just a few lines we were able to bind a server that runs in localhost and accepts TCP connections. You can further receive or do more things on the connection but this goes to show you how much it simplifies your code. A similar thing if done in a language like C would be much harder and would require a lot of boilerplate code.

Conclusion

We were able to go over this Which Python Library Should You Learn First. Hopefully, I answered any questions you may have and helped you get started on your journey with Python.

Please drop me a cheer below if you found this helpful and think it may have helped you. I would appreciate it.

If you have any questions or comments, please post them below or send me a note on my Twitter. I check periodically and try to answer them in the priority they come in. Also, if you have any corrections, please let me know, and I’ll update the article with new updates or mistakes I made.

Which nische do you use Python for?

I have personally used Python for everything around the sun. The past few years I’ve done extensive work on machine learning and that’s where my focus really is. However since I’ve done a lot of work also with networking, APIs and statistics I gave you my perspective in all the fields.

If you would like to find more articles related to Python, you can check the list below:

You can find information about relevant projects referenced in this article in the list below:

Leave a Comment

Your email address will not be published. Required fields are marked *