KAHIBARO Bookshelves
Discord Login Register

12.1. 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:

  • Python itself is the workshop (the language and runtime).
  • Your program is the project you're building.
  • Libraries are toolboxes full of useful tools (functions, classes, constants).

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:

  • Working with dates and times
  • Doing math beyond basic +, -, *, /
  • Generating random numbers
  • Reading and writing different file formats
  • Talking to websites or APIs

Writing these features correctly and safely is hard. Libraries:

  • Save time: you don't write everything yourself.
  • Reduce bugs: widely used libraries are tested by many people.
  • Make code clearer: instead of a long custom solution, you call a well-named function from a library.

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:

  • math is the library (a module).
  • sqrt is a function inside that library.
  • math.sqrt(25) means: "call the sqrt function from the math library with argument 25."

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:

  • Functions – reusable pieces of code that do something specific.
    • Example: math.sqrt(x) returns the square root of $x$.
  • Classes – blueprints for creating objects with data and behavior.
  • Constants and variables – useful predefined values.
    • Example: math.pi for $\pi$.
  • Submodules – smaller modules grouped inside a larger package.

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:

  • The task is common (dates, random numbers, math, file formats).
  • The task is complex or low-level (network communication, encryption).
  • Someone has already provided a well-tested solution.
  • You want to focus on your problem, not the technical details.

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

  • Try to write your own random number generator (hard, error-prone), or
  • Use the built-in random library:
import random
number = random.randint(1, 10)
print(number)

The second option is exactly why libraries are so valuable.

Advantages and trade-offs

Advantages:

  • Faster development
  • Less code to write and maintain
  • More reliable features
  • Access to advanced capabilities (data science, web, etc.)

Trade-offs:

  • You need to learn the library’s interface (its functions and how to call them).
  • For external libraries, you depend on:
    • Them being installed
    • Their updates and changes over time

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:

  • See how to import modules and access what’s inside them.
  • Explore some important standard libraries:
    • math, random, and datetime.
  • Learn how to install external libraries with pip.

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: 0

Comments

Please login to add a comment.

Don't have an account? Register now!

Where to Move

