SlideShare a Scribd company logo
Modules and Packages in Python
Tushar B. Kute,
http://tusharkute.com
Lets see the example...
• Modules are used to categorize code in Python
into smaller part.
• A module is simply a file, where classes,
functions and variables are defined.
• Grouping similar code into a single file makes it
easy to access.
• Types:
– System modules
– User defined modules
Advantages
• Python provides the following advantages for
using module:
– 1) Reusability: Module can be used in some
other python code. Hence it provides the
facility of code reusability.
– 2) Categorization: Similar type of attributes
can be placed in one module.
Using system modules
• import keyword
• import as
• from import
The import statement
>>> import math
>>> print math.sin(67)
­0.855519978975
>>> print math.log(67)
4.20469261939
>>> print math.sqrt(67)
8.18535277187
>>> print math.pi
3.14159265359
>>> print math.e
2.71828182846
Import using alias
>>> import math as m
>>> print m.sin(90)
0.893996663601
>>> print m.log(90)
4.49980967033
>>> print m.sqrt(90)
9.48683298051
>>> print m.pi
3.14159265359
>>> print m.e
2.71828182846
Selective import
• from..import statement is used to import
particular attribute from a module.
• In case you do not want whole of the module to
be imported then you can use from ?import
statement.
Example:
>>> from math import sin, log, pi
>>> print sin(55)
­0.999755173359
>>> print log(55)
4.00733318523
>>> print pi
3.14159265359
>>> from math import *
>>> print cos(55)
0.022126756262
>>> print e
2.71828182846
User defined modules
• Any program stored in current working
directory can be imported by import statement.
• When such program (module) is imported, we
can use all functions, classes and global
variables from that program in your program.
• The import, import...as and from...import will
work in the same fashion..
• It creates a python compiled file with
extension .pyc in current working directory
Example:
fact.py
main_prog.py
Python Package
• A Package is simply a collection of similar modules, sub-
packages etc..
• Steps to create and import Package:
– 1) Create a directory, say Info
– 2) Place different modules inside the directory. We are
placing 3 modules msg1.py, msg2.py and msg3.py
respectively and place corresponding codes in respective
modules. Let us place msg1() in msg1.py, msg2() in msg2.py
and msg3() in msg3.py.
– 3) Create a file __init__.py which specifies attributes in
each module.
– 4) Import the package and use the attributes using package.
Example:
files
folder
__init__.py
Using packages
• Now create a new file outside of ‘info’ to import
the given package. We can apply all import
methods to use the package.
• It creates four new .pyc files in info folder.
tushar@tusharkute.com
Thank you
This presentation is created using LibreOffice Impress 5.1.6.2, can be used freely as per GNU General Public License
Web Resources
http://mitu.co.in
http://tusharkute.com
/mITuSkillologies @mitu_group
contact@mitu.co.in
/company/mitu-
skillologies

More Related Content

PDF
Unit-2 Introduction of Modules and Packages.pdf
PPTX
Class 12 CBSE Chapter: python libraries.pptx
PPTX
Python module 3, b.tech 5th semester ppt
PDF
Python libraries
PDF
Modules 101
PPTX
Object oriented programming design and implementation
PPTX
Object oriented programming design and implementation
PDF
Using Python Libraries.pdf
Unit-2 Introduction of Modules and Packages.pdf
Class 12 CBSE Chapter: python libraries.pptx
Python module 3, b.tech 5th semester ppt
Python libraries
Modules 101
Object oriented programming design and implementation
Object oriented programming design and implementation
Using Python Libraries.pdf

Similar to Modules and Packages in Python_Basics.pdf (20)

PDF
Python import mechanism
PPTX
Python Modules, executing modules as script.pptx
PDF
CLTL python course: Object Oriented Programming (2/3)
PPTX
Interesting Presentation on Python Modules and packages
PPTX
Python: Modules and Packages
PPTX
Modules and Packages in Python Programming Language.pptx
PPSX
Modules and packages in python
ODP
Python Modules
PPTX
package module in the python environement.pptx
PPTX
Functions_in_Python.pptx
PPTX
CLASS-11 & 12 ICT PPT Functions in Python.pptx
PDF
Functions_in_Python.pdf text CBSE class 12
PPT
mod.ppt mod.ppt mod.ppt mod.ppt mod.pp d
PPT
Python modules
PPTX
jb_Modules_in_Python.pptx jb_Modules_in_Python.pptx
PPT
python_models_import_main_init_presentation.ppt
PPT
jb_Modules_in_Python.ppt
PPTX
packages.pptx
PPTX
Python for Beginners
PPTX
Chapter 03 python libraries
Python import mechanism
Python Modules, executing modules as script.pptx
CLTL python course: Object Oriented Programming (2/3)
Interesting Presentation on Python Modules and packages
Python: Modules and Packages
Modules and Packages in Python Programming Language.pptx
Modules and packages in python
Python Modules
package module in the python environement.pptx
Functions_in_Python.pptx
CLASS-11 & 12 ICT PPT Functions in Python.pptx
Functions_in_Python.pdf text CBSE class 12
mod.ppt mod.ppt mod.ppt mod.ppt mod.pp d
Python modules
jb_Modules_in_Python.pptx jb_Modules_in_Python.pptx
python_models_import_main_init_presentation.ppt
jb_Modules_in_Python.ppt
packages.pptx
Python for Beginners
Chapter 03 python libraries
Ad

