Subscribe Contact

Home  »  Chapter 7 : Libraries
Library Concepts

Overview

In programming, a library is a collection of prewritten, reusable, code that programmers can use to optmize tasks. These collections usually target specific categories of tasks, like mathematics, text analysis, mapping, etc. Programmers use libraries for a number of reasons, such as:

  • Efficiency: Libraries save time. They contain pre-written code that you can incorporate into your projects, allowing you to focus on the unique aspects of your application.
  • Simplicity: By using library functions, complex tasks can be accomplished with simple lines of code, making your code more readable and maintainable.
  • Reliability: Libraries are often written and tested by experienced developers. This means that the functions they provide are less likely to contain bugs than code you might write from scratch.
  • Community and Support: Popular libraries come with a community of developers. This community can be a valuable resource for learning and troubleshooting.

Libraries in Python

Python, like many programming languages, has a rich ecosystem of libraries available for use. There are libraries that are part of the Python language installation, called the Python Standard Library, and there are thousands of External Libraries available from various sources. The primary repository of external libraries is located online at pypi.org.

To use a library in Python, you use the import statement in your program along with the name of the library of interest. For example, one of the libraries in the Standard Python Library is called math which includes over 50 mathematical functions you can use in your programs. Here's a code example of using the math library:

import math
x = 9
y = 2
print(math.sqrt(x))
print(math.pow(x, y))
print(math.pow(x, (math.sqrt(x))))
And the output:
3.0
81.0
729.0
Additional details about the Standard Python Library and External Libraries are provided on the next couple of pages.


Page Menu: 


 


«  Previous : Libraries
Next : Libraries : Standard Library  »



© 2023 John Gordon
Cascade Street Publishing, LLC