CHAPTER - 03 PYTHON LIBRARIES
Unit I Programming and Computational Thinking (PCT-2) (80 Theory + 70 Practical) DCSc & Engg, PGDCA,ADCA,MCA.MSc(IT),Mtech(IT),MPhil (Comp. Sci) Department of Computer Science, Sainik School Amaravathinagar Cell No: 9431453730 Praveen M Jigajinni Prepared by Courtesy CBSE Class XII
INTRODUCTION
INTRODUCTION A Module is a file containing python definitions, functions, variables, classes and statements with .py extension. Python package is a directory of python module. A Library is a collection of various packages. There is no difference between library and python package. Library is used to loosely describe a collection of core or main modules.
COMPONENTS OF PYTHON PROGRAM
COMPONENTS OF PYTHON PROGRAM A Module is a file that contains python code. The python program comprises of three main components i) Library or Package ii) Module iii) Functions/Sub-Modules
ADVANTAGES OF MODULES
ADVANTAGES OF MODULES 1. Reusability 2. Clarity 3. Classification/ Grouping of code 4. Easy to understand
IMPORTING MODULES
IMPORTING MODULES t Python module files have an extension .py These modules can be imported in the following ways: 1) import statement 2) from statement 3) from * statement
IMPORTING MODULES- import t import statement is used to include the modules in other programs. syntax : import <filename> example: import math more than one module can be inserted in a python program syntax : import <filename> ,<filename>, <filename>…….. for example: import math,os
IMPORTING MODULES- import t using import statement one can view all the functions and other attributes of a particular module for example: import math dir(math)
IMPORTING MODULES- import
IMPORTING MODULES- from t importing module can be done using from statement specific attributes can be included in other programs. syntax : from <filename> import function name example: from math import math.sqrt
IMPORTING MODULES- from* t from* statement can be used to import all names from the module in to the current calling name space. syntax : from <filename> import * example: from math import * math.sqrt(4) we can access any function by using dot notation.
NAMESPACES
NAMESPACES t When we import modules in a particular program these modules will become part of that program and are called as namespace. Python impliments namespaces in the form of dictionaries. It maintains a name to object mapping. There are three types of namespaces 1) Global 2) Local 3) Built in
NAMESPACES Built in name space Global name space Local name space
NAME RESOLUTION
NAME RESOLUTION t9lo Already we know the scope rules of python programming. For every name reference within a program when you access a variable python follows name resolution rule i.e LEGB (Local, Enclosed, Global, Built-in) Contd.. Next slide
NAME RESOLUTION Built in name space Global name space Local name space Enclosed
MODULE ALIASING
MODULE ALIASING t9lo One can create an alias while importing module in a program syntax: import <filename> as <alias name> for example: import math as m m.sqrt(4)
MEMBER ALIASING
MEMBER ALIASING t9lo Like module aliasing members are also aliased syntax: import <filename> as <alias name>, member as alias name for example: import test as t, add as sum test.py is module file and is referred to as t and add is the function, it is referred to as sum.
PACKAGE/LIBRARY
PACKAGE/LIBRARY t9lo Python packages are the collection of related modules. You can import a package or create your own. The main difference between a module and a package is that package is a collection of modules and has an __init__.py file
PACKAGE/LIBRARY t9lo Python package is a simply directory of python modules Steps to create and import a package 1. create a directory named ‘Gemetry’ 2. add modules area.py and volume.py 3. create a file __init__.py in directory ‘Geometry’. The __init__.py files are required to make python treat the directory as containing package
PACKAGE/LIBRARY GEOMETRY Area.py Volume.py FOLDER FILES
PACKAGE/LIBRARY FOLDER IS CREATED
PACKAGE/LIBRARY AREA MODULE IS CREATED
PACKAGE/LIBRARY VOLUME MODULE IS CREATED
PACKAGE/LIBRARY CREATING __init__.py FILE
PACKAGE/LIBRARY __init__.py FILE What is __init__.py file? __init__.py is simply a file used to consider directories on the disk as package of python. It is basically used to initilize the python package
LOCATING MODULES
PACKAGE/LIBRARY Python searches module in the following manner 1) Searches in current directory 2) If the module is not found then searches each directory in the shell variable PYTHONPATH 3) If all else fails, python checks the default path which is the installation location of the python
PACKAGE/LIBRARY Python searches module in the following manner 1) Searches in current directory 2) If the module is not found then searches each directory in the shell variable PYTHONPATH 3) If all else fails, python checks the default path which is the installation location of the python
pip
What is pip? pip is a package-management system used to install and manage software packages written in Python. To check pip version run, pip --version at dos prompt
PYTHON STANDARD LIBRARY
PYTHON STANDARD LIBRARY DATE AND TIME MODULE. import datetime v_date=datetime.date.today() vyear = v_date.year() vmonth = v_date.month() vday = v_date.day()
PYTHON STANDARD LIBRARY DATE AND TIME MODULE. import datetime v_date=datetime.date.today() vnow = v_date.now() vhour = v_date.hour() vmin = v_date.minute() vsec = v_date.second()
CLASS TEST 1. What are the components of python program. 2.Explain the ways to import a module in python program. 3.What is namespace? Explain in detail 4. What is python package? Write down the steps to create a python package and also write a programs and create a package. Class : XII Time: 40 Min Topic: Python Libraries Max Marks: 40 Each Question carries 5 Marks
Thank You

Chapter 03 python libraries

  • 1.
  • 2.
    Unit I Programming andComputational Thinking (PCT-2) (80 Theory + 70 Practical) DCSc & Engg, PGDCA,ADCA,MCA.MSc(IT),Mtech(IT),MPhil (Comp. Sci) Department of Computer Science, Sainik School Amaravathinagar Cell No: 9431453730 Praveen M Jigajinni Prepared by Courtesy CBSE Class XII
  • 3.
  • 4.
    INTRODUCTION A Module isa file containing python definitions, functions, variables, classes and statements with .py extension. Python package is a directory of python module. A Library is a collection of various packages. There is no difference between library and python package. Library is used to loosely describe a collection of core or main modules.
  • 5.
  • 6.
    COMPONENTS OF PYTHONPROGRAM A Module is a file that contains python code. The python program comprises of three main components i) Library or Package ii) Module iii) Functions/Sub-Modules
  • 7.
  • 8.
    ADVANTAGES OF MODULES 1.Reusability 2. Clarity 3. Classification/ Grouping of code 4. Easy to understand
  • 9.
  • 10.
    IMPORTING MODULES t Python modulefiles have an extension .py These modules can be imported in the following ways: 1) import statement 2) from statement 3) from * statement
  • 11.
    IMPORTING MODULES- import t importstatement is used to include the modules in other programs. syntax : import <filename> example: import math more than one module can be inserted in a python program syntax : import <filename> ,<filename>, <filename>…….. for example: import math,os
  • 12.
    IMPORTING MODULES- import t usingimport statement one can view all the functions and other attributes of a particular module for example: import math dir(math)
  • 13.
  • 14.
    IMPORTING MODULES- from t importingmodule can be done using from statement specific attributes can be included in other programs. syntax : from <filename> import function name example: from math import math.sqrt
  • 15.
    IMPORTING MODULES- from* t from*statement can be used to import all names from the module in to the current calling name space. syntax : from <filename> import * example: from math import * math.sqrt(4) we can access any function by using dot notation.
  • 16.
  • 17.
    NAMESPACES t When we importmodules in a particular program these modules will become part of that program and are called as namespace. Python impliments namespaces in the form of dictionaries. It maintains a name to object mapping. There are three types of namespaces 1) Global 2) Local 3) Built in
  • 18.
    NAMESPACES Built in namespace Global name space Local name space
  • 19.
  • 20.
    NAME RESOLUTION t9lo Already weknow the scope rules of python programming. For every name reference within a program when you access a variable python follows name resolution rule i.e LEGB (Local, Enclosed, Global, Built-in) Contd.. Next slide
  • 21.
    NAME RESOLUTION Built inname space Global name space Local name space Enclosed
  • 22.
  • 23.
    MODULE ALIASING t9lo One cancreate an alias while importing module in a program syntax: import <filename> as <alias name> for example: import math as m m.sqrt(4)
  • 24.
  • 25.
    MEMBER ALIASING t9lo Like modulealiasing members are also aliased syntax: import <filename> as <alias name>, member as alias name for example: import test as t, add as sum test.py is module file and is referred to as t and add is the function, it is referred to as sum.
  • 26.
  • 27.
    PACKAGE/LIBRARY t9lo Python packages arethe collection of related modules. You can import a package or create your own. The main difference between a module and a package is that package is a collection of modules and has an __init__.py file
  • 28.
    PACKAGE/LIBRARY t9lo Python package isa simply directory of python modules Steps to create and import a package 1. create a directory named ‘Gemetry’ 2. add modules area.py and volume.py 3. create a file __init__.py in directory ‘Geometry’. The __init__.py files are required to make python treat the directory as containing package
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
    PACKAGE/LIBRARY __init__.py FILE What is__init__.py file? __init__.py is simply a file used to consider directories on the disk as package of python. It is basically used to initilize the python package
  • 35.
  • 36.
    PACKAGE/LIBRARY Python searches modulein the following manner 1) Searches in current directory 2) If the module is not found then searches each directory in the shell variable PYTHONPATH 3) If all else fails, python checks the default path which is the installation location of the python
  • 37.
    PACKAGE/LIBRARY Python searches modulein the following manner 1) Searches in current directory 2) If the module is not found then searches each directory in the shell variable PYTHONPATH 3) If all else fails, python checks the default path which is the installation location of the python
  • 38.
  • 39.
    What is pip? pipis a package-management system used to install and manage software packages written in Python. To check pip version run, pip --version at dos prompt
  • 40.
  • 41.
    PYTHON STANDARD LIBRARY DATEAND TIME MODULE. import datetime v_date=datetime.date.today() vyear = v_date.year() vmonth = v_date.month() vday = v_date.day()
  • 42.
    PYTHON STANDARD LIBRARY DATEAND TIME MODULE. import datetime v_date=datetime.date.today() vnow = v_date.now() vhour = v_date.hour() vmin = v_date.minute() vsec = v_date.second()
  • 43.
    CLASS TEST 1. Whatare the components of python program. 2.Explain the ways to import a module in python program. 3.What is namespace? Explain in detail 4. What is python package? Write down the steps to create a python package and also write a programs and create a package. Class : XII Time: 40 Min Topic: Python Libraries Max Marks: 40 Each Question carries 5 Marks
  • 44.