Kahibaro
Discord Login Register

What is a library?

In Python, a library (often called a module or package) is a collection of ready-made code that you can reuse in your own programs. Instead of writing everything from scratch, you can "import" this existing code and use it directly.

Think of a library as a toolbox:

When you use a library, you are saying: "Someone has already solved part of this problem. I will use their solution instead of reinventing it."

Why libraries exist

Programming problems repeat. For example:

Writing these features correctly and safely is hard. Libraries:

How libraries fit into your code

You use libraries by importing them. Conceptually, this is:

  1. Load the library into your program.
  2. Use the functions, classes, or variables it provides.

In code, it looks like:

import math
result = math.sqrt(25)  # uses the sqrt function from the math library
print(result)

Here:

You will learn the import statement and its variations in a later section; here, just understand that importing connects your program to a library.

Types of Python libraries

At a high level, there are two main kinds of libraries you will use:

  1. Standard library
    • Comes built-in with Python.
    • You do not need to install anything extra.
    • Always available once Python is installed.
    • Examples (which you’ll explore later):
      • math for mathematical functions
      • random for random numbers
      • datetime for working with dates and times
  2. External (third-party) libraries
    • Created by other people or organizations.
    • You do need to install them (usually with pip).
    • Add specialized features:
      • Web development
      • Data analysis
      • Automation
      • Machine learning, and more

You will learn how to install these with pip and how to choose good ones in later sections.

What is inside a library?

A typical Python library can contain:

You access these pieces using dot notation:

import math
print(math.pi)       # a constant
print(math.sqrt(9))  # a function

The pattern is:

$$
\text{library\_name}.\text{thing\_inside}
$$

Libraries vs your own code

You can think of code in three layers:

  1. Python language
    • The core rules: variables, loops, functions, etc.
  2. Libraries
    • Code built on top of the language that gives extra abilities.
  3. Your program
    • Uses the language + libraries to solve your specific problem.

Over time, you can even turn your own code into a small library that you reuse across multiple projects. That’s just code organized so others (or future you) can import and use it easily.

When should you use a library?

You usually reach for a library when:

For example, if you need random numbers, you could:

import random
number = random.randint(1, 10)
print(number)

The second option is exactly why libraries are so valuable.

Advantages and trade-offs

Advantages:

Trade-offs:

For beginners, using the Python standard library is a gentle introduction, since it’s always available and well-documented.

How you will use libraries in this course

In this chapter and the next ones, you will:

For now, the key idea to remember:

A library in Python is pre-written, reusable code that you import into your program so you can use its tools (functions, classes, constants) instead of writing everything from scratch.

Views: 16

Comments

Please login to add a comment.

Don't have an account? Register now!