Self-paced

Explore our extensive collection of courses designed to help you master various subjects and skills. Whether you're a beginner or an advanced learner, there's something here for everyone.

Bootcamp

Learn live

Join us for our free workshops, webinars, and other events to learn more about our programs and get started on your journey to becoming a developer.

Upcoming live events

Learning library

For all the self-taught geeks out there, here is our content library with most of the learning materials we have produced throughout the years.

It makes sense to start learning by reading and watching videos about fundamentals and how things work.

Full-Stack Software Developer - 16w

Data Science and Machine Learning - 16 wks

Search from all Lessons


LoginGet Started
← Back to Lessons

Weekly Coding Challenge

Every week, we pick a real-life project to build your portfolio and get ready for a job. All projects are built with ChatGPT as co-pilot!

Start the Challenge

Podcast: Code Sets You Free

A tech-culture podcast where you learn to fight the enemies that blocks your way to become a successful professional in tech.

Listen the podcast
Edit on Github

What Are Third-Party Libraries Used for in Python?

Third-Party Libraries
Examples of Using Third-Party Libraries in Python
  • Requests

Third-Party Libraries

Third-party libraries are an essential tool in modern programming, and most programming languages have different libraries that simplify certain tasks for developers. A library, also known as a package or module, is a set of predefined code that has been developed by an external programmer and made available to the general public.

Below, we will see a small example with one of Python's most popular libraries, the Pandas library.

  1. Install the Pandas library with the following command.
1pip install pandas
  1. Import the Pandas library into your working module
1import pandas as pd

The as keyword is used to give the library an alias to make it easier to use. It is optional.

  1. Now you can use Pandas in your own code.
1data_frame = pd.DataFrame({ 2 "Name": ["Emma", "Liam", "Olivia"], 3 "LastName": ["Johnson", "Smith", "Williams"], 4 "Age": [34, 54, 23] 5}) 6 7print(data_frame)

output:

1 Name LastName Age 20 Emma Johnson 34 31 Liam Smith 54 42 Olivia Williams 23

In this example, we create a small DataFrame with the help of the DataFrame() function from the Pandas library. We then add information for three different users and display the user information in the console.

What Are Third-Party Libraries in Python?

Third-party libraries are collections of code developed by external programmers. These libraries or packages encapsulate specific functionality that can be reused in different projects as needed. Instead of writing code over and over again whenever you need to perform a common or complex task, you can rely on third-party libraries that have already created and tested that code, saving you time and effort. To download a Python library, you can use the package management system called pip (Pip Install Packages).

  • To install any library, use the following syntax:
1pip install library_name
  • Then, you can create a Python module or working file, e.g., example.py, and import the library using the following syntax:
1import library_name as alias

The alias is optional and it works for giving the library a shorter name for easier use.

Once imported, you can use this library in your code and take advantage of all its benefits. Remember that you can install and import as many libraries as you need. For example, you can use the NumPy library along with the Pandas library for advanced mathematical calculations.

Examples of Using Third-Party Libraries in Python

Below, we'll see some examples of how to install, import, and use different third-party libraries in your Python code.

Requests

The requests library allows you to make HTTP requests in Python. This library abstracts the complexity of making HTTP calls with the help of intuitive and easy-to-understand functions, allowing you to focus on getting the most out of the API you want to use.

To use the Requests library on your computer, follow these steps:

  1. Install the requests library on your computer with the following command:
1pip install requests
  1. Once installed, import the library into your working module with the following syntax:
1import requests
  1. Now you can start using the wonderful features of this library.
1response = requests.get("https://jsonplaceholder.typicode.com/posts/1") 2 3if response.status_code == 200: 4 data = response.json() 5 print("Data obtained from the API\n") 6 print(data) 7 8else: 9 print("There was a problem, and the API responded with an error")

output:

1Data obtained from the API 2 3{ 4 'userId': 1, 5 'id': 1, 6 'title': 'sunt aut facere repellat provident occaecati excepturi optio reprehenderit', 7 'body': 'quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto' 8}

In this example, we use the get() method from the Requests library to retrieve information from a server. If the request is successful, we print the result to the console, but if the request fails, we display an error in the console. For this example, we use the jsonplaceholder API, which allows you to make free HTTP requests to test your application.

Numpy

The NumPy library is one of the most important packages to understand when starting to learn Python. This library allows developers to quickly perform a wide variety of numerical calculations. To install NumPy on your computer, follow these instructions:

  1. Install NumPy on your computer.
1pip install numpy
  1. Import NumPy into your working module.
1import numpy as np
  1. Start using the functionalities of NumPy in your code.
1array_numbers = np.arange(10) 2print(array_numbers) # output: [0 1 2 3 4 5 6 7 8 9] 3 4a = np.array([4, 16, 25]) 5b = np.sqrt(a) # Square root element-wise ([2. 4. 5.])

The NumPy library offers a wide variety of functions that allow you to perform advanced mathematical calculations, create arrays, matrix, and much more. Here are some of the most relevant functions:

FunctionDescription
np.array()Creates a NumPy array from a list or another iterable object.
np.zeros()Creates an array of zeros with specific dimensions.
np.linspace()Creates an array with a sequence of evenly spaced numbers in a specified range and a number of elements.
np.random.rand()Creates an array with random values in the range [0, 1] with specified dimensions.

Pandas

Pandas is one of the most important libraries in Python, especially in fields like data science or machine learning (Machine Learning). Pandas offers a wide variety of functions for data manipulation and analysis. To download Pandas on your computer, follow these instructions:

  1. Install Pandas on your computer.
1pip install pandas
  1. Import Pandas into your working module.
1import pandas as pd
  1. Start enjoying the features of Pandas.
1serie = pd.Series([1, 4, 5, 7, 9]) 2 3data_frame = pd.DataFrame({ 4 "Name": ["MacOS", "Windows", "Linux"], 5 "Color": ["Gray", "Black", "Red"] 6}) 7 8print(serie) 9print(data_frame)

output:

1// Output series 20 1 31 4 42 5 53 7 64 9 7dtype: int64 8 9// Dataframe output 10 Name Color 110 MacOS Gray 121 Windows Black 132 Linux Red

If you want to start your career in data science or artificial intelligence, Pandas will be your ally for data cleaning and analysis. This library allows you to read .csv files, Excel files, JSON files, and more

Matplotlib

Example of data visualization in matplotlib

Matplotlib is a Python library used to create high-quality visualizations and graphs. It provides an interface to produce a variety of charts, from simple to complex, that are essential for effective data communication.

To use Matplotlib in your own projects, follow these steps:

  1. Install Matplotlib on your computer.
1pip install matplotlib
  1. Import Matplotlib into your working module.
1import matplotlib.pyplot as plt
  1. You can now start using all the features of Matplotlib.
1# Sample data: years and sales 2years = [2015, 2016, 2017, 2018, 2019, 2020] 3sales = [50000, 60000, 75000, 90000, 110000, 130000] 4 5# Create a line chart 6plt.plot(years, sales, marker='o') 7 8# Add labels and title 9plt.xlabel('Years') 10plt.ylabel('Sales') 11plt.title('Sales Growth Over the Years') 12 13# Show the chart 14plt.grid(True) 15plt.show()

In this example, we create a mock visualization of the number of sales over the years of a company. Using the plot() method, we create the presentation table. Then, with the label and title methods, we create labels and the title of our table. Finally, we use the grid() function to display the grid of our visualization.

Conclusion

Third-party libraries are an essential tool for any programmer. They provide an efficient way to leverage tested and reliable functionality, speeding up development and improving project quality. Third-party libraries are especially useful in fields like data science or machine learning because they offer a wide range of features that will help you save time and effort when creating your projects.