Function and Libraries Notes
Function and Libraries Notes
(SENIOR SECONDARY)
CBSE Affiliation Number 1930766
COMPUTER SCIENCE - 083
GRADE:12 TOPIC: CH-3.WORKING WITH FUNCTIONS
FACULTY:M.PRIYANKA,M.Sc., B.Ed(CS).,
I.KEYWORDS:
In Python, the `global` and `nonlocal` keywords are used to modify variables that are defined outside of the
current scope. Here's why and when you might want to use them:
x = 10
def modify_global():
global x
x = 20
print("Before:", x)
modify_global()
print("After:", x)
Output:
Before: 10
After: 20
```python
def outer():
x = 10
def inner():
nonlocal x
x = 20
inner()
print("Inside outer:", x)
outer()
print("Outside outer:", x)
Output:
Inside outer: 20
Outside outer: 10
In both examples, using the `global` and `nonlocal` keywords allows the functions to modify variables
defined outside of their scope. This can be essential for maintaining and updating state across different parts
of your codebase. However, excessive use of these keywords can lead to code that is harder to understand
and maintain, so it's generally recommended to use them judiciously.
**Correct Answer:** C) It specifies that a variable should have global scope and can be accessed from
anywhere in the code.
**Correct Answer:** C) To access and modify a variable from outside its current scope but within an
enclosed function.
**Correct Answer:** C) It indicates that a variable should have global scope, accessible throughout the code.
**Correct Answer:** D) It allows a variable to be accessed and modified from outside its current scope but
within an enclosed function.
These questions should help reinforce your understanding of the global and nonlocal keywords in Python
functions.
RSK INTERNATIONAL SCHOOL (CBSE)
(SENIOR SECONDARY)
CBSE Affiliation Number 1930766
COMPUTER SCIENCE - 083
GRADE:12 TOPIC: CH-4.USING PYTHON LIBRARIES
FACULTY:M.PRIYANKA,M.Sc., B.Ed(CS).,
I. Namespace in python:
In Python, a namespace is a container that holds a set of identifiers (such as variable names, function names,
class names, etc.) and their corresponding objects. It serves as a mapping from names to objects, allowing for
organized access to these objects within a program.
Namespaces help avoid naming conflicts and provide a way to organize and manage variables and other
identifiers within a Python program.
1. **Local Namespace**: Every function invocation creates a local namespace, which includes the local
variables and parameters of that function. This namespace is destroyed once the function exits.
2. **Global Namespace**: This namespace contains names from the current module. It is created when the
module is imported or when the script is executed, and it remains active until the program terminates.
3. **Built-in Namespace**: This namespace contains built-in functions, exceptions, and attributes provided
by Python. It is automatically available in every Python program.
```python
# Global namespace
global_var = 10
def my_function():
# Local namespace
local_var = 20
print("Local variable inside function:", local_var)
print("Global variable inside function:", global_var)
my_function()
```
In this example:
- `global_var` is in the global namespace.
- `local_var` is in the local namespace of the `my_function()` function.
- We can access global variables from within a function, but we cannot directly access local variables from
outside the function.
Understanding namespaces is crucial for writing clean and maintainable code in Python, as it helps avoid
naming conflicts and improves code readability.
II. MODULES:
In Python, a module is a file containing Python code, typically consisting of functions, classes, and variables
that provide specific functionality. Modules allow you to organize your code into reusable units, making it
easier to manage and maintain large projects. You can import modules into other Python scripts to use their
functionality.
**Module**: A module is a single file containing Python code, with a `.py` extension, that defines variables,
functions, and classes. It allows for code organization, reusability, and encapsulation of related
functionalities.
```python
# math_operations.py
In this example:
- The module is named `math_operations.py`.
- It contains four functions (`add`, `subtract`, `multiply`, `divide`) that perform basic mathematical
operations.
- Each function is defined using the `def` keyword followed by the function name, parameters, and a colon
(`:`). The body of the function is indented and contains the code that defines the function's behavior.
- These functions can be imported into other Python scripts using the `import` statement, allowing other
scripts to use their functionality.
For example, to use the `add` function from this module in another Python script:
```python
# main.py
This example demonstrates how to import the `math_operations` module and use its `add` function to
perform addition in another Python script.
III.STRUCTURE OF MODULES:
Certainly! Here's a diagrammatic representation of the structure of a Python module:
```
+-----------------------------------+
| Module |
+-----------------------------------+
| Docstring |
| |
| -------------------------------- |
| | | |
| | Code | |
| | | |
| | Function Definitions | |
| | Class Definitions | |
| | Variable Definitions | |
| | Import Statements | |
| | | |
| | | |
| | | |
| | | |
| -------------------------------- |
+-----------------------------------+
```
1. **Module**: This represents the entire module file in Python. It encapsulates all the components
described below.
2. **Docstring**: This is an optional component at the beginning of a module. It is a string literal enclosed
in triple quotes (`"""`) and is used to provide documentation for the module itself. Docstrings describe the
purpose and usage of the module.
3. **Code**: This is the main body of the module where the actual Python code resides. It includes various
components such as function definitions, class definitions, variable definitions, and import statements.
- **Function Definitions**: Functions are defined using the `def` keyword followed by the function name,
parameters (if any), and a colon (`:`). The body of the function contains the code that defines the function's
behavior.
- **Class Definitions**: Classes are defined using the `class` keyword followed by the class name and a
colon (`:`). Inside the class, methods (functions) and attributes (variables) are defined.
- **Variable Definitions**: Variables are used to store data values. They are defined using variable names
followed by assignment operators (`=`) and values.
- **Import Statements**: Import statements are used to import modules or specific objects (such as
functions or classes) from modules. They allow access to functionality defined in other modules.
This diagram provides a visual representation of the structure of a Python module, illustrating how different
components such as docstrings, code, and imports are organized within the module file.
The structure of a Python module typically includes the following components:
1. **Docstring**: An optional string literal that appears as the first statement in a module, function, class, or
method definition. It provides documentation about the purpose and usage of the module.
2. **Imports**: Statements that import modules or specific objects (functions, classes, variables) from
modules. They allow access to functionality defined in other modules.
3. **Variable Definitions**: Definitions of variables that are used to store data values. Variables can be
defined at the module level or within functions or classes.
4. **Function Definitions**: Definitions of functions that encapsulate reusable blocks of code. Functions can
have parameters and return values.
5. **Class Definitions**: Definitions of classes that encapsulate data and behavior. Classes can contain
methods (functions) and attributes (variables).
```python
# This is a docstring providing documentation for the module
"""This module contains utility functions for working with strings."""
# Import statements
import math
from collections import Counter
# Variable definitions
PI = math.pi
MAX_LENGTH = 100
# Function definitions
def add(x, y):
"""Add two numbers."""
return x + y
# Class definition
class StringUtils:
"""A class containing utility functions for working with strings."""
def __init__(self):
pass
In this example:
Here's a basic example to demonstrate how you can import variables and functions from one module into
another to effectively use the same namespace across modules:
Suppose you have two modules, `module1.py` and `module2.py`, and you want to use the same namespace
in both modules.
```python
# module1.py
my_variable = 10
def my_function():
print("This is my_function() from module1.py")
```
```python
# module2.py
from module1 import my_variable, my_function
In this example:
- `module2.py` imports `my_variable` and `my_function` from `module1.py`.
- These imported objects (`my_variable` and `my_function`) can then be used in `module2.py` as if they
were defined within `module2.py`'s namespace.
This approach allows you to effectively use the same namespace (or at least parts of it) across multiple
modules by importing objects from one module into another.
Here's an example demonstrating how to import functions with the same name from different modules using
aliases:
Suppose you have two modules, `module1.py` and `module2.py`, each containing a function named
`my_function()`:
```python
# module1.py
def my_function():
print("This is my_function() from module1.py")
# module2.py
def my_function():
print("This is my_function() from module2.py")
```
Now, in your main Python script or another module, you can import these functions with aliases like so:
```python
# main.py
In this example, `my_function()` from `module1.py` is imported with the alias `func1`, and `my_function()`
from `module2.py` is imported with the alias `func2`. This allows you to differentiate between the functions
and call them accordingly in your code.
VI.IMPORTING MODULES IN PYTHON:
In Python, there are several methods for importing modules, each with its own syntax and use cases. Here are
the main methods of importing modules in Python:
1. **Standard Import**: This is the most common method of importing modules. It involves using the
`import` keyword followed by the module name.
```python
import module_name
```
Example:
```python
import math
```
2. **Import with Alias**: This method allows you to import a module and assign it an alias (alternate name)
that can be used to reference the module's contents.
```python
import module_name as alias
```
Example:
```python
import numpy as np
```
3. **Import Specific Objects**: Instead of importing the entire module, you can import specific objects
(functions, classes, variables) from the module using the `from ... import ...` syntax.
```python
from module_name import object1, object2, ...
```
Example:
```python
from math import sqrt, pi
```
4. **Import Everything (Not Recommended)**: You can import all objects from a module into the current
namespace. However, this is generally not recommended because it can lead to namespace pollution and
make it unclear which objects are being used.
```python
from module_name import *
```
Example:
```python
from math import *
```
5. **Conditional Import**: You can conditionally import a module based on certain conditions using the
`import` statement within an `if` block.
```python
if condition:
import module_name
```
Example:
```python
if os.name == 'posix':
import posix_module
```
6. **Dynamic Import**: You can import a module dynamically at runtime using the
`importlib.import_module()` function from the `importlib` module.
```python
import importlib
module_name = importlib.import_module('module_name')
```
Example:
```python
module_name = importlib.import_module('math')
```
These are the main methods of importing modules in Python. Each method has its own advantages and use
cases, so choose the one that best fits your needs and coding style.
VII.STRUCTURE OF LIBRARIES:
In Python, a library is typically a collection of related packages, modules, and resources that provide reusable
functionality. Here's a typical structure for a Python library:
```
mylibrary/
README.md
LICENSE
setup.py
requirements.txt
docs/
index.md
api_reference.md
...
mylibrary/
__init__.py
module1.py
module2.py
subpackage1/
__init__.py
submodule1.py
submodule2.py
subpackage2/
__init__.py
submodule3.py
submodule4.py
tests/
__init__.py
test_module1.py
test_module2.py
...
```
1. **`README.md`**: This file typically contains information about the library, including its purpose,
usage, installation instructions, and examples.
2. **`LICENSE`**: This file specifies the license under which the library is distributed. Common licenses
include MIT, BSD, GPL, and Apache.
3. **`setup.py`**: This file is used to package and distribute the library. It contains metadata about the
library, such as its name, version, dependencies, etc.
4. **`requirements.txt`**: This file lists the dependencies required by the library. It's often used with
package management tools like pip to install the necessary dependencies.
5. **`docs/`**: This directory contains documentation for the library. It may include files like `index.md` for
an overview of the library, `api_reference.md` for the API reference, and other documentation files.
6. **`mylibrary/`**: This directory contains the source code for the library.
7. **`__init__.py`**: This file indicates that `mylibrary` is a Python package. It may be empty or contain
initialization code.
8. **`module1.py` and `module2.py`**: These are Python module files containing the core functionality of
the library.
9. **`subpackage1/` and `subpackage2/`**: These are sub-packages within `mylibrary`. Each sub-package
follows the same structure as the main package, with its own `__init__.py` file and Python module files.
11. **`tests/`**: This directory contains unit tests for the library. Each test typically corresponds to a module
or function and verifies its behavior.
12. **`test_module1.py` and `test_module2.py`**: These are test modules containing test cases for
`module1.py` and `module2.py`, respectively. They may import the modules they are testing and run various
test functions to verify their behavior.
This structure allows you to organize your library's code, documentation, and tests in a clear and
maintainable way, making it easy for users to understand, install, and use your library.
VIII.Steps to create Python libraries:
Creating libraries in Python involves several steps, which can be represented diagrammatically as follows:
```
+-----------------------------------+
| 1. Organize Your Code |
| |
| Group related functionality |
| into modules and packages |
+-----------------------------------+
|
v
+-----------------------------------+
| 2. Write Your Code |
| |
| Implement the functionality |
| in your modules and packages |
+-----------------------------------+
|
v
+-----------------------------------+
| 3. Document Your Code |
| |
| Add docstrings to functions, |
| classes, and modules |
+-----------------------------------+
|
v
+-----------------------------------+
| 4. Write Tests |
| |
| Create test modules to |
| ensure library functionality |
+-----------------------------------+
|
v
+-----------------------------------+
| 5. Package Your Library |
| |
| Create setup.py to specify |
| metadata about the library |
+-----------------------------------+
|
v
+-----------------------------------+
| 6. Distribute Your Library |
| |
| Publish your library to a |
| package index like PyPI |
+-----------------------------------+
```
This diagram illustrates the steps involved in creating libraries in Python. Each step contributes to the overall
process of organizing, implementing, documenting, testing, packaging, and distributing the library.
Following these steps ensures that the library is well-structured, well-documented, thoroughly tested, and
easily accessible to other developers.
Creating a Python library involves organizing your code into reusable modules and packages that can be
easily distributed and imported by other Python programs. Here are the basic steps to create a Python library:
1. **Organize Your Code**: Group related functions, classes, and constants into modules and packages.
Modules are single files containing Python code, while packages are directories containing multiple modules
and a special `__init__.py` file.
2. **Write Your Code**: Implement the functionality you want your library to provide. This includes
defining functions, classes, and any necessary constants or variables.
3. **Document Your Code**: Write documentation for your library to explain its purpose, usage, and API.
This documentation will help users understand how to use your library effectively.
4. **Add Tests**: Write tests to ensure that your library functions as expected and to catch any bugs or
regressions that may occur during development.
5. **Package Your Library**: Package your library using Python packaging tools such as setuptools or
poetry. This involves creating a `setup.py` file and possibly a `setup.cfg` file to specify metadata about your
library, such as its name, version, dependencies, etc.
6. **Distribute Your Library**: Publish your library to a package index such as PyPI (the Python Package
Index) so that others can easily install and use it with tools like pip.
def greet(name):
"""Print a greeting message."""
print(f"Hello, {name}!")
def test_greet():
assert greet("Alice") == "Hello, Alice!"
def test_add():
assert add(2, 3) == 5
```
setup(
name='mylibrary',
version='0.1',
packages=find_packages(),
description='A simple Python library',
author='Your Name',
author_email='[email protected]',
url='https://github.com/yourusername/mylibrary',
)
```
After following these steps, your Python library is ready for distribution and can be installed by others using
`pip install mylibrary`.
1. **Identify the Library**: Determine the name of the library you want to import. This can be a standard
library that comes with Python or a third-party library that you've installed.
2. **Use the `import` Statement**: Use the `import` statement followed by the name of the library to import
the entire library. Alternatively, you can import specific objects (such as functions, classes, or variables)
from the library using the `from ... import ...` syntax.
3. **Access Library Functionality**: Once imported, you can access the functionality provided by the
library in your code. This may involve calling functions, using classes, or accessing variables defined in the
library.
4. **Read Documentation (Optional)**: It's a good practice to refer to the official documentation of the
library to understand its purpose, usage, and available functionality. This can help you use the library
effectively and efficiently.
5. **Handle Errors (Optional)**: Handle any potential errors or exceptions that may occur while using the
library. This may involve using try-except blocks or other error handling techniques.
6. **Test Your Code (Optional)**: Write test cases to verify that your code works as expected when using
the library. Testing helps ensure that your code behaves correctly under different scenarios.
Here's an example demonstrating how to import and use the `math` library, which is a standard library in
Python:
```python
# Import the entire math library
import math
In this example:
- We import the entire `math` library using the `import math` statement.
- We use functions and variables provided by the `math` library, such as `sqrt()` and `pi`.
- We then import specific objects (`factorial` and `sin`) from the `math` library using the `from math import
factorial, sin` statement.
- Finally, we use these specific functions in our code.
1. **Import the Module**: Import the desired module using the `import` statement. You can import the
entire module or specific objects (functions, classes, variables) from the module.
2. **Use the Functionality**: Access the functionality provided by the module. This may involve calling
functions, using classes, or accessing variables defined in the module.
3. **Read the Documentation**: Refer to the official documentation of the module to understand its purpose,
usage, and available functionality. This can help you use the module effectively and efficiently.
4. **Handle Errors**: Handle any potential errors or exceptions that may occur while using the module. This
may involve using try-except blocks or other error handling techniques.
5. **Test Your Code**: Write test cases to verify that your code works as expected when using the standard
library module. Testing helps ensure that your code behaves correctly under different scenarios.
6. **Optimize Performance**: If necessary, optimize the performance of your code by considering factors
such as memory usage, execution time, and algorithm efficiency. This may involve profiling your code to
identify bottlenecks and optimizing critical sections.
Here's a simple example demonstrating these steps using the `random` module from the Python standard
library:
```python
# Import the random module
import random
In this example:
- We import the `random` module to generate random numbers and make random choices.
- We use the `randint()` function to generate a random number between 1 and 100.
- We handle potential errors using a try-except block to catch `ValueError` exceptions that may occur when
making a random choice from a list.
- We could write test cases to verify the behavior of functions from the `random` module and optimize
performance if needed.
XI.STRUCTURE OF PACKAGE:
In Python, a package is simply a directory containing Python modules and a special file named `__init__.py`.
This `__init__.py` file can be empty or can contain initialization code for the package. Here's the structure of
a typical Python package:
```
mypackage/
__init__.py
module1.py
module2.py
subpackage1/
__init__.py
submodule1.py
submodule2.py
subpackage2/
__init__.py
submodule3.py
submodule4.py
tests/
__init__.py
test_module1.py
test_module2.py
```
2. **`__init__.py`**: This file indicates that `mypackage` is a Python package. It can be empty or contain
initialization code that runs when the package is imported.
3. **`module1.py` and `module2.py`**: These are Python module files containing code that provides
specific functionalities. They can contain functions, classes, variables, etc.
4. **`subpackage1/` and `subpackage2/`**: These are sub-packages within `mypackage`. Each sub-package
follows the same structure as the main package, with its own `__init__.py` file and Python module files.
6. **`tests/`**: This directory contains test modules for testing the functionality of the package. Each test
module typically corresponds to a module or sub-module and contains test cases to verify the behavior of the
code.
7. **`test_module1.py` and `test_module2.py`**: These are test modules containing test cases for
`module1.py` and `module2.py`, respectively. They may import the modules they are testing and run various
test functions to verify their behavior.
This structure allows you to organize your code into logical units (modules and sub-packages) and provides a
standardized way to distribute and import your code as a package.
```
+-----------------------------------+
| 1. Organize Your Modules |
| |
| Create a directory structure |
| Group related modules |
+-----------------------------------+
|
v
+-----------------------------------+
| 2. Add __init__.py |
| |
| Create __init__.py files in |
| each directory |
+-----------------------------------+
|
v
+-----------------------------------+
| 3. Write Your Code |
| |
| Implement functionality in |
| your modules |
+-----------------------------------+
|
v
+-----------------------------------+
| 4. Document Your Code |
| |
| Add docstrings to functions, |
| classes, and modules |
+-----------------------------------+
|
v
+-----------------------------------+
| 5. Write Tests |
| |
| Create test modules to |
| ensure package functionality|
+-----------------------------------+
|
v
+-----------------------------------+
| 6. Package Your Package |
| |
| Create setup.py to specify |
| metadata about the package |
+-----------------------------------+
|
v
+-----------------------------------+
| 7. Distribute Your Package |
| |
| Publish your package to a |
| package index like PyPI |
+-----------------------------------+
```
This diagram illustrates the sequential steps involved in creating packages in Python, starting from
organizing modules to distributing the package. Each step builds upon the previous one, ultimately leading to
the creation of a reusable and distributable package.
Creating packages in Python involves organizing multiple modules into a directory structure and adding a
special `__init__.py` file to indicate that the directory is a package. Here's the procedure to create a package:
1. **Organize Your Modules**: Group related modules into a directory structure. Each module should be a
Python file (ending with `.py`).
2. **Add `__init__.py`**: Within each directory that you want to treat as a package, add a special file named
`__init__.py`. This file can be empty or can contain initialization code for the package.
3. **Write Your Code**: Implement the functionality in your modules as needed. You can define functions,
classes, constants, etc., in your modules.
4. **Document Your Code**: Add docstrings to your functions, classes, and modules to document their
purpose, usage, and API.
5. **Write Tests**: Create test modules to ensure that your package functions as expected.
6. **Package Your Package**: Create a `setup.py` file to specify metadata about your package, such as its
name, version, dependencies, etc.
7. **Distribute Your Package**: Publish your package to a package index such as PyPI so that others can
easily install and use it with tools like pip.
`module1.py`:
```python
# module1.py
def function1():
print("This is function1 from module1")
```
`module2.py`:
```python
# module2.py
def function2():
print("This is function2 from module2")
```
3. **Add `__init__.py`**:
The `__init__.py` files can be empty or can contain initialization code. In this example, they can be left
empty.
`test_module1.py`:
```python
# test_module1.py
from mypackage import module1
def test_function1():
assert module1.function1() is None
```
`test_module2.py`:
```python
# test_module2.py
from mypackage import module2
def test_function2():
assert module2.function2() is None
```
`setup.py`:
```python
# setup.py
from setuptools import setup, find_packages
setup(
name='mypackage',
version='0.1',
packages=find_packages(),
description='A simple Python package',
author='Your Name',
author_email='[email protected]',
url='https://github.com/yourusername/mypackage',
)
```
After following these steps, your Python package (`mypackage`) is ready for distribution and can be installed
by others using `pip install mypackage`.