[Go to site: main page, start]

0% found this document useful (1 vote)
58 views8 pages

Class 12 Python Libraries Overview

The document discusses Python libraries, modules, packages, and frameworks. It defines each term and provides examples of creating and importing modules and packages. Modules are Python files that can contain functions and classes. Packages are namespaces that contain multiple modules or subpackages, and are represented as directories with an __init__.py file. Libraries are collections of packages, while frameworks are collections of libraries that provide additional components for application development.

Uploaded by

Sahil Ahmad
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
58 views8 pages

Class 12 Python Libraries Overview

The document discusses Python libraries, modules, packages, and frameworks. It defines each term and provides examples of creating and importing modules and packages. Modules are Python files that can contain functions and classes. Packages are namespaces that contain multiple modules or subpackages, and are represented as directories with an __init__.py file. Libraries are collections of packages, while frameworks are collections of libraries that provide additional components for application development.

Uploaded by

Sahil Ahmad
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

New

syllabus
2020-21

Chapter 4
Using
Python
Libraries

Computer Science
Class XII ( As per CBSE Board)
Visit : [Link] for regular updates
Modularization of
python program
Frame-
work

Libr- Libr-
ary1 ary2

Pack- Pack- Pack- Pack-


age1 age2 age3 age4

Framework=multiple library
mod-
ule1
mod-
ule2
Library=multiple packages
Package=multiple module
Module=multiple function/class
Visit : [Link] for regular updates
Using
Python Libraries
Following terms must be clear while developing any python
project/program.
1. Module
2. Package
3. Library
4. Framework
1. Using Module -It is a file which contains python functions/global
variables/clases etc. It is just .py file which has python executable code /
[Link] example: Let’s create a file [Link]
def hello_message(user_name):
return “Hello " + name
Now we can import [Link] module either in python interpreter or
other py file.
import usermodule
print usermodule.hello_message(“India")
Visit : [Link] for regular updates
Using
Python Libraries
How to import modules in Python?
Python module can be accessed in any of following way.
1. Python import statement
import math
print(“2 to the power 3 is ", [Link](2,3))
Just similar to math ,user defined module can be accessed using import
statement
2. Import with renaming
import math as mt
print(“2 to the power 3 is ", [Link](2,3))
3. Python from...import statement
from math import pow
print(“2 to the power 3 is ", pow(2,3))
4. Import all names
from math import *
print(“2 to the power 3 is ", pow(2,3))
Visit : [Link] for regular updates
Using
Python Libraries
2. Using Package - It is namespace that contains multiple package or modules. It is a
directory which contains a special file __ init __.py
Let’s create a directory geometry. Now this package contains multiple packages / modules
to handle user related requests.
geometry/ # top level package
__ init __.py

rectangle/ # first subpackage


__ init __.py
area_rect.py
perimeter_rect.py
circle/ # second subpackage
__ init __.py
area_circ.py
perimeter_circ.py
Now we can import it in following way in other .py file
from [Link] import area_rect
from [Link] import perimeter_circ
Visit : [Link] for regular updates
Using
Python Libraries
3. Using Library
It is a collection of various packages. Conceptually,There is no difference between
package and python [Link] Python, a library is used loosely to describe a
collection of the core modules.
‘standard library‘ of Python language comes bundled with the core Python
distribution are collection of exact syntax, token and semantics of the Python
language . The python standard library lists down approx more than 200 such core
modules that form the core of Python.
“Additional libraries” refer to those optional components that are commonly
included in Python distributions.
The Python installers automatically adds the standard library and some additional
libraries.
The additional library is generally provided as a collection of packages. To use such
additional library we have to use packaging tools like easyinstall or pip to install such
additional libraries.

Visit : [Link] for regular updates


Using
Python Libraries
3. Create library – create following directory and files structure to learn library creation & use
C:/mylib/Library1(dir)----------|
| |package1(dir.)------|[Link] functions1/classs1
| | |[Link] functions2/classs2
| |
| |package2(dir.)------|module3.p functions3/classs3
|__init__.py(blank file) |[Link] functions4/classs4
|[Link]
def moduletest2():
print("from module2")
from library1.package1 import module2
print(module2.moduletest2())
Define a function moduletest2() in [Link] file and call this
function in [Link] file as a part of library1 [Link] run [Link] file
It will call moduletest2() method and display-’from module2’ message.
Please make sure that a blank file with __init__.py is created.
Set as environment variable of library1 to call any where(path) on the system
Visit : [Link] for regular updates
Using
Python Libraries
4. Using Framework
Framework is like a collection of various libraries which architects
some more component.
For e.g. Django which has various in-built libraries like Auth, user,
database connector etc.

Visit : [Link] for regular updates

Common questions

Powered by AI

Packages and libraries contribute to the scalability of a Python project by allowing the organization of code into manageable modules and reusable components. They encapsulate functionality and provide shared interfaces, promoting reuse and reducing duplication of logic. This modular approach enables teams to expand functionality without altering the core components, facilitating easier addition of features and maintenance as the project grows .

To structure a Python project using a standard library, a custom package, and a framework, first install and set up the Python environment with the necessary framework (e.g., Django). Create a 'project' directory with subdirectories for 'custom_package', including '__init__.py'. Use the standard library as necessary through imports (e.g., 'import math'). Integrate the framework by placing its configuration and application files at the root level. Ensure that all modules and functions are appropriately imported and utilized in alignment with the MVC framework architecture .

The '__init__.py' file serves to mark a directory as a Python package, allowing the directory's modules to be imported. It's necessary because it maintains the structure of a package and helps in differentiating between a directory with modules and a package itself. Without this file, the directory would not be recognized as a package .

Modularization in Python is crucial as it breaks a program into manageable pieces or modules, promoting code reuse and organization. It aligns with software design principles such as separation of concerns and single responsibility, facilitating easier maintenance, testing, and collaboration. Modules can encapsulate functionalities, allowing teams to work on separate components without affecting the entire system .

Using 'from module import *' imports all functions and classes from a module into the global namespace, leading to potential naming conflicts and reduced readability as it becomes unclear which module a function belongs to. This practice is discouraged as explicit imports are better for clarity and maintenance, reducing the risk of unintended overwrites and making the codebase easier to understand for collaborators .

Python packages encapsulate logic by organizing modules within a directory and utilizing '__init__.py' to bundle them as a single unit. This encapsulation promotes organized and manageable code by separating functionalities into distinct namespaces. The benefit is significant in large projects, as it enhances readability, maintainability, and prevents naming conflicts among modules that might otherwise arise in a large codebase .

The 'import' statement in Python is used to bring in modules or functions and classes within modules. Variants include 'import module', 'import module as alias', 'from module import name', and 'from module import *'. Each allows for different levels of specificity and renaming, aiding in reducing naming conflicts and improving code readability .

Frameworks in Python differ from libraries in that frameworks provide a skeleton where developers define the application specifics. A library, on the other hand, is a collection of functions or classes you can call upon. A popular example of a Python framework is Django, which provides a collection of libraries to handle tasks such as authentication, database management, and URL routing .

Installing and managing additional libraries in Python involves using tools like 'pip' and 'easyinstall' to retrieve packages from repositories like PyPI. These tools handle dependencies and versioning. Using a 'requirements.txt' file can specify library versions for consistent installations across environments. Virtual environments are also utilized to manage dependencies per project without conflicts .

The Python Standard Library is integral to project development as it provides a robust set of modules that perform a variety of tasks, including data serialization, mathematics, and file handling. It enhances Python's functionality by offering readily available tools for common programming needs, reducing the need for external dependencies and simplifying code readability and maintenance .

You might also like