Unlock the power of Python's libraries
Learn how to simplify image processing, creating GUIs, and more with these free tools
This post is from Grokking Python, a free newsletter available on Substack from Educative, the world’s best learning platform for software developers. It’s where we’ll share our team’s best Python-related info and resources — stuff we think could really enhance your understanding.
Hey Grokking Python readers!
We've been talking a lot in recent editions about Python's real-world applications for things like web development and machine learning. Despite being distinct fields, they have one important thing in common: they both use Python's extensive assortment of libraries and packages.
These versatile and robust libraries and packages are among our favorite things about Python, so today we're going to look at them more closely. We'll start with the Python Standard Library, which you should definitely know about, then share five use cases that external libraries and packages serve to show just how diverse these offerings are. We'll also point you to some resources for getting hands-on with these tools.
First things first
Before diving in, we should get clear about some definitions. When we talk about libraries and packages in Python, we're basically just referring to collections of modules. (Packages can also contain sub-packages.) Modules are simply files containing Python code: functions or methods, classes, and variables.
Libraries and packages save us time and help us do useful things because their code is predefined and contains reusable functions. And you don't have to look very hard to find this reusable code in action, since Python itself ships with a lot of it.
The Python Standard Library
The Python Standard Library (PSL) is a set of modules distributed with Python. You can think of it as out-of-the-box Python, and it contains an extensive amount of functionality. A few examples of the types of modules in the PSL include:
- Built-in functions
- High-level data types
- File and directory access
- Numeric and mathematical modules
- Data compression and archiving modules
Among the useful PSL modules for Python beginners are math
, heapq
, and random
:
math
: This module offers many mathematical functions like factorial and trigonometric operations.
heapq
: This module lets you create the heap data structure, which can help with programming problems that involve finding the best element in a dataset.
random
: This module allows you to generate different types of random numbers, which can come in handy in solving various coding problems.
These modules barely scratch the surface of what the PSL offers. Take a look at the PSL documentation's table of contents, and you'll see just how extensive it is.
5 use cases and 30 libraries and packages
Although the PSL is robust, external libraries and packages make Python even more powerful. You can search for these resources in the Python Package Indexer (PyPI), a repository of software for Python.
PyPI contains libraries and packages for all sorts of use cases, five of which we'll discuss now.
1. Image processing
Python libraries can help with tasks like cropping photos, creating thumbnails, and grayscaling images. Some of these include:
2. Graphical user interfaces
A graphical user interface (GUI) lets you interact with a computer using visual elements. GUIs make it easier to visualize your code and can be useful in all sorts of projects. Several Python libraries and tools make it easier to build GUIs:
Kivy's website includes a gallery of projects where it's used.
3. Web scraping
Web scraping automates the process of grabbing relevant data across multiple web pages, and Python is considered one of the best programming languages to use for it. You can use a number of tools and libraries to scrape the web:
4. Data science and machine learning
In the previous edition of Grokking Python, we covered some of the most commonly used libraries for these fields. To recap, this list included:
5. Game development
Yes, you can build games with Python, and the following resources will help you:
This is just a tiny sampling of the available external Python libraries and packages. Explore PyPI on your own, and see what you can discover.
Importing and installing libraries and packages
Before you can do anything with a Python library or package, you need to make sure you have the modules at your disposal. The process of doing so differs slightly for the PSL and external libraries, and we'll take a look at some basics next. (Code examples come from the Educative course Learn Python 3 from Scratch.)
PSL
To use the methods of a module, you must import the module into your code. You can do this with the import
keyword. Here's what that looks like when importing the datetime
module, which contains methods for working with the current date and time:
import datetime
date_today = datetime.date.today() # Current date
print(date_today)
time_today = datetime.datetime.now()
print(time_today.strftime("%H:%M:%S")) # Current time
In that code, datetime.date
and datetime.datetime
are classes in the datetime
module. Each class contains its own methods, which you access with the . operator.
To import only a particular class from a module, you can use the from
keyword:
from datetime import date
# Now we only have the methods of the date class
date_today = date.today() # Current date
print(date_today)
# These won't work
# time_today = datetime.datetime.now()
# print (time_today.strftime("%H:%M:%S"))# Current time
Finally, you can give your own names to modules you import by using the as keyword. For example, the following code renames datetime
to dt
:
import datetime as dt
date_today = dt.date.today() # Current date
print(date_today)
time_today = dt.datetime.now()
print(time_today.strftime("%H:%M:%S")) # Current time
External libraries
Importing modules from an external package works the same as with the PSL. The difference is that you must install the package first. You can find detailed instructions on installing these packages in the Python Packaging User Guide.
Projects and courses on Educative
Hopefully, today's look at libraries and packages in Python has inspired you to try some out for yourself. To help you get started, we've collected some projects and courses that will teach you about notable libraries and packages:
- Project: Visualize Stock Market Data with Python. This project uses Matplotlib and Pandas.
- Project: Develop a Chatbot. This project involves the Natural Language Toolkit (NLTK) and TensorFlow, a machine learning platform.
- Course: Simplifying Machine Learning with PyCaret. PyCaret makes machine learning a lot more approachable.
- Course: Deep Learning with PyTorch Step-by-Step. PyTorch is another popular machine learning framework.
- Course: Predictive Data Analysis With Python. This course teaches you about NumPy, Pandas, and Selenium, among other things.
- Python Tkinter Tutorial: Build a Number Guessing Game. This article on the Educative blog links to four more tutorials for learning the GUI library Tkinter.
The web is full of other tutorials and hands-on learning opportunities for Python libraries and packages. Do you have a favorite library or tutorial that you'd like to share? Let us know in the comments.
As always, happy learning!
Start a discussion
Which Python library is your favorite? Was this article helpful? Let us know in the comments below!