Recently uploaded (20)

PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
RMMM.pdf make it easy to upload and study
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Complications of Minimal Access Surgery at WLH
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PPTX
master seminar digital applications in india
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
01-Introduction-to-Information-Management.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
human mycosis Human fungal infections are called human mycosis..pptx
Final Presentation General Medicine 03-08-2024.pptx
RMMM.pdf make it easy to upload and study
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Complications of Minimal Access Surgery at WLH
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Week 4 Term 3 Study Techniques revisited.pptx
master seminar digital applications in india
VCE English Exam - Section C Student Revision Booklet
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Abdominal Access Techniques with Prof. Dr. R K Mishra
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
01-Introduction-to-Information-Management.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Supply Chain Operations Speaking Notes -ICLT Program
O5-L3 Freight Transport Ops (International) V1.pdf
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Renaissance Architecture: A Journey from Faith to Humanism
Ad

Modules and Packages in Python_Basics.pdf

  • 1. Modules and Packages in Python Tushar B. Kute, http://tusharkute.com
  • 2. Lets see the example... • Modules are used to categorize code in Python into smaller part. • A module is simply a file, where classes, functions and variables are defined. • Grouping similar code into a single file makes it easy to access. • Types: – System modules – User defined modules
  • 3. Advantages • Python provides the following advantages for using module: – 1) Reusability: Module can be used in some other python code. Hence it provides the facility of code reusability. – 2) Categorization: Similar type of attributes can be placed in one module.
  • 4. Using system modules • import keyword • import as • from import
  • 5. The import statement >>> import math >>> print math.sin(67) ­0.855519978975 >>> print math.log(67) 4.20469261939 >>> print math.sqrt(67) 8.18535277187 >>> print math.pi 3.14159265359 >>> print math.e 2.71828182846
  • 6. Import using alias >>> import math as m >>> print m.sin(90) 0.893996663601 >>> print m.log(90) 4.49980967033 >>> print m.sqrt(90) 9.48683298051 >>> print m.pi 3.14159265359 >>> print m.e 2.71828182846
  • 7. Selective import • from..import statement is used to import particular attribute from a module. • In case you do not want whole of the module to be imported then you can use from ?import statement.
  • 8. Example: >>> from math import sin, log, pi >>> print sin(55) ­0.999755173359 >>> print log(55) 4.00733318523 >>> print pi 3.14159265359 >>> from math import * >>> print cos(55) 0.022126756262 >>> print e 2.71828182846
  • 9. User defined modules • Any program stored in current working directory can be imported by import statement. • When such program (module) is imported, we can use all functions, classes and global variables from that program in your program. • The import, import...as and from...import will work in the same fashion.. • It creates a python compiled file with extension .pyc in current working directory
  • 11. Python Package • A Package is simply a collection of similar modules, sub- packages etc.. • Steps to create and import Package: – 1) Create a directory, say Info – 2) Place different modules inside the directory. We are placing 3 modules msg1.py, msg2.py and msg3.py respectively and place corresponding codes in respective modules. Let us place msg1() in msg1.py, msg2() in msg2.py and msg3() in msg3.py. – 3) Create a file __init__.py which specifies attributes in each module. – 4) Import the package and use the attributes using package.
  • 14. Using packages • Now create a new file outside of ‘info’ to import the given package. We can apply all import methods to use the package. • It creates four new .pyc files in info folder.
  • 15. tushar@tusharkute.com Thank you This presentation is created using LibreOffice Impress 5.1.6.2, can be used freely as per GNU General Public License Web Resources http://mitu.co.in http://tusharkute.com /mITuSkillologies @mitu_group contact@mitu.co.in /company/mitu- skillologies