Move chapter:

  • ☰ 1. Chapter 1: What Is Programming?
    • ☰ 1.1. What is a computer program?
      • ☰ 1.2. What is Python?
        • ☰ 1.3. Where is Python used?
          • ☰ 1.4. How programming helps solve problems
            • ☰ 1.5. Your first Python program
            • ☰ 2. Chapter 2: Setting Up Python
              • ☰ 2.1. Installing Python (Windows / macOS / Linux)
                • ☰ 2.2. Using Python in the terminal
                  • ☰ 2.3. Using an IDE (IDLE / VS Code)
                    • ☰ 2.4. Running Python scripts
                      • ☰ 2.5. Python interactive mode
                      • ☰ 3. Chapter 3: Variables and Data Types
                        • ☰ 3.1. What is a variable?
                          • ☰ 3.2. Creating variables
                            • ☰ 3.3. Numbers
                              • ☰ 3.4. Test
                                • ☰ 3.5. Boolean values
                                  • ☰ 3.6. Type checking
                                  • ☰ 4. Chapter 4: Basic Operations
                                    • ☰ 4.1. Arithmetic operations
                                      • ☰ 4.2. Working with numbers
                                        • ☰ 4.3. String operations
                                          • ☰ 4.4. Combining text and variables
                                            • ☰ 4.5. Comments in Python
                                            • ☰ 5. Chapter 5: User Input and Output
                                              • ☰ 5.1. Getting input from the user
                                                • ☰ 5.2. Converting input to numbers
                                                  • ☰ 5.3. Formatting output
                                                    • ☰ 5.4. Writing interactive programs
                                                    • ☰ 6. Chapter 6: Conditions
                                                      • ☰ 6.1. Comparison operators
                                                        • ☰ 6.2. Logical operators
                                                          • ☰ 6.3. if, elif, else
                                                            • ☰ 6.4. Nested conditions
                                                              • ☰ 6.5. Common beginner mistakes
                                                              • ☰ 7. Chapter 7: Loops
                                                                • ☰ 7.1. Why loops are useful
                                                                  • ☰ 7.2. for loops
                                                                    • ☰ 7.3. while loops
                                                                      • ☰ 7.4. Loop counters
                                                                        • ☰ 7.5. break and continue
                                                                          • ☰ 7.6. Loop exercises
                                                                          • ☰ 8. Chapter 8: Data Collections
                                                                            • ☰ 8.1. Lists
                                                                              • ☰ 8.2. Tuples
                                                                                • ☰ 8.3. Dictionaries
                                                                                  • ☰ 8.4. Sets (basics)
                                                                                    • ☰ 8.5. Choosing the right data structure
                                                                                    • ☰ 9. Chapter 9: Functions
                                                                                      • ☰ 9.1. Why functions matter
                                                                                        • ☰ 9.2. Defining functions
                                                                                          • ☰ 9.3. Parameters and arguments
                                                                                            • ☰ 9.4. Return values
                                                                                              • ☰ 9.5. Scope (local vs global)
                                                                                                • ☰ 9.6. Reusing code
                                                                                                • ☰ 10. Chapter 10: Working with Files
                                                                                                  • ☰ 10.1. What is a file?
                                                                                                    • ☰ 10.2. Reading text files
                                                                                                      • ☰ 10.3. Writing and appending files
                                                                                                        • ☰ 10.4. File paths
                                                                                                          • ☰ 10.5. Simple file-based programs
                                                                                                          • ☰ 11. Chapter 11: Errors and Debugging
                                                                                                            • ☰ 11.1. Types of errors
                                                                                                              • ☰ 11.2. Reading error messages
                                                                                                                • ☰ 11.3. Common beginner errors
                                                                                                                  • ☰ 11.4. try and except
                                                                                                                    • ☰ 11.5. Debugging strategies
                                                                                                                    • ☰ 12. Chapter 12: Using Python Libraries
                                                                                                                      • ☰ 12.1. What is a library?
                                                                                                                        • ☰ 12.2. Importing modules
                                                                                                                          • ☰ 12.3. Standard libraries
                                                                                                                            • ☰ 12.3.1. math
                                                                                                                              • ☰ 12.3.2. random
                                                                                                                                • ☰ 12.3.3. datetime
                                                                                                                                • ☰ 12.4. Installing external libraries with pip
                                                                                                                                • ☰ 13. Chapter 13: Object-Oriented Programming
                                                                                                                                  • ☰ 13.1. What is object-oriented programming?
                                                                                                                                    • ☰ 13.2. Classes and objects
                                                                                                                                      • ☰ 13.3. The `__init__` method
                                                                                                                                        • ☰ 13.4. Attributes and methods
                                                                                                                                          • ☰ 13.5. Encapsulation
                                                                                                                                            • ☰ 13.6. Inheritance (basics)
                                                                                                                                              • ☰ 13.7. Simple OOP examples
                                                                                                                                              • ☰ 14. Chapter 14: Automation with Python
                                                                                                                                                • ☰ 14.1. What is automation?
                                                                                                                                                  • ☰ 14.2. Automating simple tasks
                                                                                                                                                    • ☰ 14.3. Working with files and folders
                                                                                                                                                      • ☰ 14.4. Automating text processing
                                                                                                                                                        • ☰ 14.5. Scheduling scripts
                                                                                                                                                          • ☰ 14.6. Practical automation examples
                                                                                                                                                          • ☰ 15. Chapter 15: Web Development with Python
                                                                                                                                                            • ☰ 15.1. How the web works (basic overview)
                                                                                                                                                              • ☰ 15.2. Introduction to Flask
                                                                                                                                                                • ☰ 15.3. Creating a simple web server
                                                                                                                                                                  • ☰ 15.4. Routing and templates
                                                                                                                                                                    • ☰ 15.5. Handling user input (forms)
                                                                                                                                                                      • ☰ 15.6. Very basic APIs
                                                                                                                                                                      • ☰ 16. Chapter 16: Data Science with Python
                                                                                                                                                                        • ☰ 16.1. What is data science?
                                                                                                                                                                          • ☰ 16.2. Working with data
                                                                                                                                                                            • ☰ 16.3. Introduction to NumPy
                                                                                                                                                                              • ☰ 16.4. Introduction to pandas
                                                                                                                                                                                • ☰ 16.5. Data visualization with matplotlib
                                                                                                                                                                                  • ☰ 16.6. Simple data analysis examples
                                                                                                                                                                                  • ☰ 17. Chapter 17: Mini Projects
                                                                                                                                                                                    • ☰ 17.1. Number guessing game
                                                                                                                                                                                      • ☰ 17.2. Simple calculator
                                                                                                                                                                                        • ☰ 17.3. To-do list
                                                                                                                                                                                          • ☰ 17.4. Password generator
                                                                                                                                                                                            • ☰ 17.5. Quiz application
                                                                                                                                                                                              • ☰ 17.6. Small web or data project
                                                                                                                                                                                              • ☰ 18. Chapter 18: Next Steps
                                                                                                                                                                                                • ☰ 18.1. How to practice Python
                                                                                                                                                                                                  • ☰ 18.2. Writing clean and readable code
                                                                                                                                                                                                    • ☰ 18.3. Choosing a specialization
                                                                                                                                                                                                      • ☰ 18.3.1. Web development
                                                                                                                                                                                                        • ☰ 18.3.2. Data science
                                                                                                                                                                                                          • ☰ 18.3.3. Automation
                                                                                                                                                                                                            • ☰ 18.3.4. Software development
                                                                                                                                                                                                            • ☰ 18.4. Learning resources and communities
                                                                                                                                                                                                            • ☰ 19. Appendix
                                                                                                                                                                                                              • ☰ 19.1. Python syntax cheat sheet
                                                                                                                                                                                                                • ☰ 19.2. Common errors and solutions
                                                                                                                                                                                                                  • ☰ 19.3. Glossary of terms