Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

Python Mod & Pac

Uploaded by

Inbavathi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Python Mod & Pac

Uploaded by

Inbavathi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

PYTHON MODULES

&PACKAGES

 MODULES
 CREATING A MODULE
 USING A MODULE
 NAMING A MODULE
 BUILT-IN MODULES
 PACKAGES
 STRUCTURE OF PACKAGES
 DIFFERENCE B/W MODULES &PACKAGES
WHAT IS MODULES:
A file containing definitions of functions and
various statements is called a python module.
TYPES:
1. Built-in module
2. User-defined module
CREATING A MODULE
To create a module just save the code you want in a file with
the file extension .Py:
EXAMPLE:
get your own python server save this code in a file
named mymodule.Py

def greeting(name):
print("hello, " + name)
USING A MODULE:
Now we can use the module we just created, by using the import
statement:
EXAMPLE:
Import the module named mymodule, and call the
greeting function:

import mymodule
mymodule.greeting(“Prithi")
NAMING A MODULE:
You can name the module file whatever you like, but it must
have the file extension .py.You can create an alias when you import
a module.
Example:
Create an alias for mymodule called mx:

import mymodule as mx
a = mx.greeting(“Nila”)
print(a)
IMPORT FROM MODULE:
You can choose to import only parts from a module, by
using the from keyword.

Example:

The module named mymodule1 has two function,


which you can import whenever you like.
Example:

def add(a,b):
c=a+b
print("addition of ",a," &", b, "is", c)

def multiple(a,b):
c=a*b
print("multiple of ",a," &", b, "is", c)
Example:
Import only the person1 dictionary from the module:
from mymodule1 import add
a=int(input("enter A value:"))
b=int(input("enter B value:"))
x=add(a,b)
Note: When importing using the from keyword, do not use the
module name when referring to elements in the module.
Built –in module:
PYTHON DATES:
A date in Python is not a data type of its own, but we
can import a module named datetime to work with dates as date
objects.
Example:
Get your own Python Server Import the datetime module
and display the current date:
import datetime
x = datetime.datetime.now()
print(x)
The datetime module has many methods to return
information about the date object.
Here are a few examples, you will learn more about them later
in this chapter:
Example1:
Return the year and name of weekday:
import datetime
x =datetime.datetime.now()
print(x.year)
print(x.strftime("%A"))
Example2:
Create a date object:
import datetime
x = datetime.datetime(2020, 5, 17)
print(x)

The datetime() class also takes parameters for time and


timezone (hour, minute, second, microsecond, tzone), but they are
optional, and has a default value of 0, (None for timezone).
A reference of all the legal format codes:
Directive Description
Example
%a Weekday, short version
Wed
%A Weekday, full version
Wednesday
%w Weekday as a number 0-6, 0 is
Sunday 3
%d Day of month 01-31 31
%b Month name, short version Dec
%B Month name, full version
python math:
Python has a set of built-in math functions, including an
extensive math module, that allows you to perform mathematical tasks
on numbers.

Built-in Math Functions:


min()
max()
pow()
Example:

x = min(5, 10, 25)


y = max(5, 10, 25)
print(x)
print(y)
x = pow(4, 3)
print(x)
Math Module:
Python has also a built-in module called math, which extends the list of
mathematical functions.To use it, you must import the math module:
import math
When you have imported the math module, you can start using
methods and constants of the module.
math.sqrt()
math.pi
math.floor()
math.ceil()
Example:
import math
x = math.sqrt(64)
print(x)
a= math.ceil(1.4)
b = math.floor(1.4)
print(a)
print(b)
c= math.pi
print(c)
python package:
o Python Packages are collections of modules that provide a set of
related functionalities, and these modules are organized in a
directory hierarchy.
o Packages in Python are installed using a package manager like pip
(a tool for installing and managing Python packages).
o Each Python package must contain a file named init.py. init file
may be empty.
o This file contains the initialization code for the corresponding
package.
o Some popular Python packages are: NumPy, Pandas, and
Matplotlib.
THANK
YOU!!!
THANK
YOU!!!

You might also like