Import Python Libraries (2024)

Import Python Libraries (1)

What Are Libraries?

A library is a collection of functions that can be added to your Python code and called as necessary, just like any other function. There is no reason to rewrite code that will perform a standard task. With libraries, you can importpre-existing functions and efficiently expand the functionality of your code.

Some examples of popular libraries and their functionalities:

  • Requests: an elegant and simple HTTP library for Python, commonly used for REST API calls. (See my other course, Build Your Web Projects With REST APIs, to learn more! 😉 )

  • Beautiful Soup: a library for pulling data out of HTML and XML files.

  • Pandas: a fast, powerful, and easy-to-use open-source data analysis and manipulation tool.

There are thousands of Python libraries that you can use in your Python code!

Install Libraries Using Pip

Pip is a Python package manager.

What’s a package manager?

A package manager is a tool that allows you to install and manage additional libraries through your terminal. Pip comes ready to go with a Python installation, so you’re already good to go!

To install a package using pip in your terminal, we use the following pattern:

pip install <name-of-package>

To view the packages already installed, you can type the following:

pip freeze

That will print a list of all the existing libraries, or dependencies, to your terminal.

Check out the screencast below to learn more about installing and viewing your Python packages using pip!

Import Libraries in Your Code

To import library functionality into your code, use the keywordimport. (Shocker, right? 😄 )

Using the import keyword at the top of your code file, you can import certain library functions or the entire library at once. For example, to import the entirerequestslibrary, you must first installrequestsusing your terminal with pip install requests. Then you can write the following code in your text editor:

import requests

Now to call theget()function in the requests library, write:

requests.get()

If you want to import just one function named get from the requests library, write the following:

from requests import get

Nowyou don’t need to type requests before get() because the method itself has already been imported.

Level Up:Import Python Libraries

Import Python Libraries (2)

Context:

In this exercise, you will explore the concept of importing modules to enhance the calculator you previously developed. The goal is to group the calculation functionalities into a separate function, which will allow for better code organization and more efficient reuse of calculation functionalities in other parts of the program. You will learn how to import this calculation function into the main program, enabling you to use the calculation functionalities in a more modular and efficient manner.

Instructions:

  • In the module named operations.py , add the following functions:

    • addition(a, b): which returns the sum of two numbers a and b;

    • multiplication(a, b): which returns the product of two numbers a and b.

  • In the main file, import the operations.pymodule and use its functions to perform the following mathematical operations:

    • Addition of 10 and 5;

    • Multiplication of 8 and 4.

  • Display the results of the operations in the console.

Once you have completed the exercise, you can run the following command in the VS code terminal pytest tests.py.

Let’s Recap!

  • A library is a collection of existing functions that can be used in your code.

  • Pip is a Python package managerthat lets you install and manage your external libraries.

  • The import keyword lets you import entire libraries or specific library functions into your code.

Nice, you’ve learned how to import libraries using pip! Next, you’ll learn how to use these libraries to extract web data. Exciting stuff, right?

Import Python Libraries (2024)
Top Articles
How to Log Into Facebook If You Lost Access to Code Generator
Mobile App
Funny Roblox Id Codes 2023
AMC Theatre - Rent A Private Theatre (Up to 20 Guests) From $99+ (Select Theaters)
Kokichi's Day At The Zoo
Workday Latech Edu
Fully Enclosed IP20 Interface Modules To Ensure Safety In Industrial Environment
Crossed Eyes (Strabismus): Symptoms, Causes, and Diagnosis
Costco The Dalles Or
Craigslist In South Carolina - Craigslist Near You
Chuckwagon racing 101: why it's OK to ask what a wheeler is | CBC News
What Was D-Day Weegy
1TamilMV.prof: Exploring the latest in Tamil entertainment - Ninewall
Nieuwe en jong gebruikte campers
Catsweb Tx State
Craigslist Alabama Montgomery
Summer Rae Boyfriend Love Island – Just Speak News
Daily Voice Tarrytown
Plan Z - Nazi Shipbuilding Plans
Unterwegs im autonomen Freightliner Cascadia: Finger weg, jetzt fahre ich!
Nordstrom Rack Glendale Photos
Blue Rain Lubbock
Pecos Valley Sunland Park Menu
67-72 Chevy Truck Parts Craigslist
Www Craigslist Madison Wi
The best brunch spots in Berlin
Skycurve Replacement Mat
Creed 3 Showtimes Near Island 16 Cinema De Lux
Cvs Sport Physicals
Uno Fall 2023 Calendar
Funky Town Gore Cartel Video
Craigslist Maryland Baltimore
Baddies Only .Tv
Babbychula
Diana Lolalytics
Prima Healthcare Columbiana Ohio
Helloid Worthington Login
Whitehall Preparatory And Fitness Academy Calendar
Oxford House Peoria Il
Craigslist Tulsa Ok Farm And Garden
2007 Peterbilt 387 Fuse Box Diagram
Citibank Branch Locations In Orlando Florida
Hazel Moore Boobpedia
Hk Jockey Club Result
Suppress Spell Damage Poe
Wrentham Outlets Hours Sunday
Nkey rollover - Hitta bästa priset på Prisjakt
Thrift Stores In Burlingame Ca
Bob Wright Yukon Accident
Ark Silica Pearls Gfi
Latest Posts
Article information

Author: Kerri Lueilwitz

Last Updated:

Views: 6547

Rating: 4.7 / 5 (67 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Kerri Lueilwitz

Birthday: 1992-10-31

Address: Suite 878 3699 Chantelle Roads, Colebury, NC 68599

Phone: +6111989609516

Job: Chief Farming Manager

Hobby: Mycology, Stone skipping, Dowsing, Whittling, Taxidermy, Sand art, Roller skating

Introduction: My name is Kerri Lueilwitz, I am a courageous, gentle, quaint, thankful, outstanding, brave, vast person who loves writing and wants to share my knowledge and understanding with you.