20. Python Modules and Libraries

Quiz: 0/4

A module in Python is a file containing Python definitions and statements. Modules help you organize your code into multiple files, and they allow you to reuse your code across different projects. Python has many built-in modules, and you can also use external modules that others have written.

How to use modules in Python?

To use a module, we need to import it into our program. You can import a module using the import statement. Here's a simple example:

import math

After importing the math module, you can use its functions. For example, let's find the square root of a number:

import math
result = math.sqrt(16)
print(result)

This will output 4.0 because the square root of 16 is 4.

Python's Built-In modules

Python comes with several built-in modules that provide useful functionality. Here are some examples:

math module:

Provides mathematical functions like sqrt() (square root), pi (the mathematical constant), and more.

import math
print(math.pi)  # Output: 3.141592653589793
print(math.factorial(5))  # Output: 120

random module:

Used for generating random numbers.

import random
print(random.randint(1, 100))  # Random integer between 1 and 100

datetime module:

Helps you work with dates and times.

import datetime
now = datetime.datetime.now()
print(now)  # Current date and time

How to install third-party libraries?

In addition to Python's built-in modules, there are many third-party libraries you can install. One way to install a library is by using the pip tool, which stands for "Pip Installs Packages." Let's look at how to install and use a third-party library.

Example: Installing the requests library (used for making HTTP requests).

  1. Open your terminal or command prompt and run:
pip install requests
  1. Once installed, you can use it in your Python code:
import requests

response = requests.get("https://www.example.com")
print(response.status_code)  # Output: 200 (if the website is available)

Summary

  • Modules help you extend the functionality of Python by importing built-in or third-party code.
  • You can use built-in modules like math, random, and datetime to perform common tasks.
  • To use third-party libraries, you can install them with pip and then import them into your code.

Learning to use modules and libraries is one of the most important skills to improve your coding efficiency in Python. So, keep exploring and installing more libraries to make your programs even better!

Next steps

  • Try exploring more Python modules such as os (for working with files and directories), sys (for system-specific parameters), or json (for working with JSON data).
  • Install a new library using pip and use it in your code.

Hands-On Practise

Exercise: Using Python modules and libraries

Task:

In this exercise, you will work with Python's built-in modules and also learn how to install and use a third-party library.

Part 1: Using Built-in Modules

  1. math module:

    • Import the math module.
    • Calculate the value of pi (math.pi) and the square root of a number of your choice (e.g., 64) using math.sqrt().
    • Print both results.
  2. random module:

    • Import the random module.
    • Generate a random floating-point number between 0 and 1 and print it.
    • Generate a random integer between 10 and 50 and print it.
  3. datetime module:

    • Import the datetime module.
    • Print the current date and time using datetime.datetime.now().

Part 2: Using Third-Party Libraries

  1. Install the requests library (if you haven’t already):

    • Open your terminal or command prompt and run:
      pip install requests
      
  2. Using the requests library:

Hints:

  • Use the help() function to explore available functions in any module (e.g., help(math)).
  • To handle JSON responses with requests, you can use .json() method on the response object.

Bonus:

  • Try installing and using another third-party library, such as pandas or beautifulsoup4, and perform basic operations with them.
Output:

Quizzes: 0/4

Question 1:

What does the 'math' module in Python allow you to do?

  • Perform mathematical operations like square root and trigonometry.
  • Send HTTP requests to websites.
  • Manipulate date and time.
  • Generate random numbers.

Question 2:

What is the purpose of the 'random' module in Python?

  • Generate random numbers and selections.
  • Perform mathematical calculations.
  • Handle date and time operations.
  • Manage file input and output.

Question 3:

What can you do with the 'datetime' module in Python?

  • Perform string manipulations.
  • Generate random numbers.
  • Work with dates and times.
  • Manage HTTP requests.

Question 4:

Which Python library is used to send HTTP requests and retrieve data from websites?

  • math
  • random
  • requests
  • datetime

Feedback

Share your thoughts about this lesson

Discussion

Please log in to create a discussion.
Previous lessonError and exception handlingNext lessonFunctions in Python