0% found this document useful (0 votes)
23 views28 pages

Understanding Python Modules and Packages

The document provides an overview of Python modules and packages, explaining their purpose in organizing code and promoting reusability. It details how to create and import modules, use the 'from' keyword for specific imports, and outlines the structure of packages containing multiple modules. Additionally, it introduces PIP as a package manager for installing Python packages and highlights several standard modules available in Python.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views28 pages

Understanding Python Modules and Packages

The document provides an overview of Python modules and packages, explaining their purpose in organizing code and promoting reusability. It details how to create and import modules, use the 'from' keyword for specific imports, and outlines the structure of packages containing multiple modules. Additionally, it introduces PIP as a package manager for installing Python packages and highlights several standard modules available in Python.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Modules

Modules refer to a file


containing Python statements and definitions.
We use modules to break down large
programs into small manageable and organized
files.
The file name is the module name with the
suffix .py which can be imported with the help
of import statement.
We can name the module file whatever
you like, but it must have the file extension .py
Advantages of modules
• Reusability: Working with modules makes the
code reusability a reality.
• Simplicity: Module focuses on a small
proportion of the problem, rather than
focusing on the entire problem.
• Scoping: A separate namespace is defined by
a module that helps to avoid collisions
between identifiers.
• To create a module just save the code we want in a file with the file
with extension .py:
Ex:
Save this code in a file named [Link]

def greeting(name):
print("Hello, " + name)

• Now we can use the module we just created, by using


the import statement:
Ex:
Import the module named mymodule, and call the greeting function:

import mymodule
[Link](“FED")
# A simple module, [Link]

def add(x, y):


return (x+y)

def subtract(x, y):


return (x-y)

# importing module [Link]


import calc

print([Link](10, 2))
• The module can contain functions, as already described, but also
variables of all types (arrays, dictionaries, objects etc):
Ex:
Save this code in the file [Link]

person1 = {
"name": "John",
"age": 36,
"country": "Norway"
}
Ex:
Import the module named mymodule, and access the person1 dictionary:

import mymodule
a = mymodule.person1["age"]
print(a)
• We can create an alias when we import a
module, by using as keyword.
Ex:
Create an alias for mymodule called mx:
import mymodule as mx
a = mx.person1["age"]
print(a)
• We can choose to import only parts from a module, by using the from keyword.
Ex:
The module named mymodule has one function and one dictionary:
def greeting(name):
print("Hello, " + name)

person1 = {
"name": "John",
"age": 36,
"country": "Norway"
}
Ex:
import only the person1 dictionary from the module:
from mymodule import person1
print (person1["age"])

When importing using from keyword, do not use the module name when referring to
elements in the module.
Ex:
person1["age"], not mymodule.person1["age"]
From import statement
• Import in python is similar to #include header_file in
C/C++.
• Python modules can get access to code from
another module by importing the file/function using
import.
• Python’s from statement lets you import specific
attributes from a module.
• The from .. import .. has the following syntax :
from modname import name1, name2, ... nameN
Ex1:
import math
print([Link])

Ex2:
from math import pi

# Note that in the above example,


# we used [Link]. Here we have used
# pi directly.
print(pi)
• In the above code module, math is not
imported, rather just pi has been imported as
a variable.
• All the functions and constants can be
imported using *.

Ex:
from math import *
print(pi)
print(factorial(6))
Ex:

import mathematics
print([Link])
Python Packages

• We usually organize our files in different folders and subfolders


based on some criteria, so that they can be managed easily and
efficiently.
• For example, we keep all our games in a Games folder and we can
even subcategorize according to the genre of the game or
something like this. The same analogy is followed by the Python
package.
• A Python module may contain several classes, functions, variables,
etc. whereas a Python package can contains several module. In
simpler terms a package is folder that contains various modules as
files.
• A package is a collection of modules in directories that give
structure and hierarchy to the modules.
Creating Package
Let’s create a package named mypckg that will
contain two modules mod1 and mod2. To
create this module follow the below steps –
Create a folder named mypckg.
Inside this folder create an empty Python file i.e.
__init__.py
Then create two modules mod1 and mod2 in
this folder.
• [Link]
• def gfg():
• print("Welcome to GFG")
• [Link]
• def sum(a, b):
• return a+b
• The hierarchy of the our package looks like this –
• mypckg
• |
• |
• ---__init__.py
• |
• |
• ---[Link]
• |
• |
• ---[Link]
• from .mod1 import gfg
• from .mod2 import sum

• Import Modules from a Package


• We can import these modules using the
from…import statement and the dot(.) operator.
• Syntax:
• import package_name.module_name
• Example: Import Module from package
• We will import the modules from the above created
package and will use the functions inside those
modules.
• from mypckg import mod1
• from mypckg import mod2

• [Link]()
• res = [Link](1, 2)
• print(res)
Python PIP

• PIP(preferred installer program) is a package


manager for Python packages, or modules if
you like.
• If you do not have PIP installed, you can
download and install it from this page:
[Link]
• Downloading a package is very easy.
• Open the command line interface and tell PIP to
download the package you want.
• Navigate your command line to the location of
Python's script directory, and type the following:
• Example
• Download a package named "camelcase":
• C:\Users\Your Name\AppData\Local\Programs\
Python\Python36-32\Scripts>pip install camelcase
• Once the package is installed, it is ready to use.
• Import the "camelcase" package into your project.
• Example
• Import and use "camelcase":
import camelcase

c = [Link]()

txt = "hello world"

print([Link](txt))
Python standard modules
• Os
• Sys
• Math
• Collections - applied on dictionary
• Statistics - to find mean, median, mode,
variance etc..
• Random - random number or element etc..
Math Module

• Some of the most popular mathematical functions


are defined in the math module. These include
trigonometric functions, representation functions,
logarithmic functions, angle conversion functions,
etc. In addition, two mathematical constants are
also defined in this module.
• Pi is a well-known mathematical constant, which is
defined as the ratio of the circumference to the
diameter of a circle and its value is
3.141592653589793.
>>> import math
>>>[Link]

3.141592653589793
[Link]()
The [Link]() method returns the natural
logarithm of a given number. The natural
logarithm is calculated to the base e.
Example: log
>>> import math
>>>[Link](10)

2.302585092994046
math.log10()
The math.log10() method returns the base-10
logarithm of the given number. It is called the
standard logarithm.
Example: log10
>>> import math
>>>math.log10(10)

1.0
[Link]()
The [Link]() method returns a float
number after raising e to the power of the
given number. In other words, exp(x) gives
e**x.
Example: Exponent
>>> import math
>>>[Link](10)

22026.465794806718
[Link]()
The [Link]() method receives two float
arguments, raises the first to the second and
returns the result. In other words, pow(4,4) is
equivalent to 4**4.
Example: Power
>>> import math
>>> [Link](2,4)
16.0
>>> 2**4 16
[Link]()
The [Link]() method returns the square
root of a given number.
Example: Square Root
>>> import math
>>> [Link](100)
10.0
>>> [Link](3)
1.7320508075688772

You might also like