Libraries ¶
In Python, a library serves as a structured repository of functionality encapsulated within a collection of files known as modules. These modules act as containers for various elements, predominantly functions but also encompassing other components such as data values, constants, and potentially more. The primary purpose of a Python library is to provide a modular and reusable set of tools that can be easily integrated into other programs. This modular design facilitates code reuse and helps developers avoid the redundancy of reinventing solutions to common problems, contributing to the efficiency and maintainability of their codebase.
The essence of a library lies in its potential for widespread applicability. By creating reusable modules, developers can encapsulate specific functionalities or algorithms, making them readily available for integration into diverse projects. This promotes a more streamlined development process, as programmers can leverage the existing functionality of a library rather than starting from scratch for each new project.
Although libraries are conceived with the idea of containing related functionalities, Python does not enforce strict rules on their internal organization. This flexibility allows developers creative freedom in structuring their libraries. However, this lack of enforcement comes with the responsibility of logical organization. Developers are expected to organize functions and data in a coherent and comprehensible manner within a library, ensuring that users can easily navigate and comprehend its contents. This logical organization not only benefits the library's creator but also enhances the usability of the library for others who may want to leverage its functionality. Overall, the freedom and responsibility associated with Python libraries contribute to a dynamic and collaborative programming environment.
Standard library ¶
Python comes with a comprehensive standard library. This library includes a wide range of modules covering diverse functionalities, from basic operations to advanced features. It provides a solid foundation for building various types of applications without requiring additional installations.
General imports ¶
Before you can leverage the tools and features offered by a library, it's crucial to import the relevant module into your program's memory. This ensures that the program recognizes and can access the functions, data, and other elements contained within the library.
The
import
keyword plays a pivotal role in incorporating external functionality into your Python program.
To use a library module, you employ the import keyword followed by the name of the module.
This action essentially loads the module into the program's memory, making its contents accessible for use.
import math
Accessing ¶
Once a library module is imported, you can refer to its elements using a specific syntax:
module_name.element_name
.
Here,
module_name
represents the name of the imported module, and
element_name
denotes the specific function, data, or other components you wish to utilize from within that module.
Python employs the dot notation (
.
) to signify the relationship between the module and its elements.
When you see
module_name.thing_name
, the dot (
.
) serves as an indicator of belonging or being "part of."
It's a visual cue that the referenced element is a component or functionality encapsulated within the specified module.
You can also think of this as specifying a file in a folder.
If you have a folder called
fridge
with a file called
tomato
, then you could specify this in dot notation as
fridge.tomato
.
Except in Python, we are specifying a path to a piece of code we want to use.
print("π is", math.pi)
print("cos(π) is", math.cos(math.pi))
π is 3.141592653589793 cos(π) is -1.0
If we try to use
pi
or
cos
without the
math.
prefix we will get a
NameError
.
print("π is", pi)
print("cos(π) is", cos(math.pi))
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[3], line 1 ----> 1 print("π is", pi) 2 print("cos(π) is", cos(math.pi)) NameError: name 'pi' is not defined
They are not in our namespace yet, but
math
is.
That is why
math.pi
and
math.cos
work.
We can fix this with a specific import.
Specific imports ¶
In Python programming, efficiency and code clarity are key considerations. One effective way to achieve these goals is by selectively importing only the specific items you need from a library module. This not only helps in keeping your programs concise but also enhances readability.
The
from ... import ...
syntax allows you to import specific items directly from a library module.
This targeted approach ensures that only the necessary elements are brought into your program's scope.
Once you've used the
from ... import ...
statement, you can directly reference the imported items in your code without prefixing them with the library name.
This can significantly reduce the verbosity of your code and make it more straightforward.
from math import pi, cos
print("π is", pi)
print("cos(π) is", cos(math.pi))
π is 3.141592653589793 cos(π) is -1.0
Aliases ¶
To further streamline your code and reduce verbosity, Python provides a mechanism to create aliases for library modules.
This involves using the
import ... as ...
syntax, allowing you to give a library a short and customized alias during the import process.
This alias can then be used as a shorthand reference to the library's functionalities within your program.
import math as m
print(m.sqrt(25))
5.0
Choose meaningful and intuitive alias names that reflect the purpose of the library to enhance code understanding.
Be mindful of potential naming conflicts; avoid overly generic alias names that may clash with common variable names.
External libraries ¶
In addition to the standard library, Python users can access a vast array of additional libraries hosted on PyPI. These libraries are contributed by the Python community and cover specialized domains, allowing developers to extend Python's capabilities for specific tasks.
PyPI libraries cater to a broad spectrum of needs, including web development, data science, machine learning, networking, and more. This extensibility is a key strength of the Python ecosystem.
We will be heavily using the following libraries in this course: