SlideShare a Scribd company logo
1 of 30
Download to read offline
PYTHON MODULE
Group of functions, classes, variables
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ,
PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
What is Python Module
 A Module is a file containing Python definitions
(docstrings) , functions, variables, classes and
statements.
 Act of partitioning a program into individual
components(modules) is called modularity. A module is a
separate unit in itself.
 It reduces its complexity to some degree
 It creates numbers of well-defined, documented boundaries
within program.
 Its contents can be reused in other program, without having
to rewrite or recreate them.
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Structure of Python module
 A python module is simply a normal python file(.py)
and contains functions, constants and other elements.
 Python module may contains following objects:
docstring Triple quoted comments. Useful for documentation
purpose
Variables and
constants
For storing values
Classes To create blueprint of any object
Objects Object is an instance of class. It represent class in real world
Statements Instruction
Functions Group of statements
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Composition/Structure of python module
MODULES
VARIABLES
FUNCTIONS
VARIABLES
CLASSES
MEMBERS
METHODS
OTHER
PYTHON
MODULES
OTHER
PYTHON
MODULES
IMPORT
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Importing Python modules
 To import entire module
 import <module name>
 Example: import math
 To import specific function/object from module:
 from <module_name> import <function_name>
 Example: from math import sqrt
 import * : can be used to import all names from
module into current calling module
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Accessing function/constant of imported module
 To use function/constant/variable of imported module
we have to specify module name and function name
separated by dot(.). This format is known as dot
notation.
 <module_name>.<function_name>
 Example: print(math.sqrt(25))
 How ever if only particular function is imported using
from then module name before function name is not
required. We will se examples with next slides.
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Types of Modules
 There are various in-built module in python, we will
discuss few of them
 Math module
 Random module
 Statistical module
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Math module
 This module provides various function to perform
arithmetic operations.
 Example of functions in math modules are:
 Example of variables in math modules are:
 pi
 e
sqrt ceil floor pow
fabs sin cos tan
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Math module functions
 sqrt(x) : this function returns the square root of
number(x).
 pow(x,y) : this function returns the (x)y
 ceil (x) : this function return the x rounded to next
integer.
module name is
required before
function name here
module name is not
required before
function name here
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Math module functions
 floor(x) : this function returns the x rounded to previous
integer.
 fabs(x) : this function returns absolute value of float x.
absolute value means number without any sign
 sin (x) : it return sine of x (measured in radian)
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Math module functions
 cos(x) : it return cosine of x (measured in radian)
 tan(x) : it return tangent of x (measured in radian)
 pi : return the constant value of pi (22/7)
 e : return the constant value of constant e
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Using Random Module
 Python has a module namely random that provides
random – number generators. Random number
means any number generated within the given
range.
 To generate random number in Python we have to
import random module
 2 most common method to generate random number
in python are :
 random() function
 randint(a,b) function
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
random() function
 It is floating point random number generator
between 0.0 to 1.0. here lower limit is inclusive
where as upper limit is less than 1.0.
 0<=N<1
 Examples:
Output is less than 1
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
random() function
 To generate random number between given range
of values using random(), the following format
should be used:
 Lower_range + random() * (upper_range-lower_range)
 For example to generate number between 10 to 50:
 10 + random() * (40)
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
randint() function
 Another way to generate random number is
randint() function, but it generate integer numbers.
 Both the given range values are inclusive i.e. if we
generate random number as :
 randint(20,70)
 In above example random number between 20 to 70 will
be taken. (including 20 and 70 also)
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
E
X
A
M
P
L
E
for more updates visit: www.python4csip.com
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
O
U
T
P
U
T
for more updates visit: www.python4csip.com
Just a Minute…
 Give the following python code, which is repeated
four times. What could be the possible set of
output(s) out of four sets (ddd is any combination of
digits)
import random
print(15 + random.random()*5)
a) b) c) d)
17.ddd
19.ddd
20.ddd
15.ddd
15.ddd
17.ddd
19.ddd
18.ddd
14.ddd
16.ddd
18.ddd
20.ddd
15.ddd
15.ddd
15.ddd
15.ddd
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Just a Minute…
 What could be the minimum possible and maximum
possible numbers by following code
import random
print(random.randint(3,10)-3)
 In a school fest, three randomly chosen students out
of 100 students (having roll number 1 -100) have to
present the bouquet to the guests. Help the school
authorities choose three students randomly
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Just a Minute…
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Just a Minute… VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
Look at the following Python code and find the possible output(s) from the
options (i) to (iv) following it. Also, write the maximum and the minimum values
that can be assigned to the variable PICKER.
Note:
‐ Assume all the required header files are already being included in the code.
‐ The function randint() generates an integer between 1 to n
import random
PICKER=1+random.randint(0,2)
COLOR=[”BLUE”,”PINK”,”GREEN”,”RED”]
for I in range(1,PICKER+1):
for j in range(I+1):
print(COLOR[j],end=‘’)
print()
for more updates visit: www.python4csip.com
What are the possible outcome(s) executed from the following
code? Also specify the maximum and minimum values that
can be assigned to variable PICK
1)
DELHIDELHI
MUMBAIMUMBAI
CHENNAICHENNAI
KOLKATAKOLKATA
2)
DELHI
DELHIMUMBAI
DELHIMUMBAICHENNAI
3)
DELHI
MUMBAI
CHENNAI
KOKLATA
4)
DELHI
DELHIMUMBAI
KOLKATAKOLKATAKOLKATA
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
randrange() function
 This function is also used to generate random
number within given range.
 Syntax
 randrange(start,stop,step)
random output between 5 to 14, may vary
It will generate random
number between 5 to 14
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
randrange() function
It will generate
random number
between 1 to 29 with
stepping of 2 i.e. it
will generate number
with gap of 2 i.e.
1,3,5,7 and so on
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Mathematics Game for Kids
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Mathematics Game for Kids
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Statistical Module
 This module provides functions for calculating
mathematical statistics of numeric (Real-valued)
data.
 We will deal with 3 basic function under this module
 Mean
 Median
 mode
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Mean
 The mean is the average of all numbers and is
sometimes called the arithmetic mean.
55, is the average of all numbers in the list
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Median
 The median is the middle number in a group of
numbers.
With odd number of
elements it will simply
return the middle
position value
With even number of
elements, it will return
the average of value
at mid + mid-1 i.e.
(50+60)/2 = 55.0
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Mode
 The mode is the number that occurs most often within
a set of numbers i.e. most common data in list.
Here, 10 occurs
most in the list.
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com

More Related Content

Similar to PYTHON MODULE: Everything You Need to Know

Advanced Web Technology ass.pdf
Advanced Web Technology ass.pdfAdvanced Web Technology ass.pdf
Advanced Web Technology ass.pdfsimenehanmut
 
Introduction to python & its applications.ppt
Introduction to python & its applications.pptIntroduction to python & its applications.ppt
Introduction to python & its applications.pptPradeepNB2
 
Boost.Python: C++ and Python Integration
Boost.Python: C++ and Python IntegrationBoost.Python: C++ and Python Integration
Boost.Python: C++ and Python IntegrationGlobalLogic Ukraine
 
WORKING WITH FUNCTIONS
WORKING WITH FUNCTIONSWORKING WITH FUNCTIONS
WORKING WITH FUNCTIONSPihuJha1
 
PHP applications/environments monitoring: APM & Pinba
PHP applications/environments monitoring: APM & PinbaPHP applications/environments monitoring: APM & Pinba
PHP applications/environments monitoring: APM & PinbaPatrick Allaert
 
Chapter 1 Class 12 Computer Science Unit 1
Chapter 1 Class 12 Computer Science Unit 1Chapter 1 Class 12 Computer Science Unit 1
Chapter 1 Class 12 Computer Science Unit 1ssusera7a08a
 
Cluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in CCluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in CSteffen Wenz
 
Revision of the basics of python1 (1).pdf
Revision of the basics of python1 (1).pdfRevision of the basics of python1 (1).pdf
Revision of the basics of python1 (1).pdfoptimusnotch44
 
Chapter Eight(3)
Chapter Eight(3)Chapter Eight(3)
Chapter Eight(3)bolovv
 
Numba: Array-oriented Python Compiler for NumPy
Numba: Array-oriented Python Compiler for NumPyNumba: Array-oriented Python Compiler for NumPy
Numba: Array-oriented Python Compiler for NumPyTravis Oliphant
 
PythonStudyMaterialSTudyMaterial.pdf
PythonStudyMaterialSTudyMaterial.pdfPythonStudyMaterialSTudyMaterial.pdf
PythonStudyMaterialSTudyMaterial.pdfdata2businessinsight
 
008. PROGRAM EFFICIENCY computer science.pdf
008. PROGRAM EFFICIENCY computer science.pdf008. PROGRAM EFFICIENCY computer science.pdf
008. PROGRAM EFFICIENCY computer science.pdfomchoubey297
 
Python na Infraestrutura 
MySQL do Facebook

Python na Infraestrutura 
MySQL do Facebook
Python na Infraestrutura 
MySQL do Facebook

Python na Infraestrutura 
MySQL do Facebook
Artur Rodrigues
 
Python Basis Tutorial
Python Basis TutorialPython Basis Tutorial
Python Basis Tutorialmd sathees
 
Rcpp: Seemless R and C++
Rcpp: Seemless R and C++Rcpp: Seemless R and C++
Rcpp: Seemless R and C++Romain Francois
 

Similar to PYTHON MODULE: Everything You Need to Know (20)

Advanced Web Technology ass.pdf
Advanced Web Technology ass.pdfAdvanced Web Technology ass.pdf
Advanced Web Technology ass.pdf
 
python modules1522.pdf
python modules1522.pdfpython modules1522.pdf
python modules1522.pdf
 
Introduction to python & its applications.ppt
Introduction to python & its applications.pptIntroduction to python & its applications.ppt
Introduction to python & its applications.ppt
 
Boost.Python: C++ and Python Integration
Boost.Python: C++ and Python IntegrationBoost.Python: C++ and Python Integration
Boost.Python: C++ and Python Integration
 
C463_02_python.ppt
C463_02_python.pptC463_02_python.ppt
C463_02_python.ppt
 
C463_02_python.ppt
C463_02_python.pptC463_02_python.ppt
C463_02_python.ppt
 
WORKING WITH FUNCTIONS
WORKING WITH FUNCTIONSWORKING WITH FUNCTIONS
WORKING WITH FUNCTIONS
 
PHP applications/environments monitoring: APM & Pinba
PHP applications/environments monitoring: APM & PinbaPHP applications/environments monitoring: APM & Pinba
PHP applications/environments monitoring: APM & Pinba
 
Chapter 1 Class 12 Computer Science Unit 1
Chapter 1 Class 12 Computer Science Unit 1Chapter 1 Class 12 Computer Science Unit 1
Chapter 1 Class 12 Computer Science Unit 1
 
Cluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in CCluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in C
 
Revision of the basics of python1 (1).pdf
Revision of the basics of python1 (1).pdfRevision of the basics of python1 (1).pdf
Revision of the basics of python1 (1).pdf
 
Chapter Eight(3)
Chapter Eight(3)Chapter Eight(3)
Chapter Eight(3)
 
Functions2.pptx
Functions2.pptxFunctions2.pptx
Functions2.pptx
 
Numba: Array-oriented Python Compiler for NumPy
Numba: Array-oriented Python Compiler for NumPyNumba: Array-oriented Python Compiler for NumPy
Numba: Array-oriented Python Compiler for NumPy
 
PythonStudyMaterialSTudyMaterial.pdf
PythonStudyMaterialSTudyMaterial.pdfPythonStudyMaterialSTudyMaterial.pdf
PythonStudyMaterialSTudyMaterial.pdf
 
008. PROGRAM EFFICIENCY computer science.pdf
008. PROGRAM EFFICIENCY computer science.pdf008. PROGRAM EFFICIENCY computer science.pdf
008. PROGRAM EFFICIENCY computer science.pdf
 
Python na Infraestrutura 
MySQL do Facebook

Python na Infraestrutura 
MySQL do Facebook
Python na Infraestrutura 
MySQL do Facebook

Python na Infraestrutura 
MySQL do Facebook

 
Python Basis Tutorial
Python Basis TutorialPython Basis Tutorial
Python Basis Tutorial
 
Rcpp: Seemless R and C++
Rcpp: Seemless R and C++Rcpp: Seemless R and C++
Rcpp: Seemless R and C++
 
functions
functionsfunctions
functions
 

Recently uploaded

Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 

Recently uploaded (20)

Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 

PYTHON MODULE: Everything You Need to Know

  • 1. PYTHON MODULE Group of functions, classes, variables VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 2. What is Python Module  A Module is a file containing Python definitions (docstrings) , functions, variables, classes and statements.  Act of partitioning a program into individual components(modules) is called modularity. A module is a separate unit in itself.  It reduces its complexity to some degree  It creates numbers of well-defined, documented boundaries within program.  Its contents can be reused in other program, without having to rewrite or recreate them. VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 3. Structure of Python module  A python module is simply a normal python file(.py) and contains functions, constants and other elements.  Python module may contains following objects: docstring Triple quoted comments. Useful for documentation purpose Variables and constants For storing values Classes To create blueprint of any object Objects Object is an instance of class. It represent class in real world Statements Instruction Functions Group of statements VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 4. Composition/Structure of python module MODULES VARIABLES FUNCTIONS VARIABLES CLASSES MEMBERS METHODS OTHER PYTHON MODULES OTHER PYTHON MODULES IMPORT VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 5. Importing Python modules  To import entire module  import <module name>  Example: import math  To import specific function/object from module:  from <module_name> import <function_name>  Example: from math import sqrt  import * : can be used to import all names from module into current calling module VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 6. Accessing function/constant of imported module  To use function/constant/variable of imported module we have to specify module name and function name separated by dot(.). This format is known as dot notation.  <module_name>.<function_name>  Example: print(math.sqrt(25))  How ever if only particular function is imported using from then module name before function name is not required. We will se examples with next slides. VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 7. Types of Modules  There are various in-built module in python, we will discuss few of them  Math module  Random module  Statistical module VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 8. Math module  This module provides various function to perform arithmetic operations.  Example of functions in math modules are:  Example of variables in math modules are:  pi  e sqrt ceil floor pow fabs sin cos tan VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 9. Math module functions  sqrt(x) : this function returns the square root of number(x).  pow(x,y) : this function returns the (x)y  ceil (x) : this function return the x rounded to next integer. module name is required before function name here module name is not required before function name here VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 10. Math module functions  floor(x) : this function returns the x rounded to previous integer.  fabs(x) : this function returns absolute value of float x. absolute value means number without any sign  sin (x) : it return sine of x (measured in radian) VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 11. Math module functions  cos(x) : it return cosine of x (measured in radian)  tan(x) : it return tangent of x (measured in radian)  pi : return the constant value of pi (22/7)  e : return the constant value of constant e VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 12. Using Random Module  Python has a module namely random that provides random – number generators. Random number means any number generated within the given range.  To generate random number in Python we have to import random module  2 most common method to generate random number in python are :  random() function  randint(a,b) function VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 13. random() function  It is floating point random number generator between 0.0 to 1.0. here lower limit is inclusive where as upper limit is less than 1.0.  0<=N<1  Examples: Output is less than 1 VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 14. random() function  To generate random number between given range of values using random(), the following format should be used:  Lower_range + random() * (upper_range-lower_range)  For example to generate number between 10 to 50:  10 + random() * (40) VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 15. randint() function  Another way to generate random number is randint() function, but it generate integer numbers.  Both the given range values are inclusive i.e. if we generate random number as :  randint(20,70)  In above example random number between 20 to 70 will be taken. (including 20 and 70 also) VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 16. VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR E X A M P L E for more updates visit: www.python4csip.com
  • 17. VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR O U T P U T for more updates visit: www.python4csip.com
  • 18. Just a Minute…  Give the following python code, which is repeated four times. What could be the possible set of output(s) out of four sets (ddd is any combination of digits) import random print(15 + random.random()*5) a) b) c) d) 17.ddd 19.ddd 20.ddd 15.ddd 15.ddd 17.ddd 19.ddd 18.ddd 14.ddd 16.ddd 18.ddd 20.ddd 15.ddd 15.ddd 15.ddd 15.ddd VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 19. Just a Minute…  What could be the minimum possible and maximum possible numbers by following code import random print(random.randint(3,10)-3)  In a school fest, three randomly chosen students out of 100 students (having roll number 1 -100) have to present the bouquet to the guests. Help the school authorities choose three students randomly VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 20. Just a Minute… VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 21. Just a Minute… VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR Look at the following Python code and find the possible output(s) from the options (i) to (iv) following it. Also, write the maximum and the minimum values that can be assigned to the variable PICKER. Note: ‐ Assume all the required header files are already being included in the code. ‐ The function randint() generates an integer between 1 to n import random PICKER=1+random.randint(0,2) COLOR=[”BLUE”,”PINK”,”GREEN”,”RED”] for I in range(1,PICKER+1): for j in range(I+1): print(COLOR[j],end=‘’) print() for more updates visit: www.python4csip.com
  • 22. What are the possible outcome(s) executed from the following code? Also specify the maximum and minimum values that can be assigned to variable PICK 1) DELHIDELHI MUMBAIMUMBAI CHENNAICHENNAI KOLKATAKOLKATA 2) DELHI DELHIMUMBAI DELHIMUMBAICHENNAI 3) DELHI MUMBAI CHENNAI KOKLATA 4) DELHI DELHIMUMBAI KOLKATAKOLKATAKOLKATA VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 23. randrange() function  This function is also used to generate random number within given range.  Syntax  randrange(start,stop,step) random output between 5 to 14, may vary It will generate random number between 5 to 14 VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 24. randrange() function It will generate random number between 1 to 29 with stepping of 2 i.e. it will generate number with gap of 2 i.e. 1,3,5,7 and so on VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 25. Mathematics Game for Kids VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 26. Mathematics Game for Kids VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 27. Statistical Module  This module provides functions for calculating mathematical statistics of numeric (Real-valued) data.  We will deal with 3 basic function under this module  Mean  Median  mode VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 28. Mean  The mean is the average of all numbers and is sometimes called the arithmetic mean. 55, is the average of all numbers in the list VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 29. Median  The median is the middle number in a group of numbers. With odd number of elements it will simply return the middle position value With even number of elements, it will return the average of value at mid + mid-1 i.e. (50+60)/2 = 55.0 VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 30. Mode  The mode is the number that occurs most often within a set of numbers i.e. most common data in list. Here, 10 occurs most in the list. VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com