SlideShare a Scribd company logo
Functions_in_Python.pdf text CBSE class 12
Functions_in_Python.pdf text CBSE class 12
 A function is a named sequence of statement(s) that
performs a computation. It contains
 line of code(s) that are executed sequentially from top
to bottom by Python interpreter.
 They are the most important building blocks for any
software in Python.
Functions can be categorized as -
i. Modules
ii. Built in
iii. User Defined
A module is a file containing Python definitions
(i.e. functions) and statements.
Standard library of Python is extended as
module(s) to a programmer. Definitions from the
module can be used within the code of a
program. To use these modules in the program, a
programmer needs to import the module.
There are many ways to import a module in
your program, the one's which you should
know are:
Import
From
Import
 It is simplest and most common way to use modules in our code.
 Its syntax is:
 import modulename1 [,modulename2, ---------]
 Example
 >>> import math
 To use/ access/invoke a function, you will specify the module
name and name of the
 function- separated by dot (.). This format is also known as dot
notation.
 Example
 >>> value= math.sqrt (25) # dot notation
From Statement
 It is used to get a specific function in the code instead of the
complete module file. If we know beforehand which function(s),
we will be needing, then we may use from. For modules having
large no. of functions, it is recommended to use from instead of
import.
Its syntax is:
>>> from modulename import functionname [, functionname…..]
>>>from modulename import * ( Import everything from the file)
Example
 >>> from math import sqrt
 value = sqrt (25)
Functions_in_Python.pdf text CBSE class 12
Functions_in_Python.pdf text CBSE class 12
 Python modules are .py files that consist of Python code.
Any Python file can be referenced as a module.
 Some modules are available through the Python Standard
Library and are therefore installed with your Python
installation. Others can be installed with Python’s
package manager pip. Additionally, you can create your
own Python modules since modules are comprised of
Python .py files.
 Writing a module is just like writing any other Python file.
Modules can contain definitions of functions, classes, and
variables that can then be utilized in other Python programs.
To begin, we’ll create a function that prints Hello, World!:
hello.py
# Define a function
def world( ):
print("Hello, World!")
If we run the program on the command line with python hello.py
nothing will happen since we have not told the program to do
anything.
 Let’s create a second file in the same directory called
main_program.py so that we can import the module we just
created, and then call the function. This file needs to be in
the same directory so that Python knows where to find the
module since it’s not a built-in module.
main_program.py
# Import hello module
import hello
# Call function
hello.world()
# or from hello import world
Functions_in_Python.pdf text CBSE class 12
Functions_in_Python.pdf text CBSE class 12
 To append the path of a module to another programming file, you’ll start by importing
the sys module alongside any other modules you wish to use in your main program file.
 The sys module is part of the Python Standard Library and provides system-specific
parameters and functions that you can use in your program to set the path of the
module you wish to implement.
 For example, let’s say we moved the hello.py file and it is now on the path
/usr/sammy/ while the main_program.py file is in another directory.
 In our main_program.py file, we can still import the hello module by importing the sys
module and then appending /usr/sammy/ to the path that Python checks for files.
main_program.py
 import sys
 sys.path.append('/user/sammy/')
 import hello
...
 As long as you correctly set the path for the hello.py
file, you’ll be able to run the main_program.py file
without any errors and receive the same output as above
when hello.py was in the same directory.
Built in Function
Built in functions are the function(s) that
are built into Python and can be accessed
by a programmer.
These are always available and for using
them, we don’t have to import any
module (file).
Functions_in_Python.pdf text CBSE class 12
Functions_in_Python.pdf text CBSE class 12
Functions_in_Python.pdf text CBSE class 12
To define a function keyword def is used
After the keyword comes an identifier i.e. name
of the function, followed by parenthesized list of
parameters and the colon which ends up the line.
Next follows the block of statement(s) that are
the part of function.
def sayHello ( ): # Header
print “Hello World!”
Example-
def area (radius):
a = 3.14*radius**2
returna
Functioncall
>>>printarea (5)
The part of the program where a variable can be used is
known as Scope of variable
Two types of scopes :
● Global Scope
● Local Scope
● With global scope, variable can be used
anywhere in the program
eg:
x=50
def test ( ):
print(“inside test x is “, x)
print(“value of x is “, x)
Output:
inside test x is 50
value of x is 50
● With local scope, variable can be used only within the
function / block that it is created .
Eg:
X=50
def test ( ):
y = 20
print(‘value of x is ’, X, ‘ y is ’ , y)
print(‘value of x is ’, X, ‘ y is ‘ , y)
On executing the code we will get
Value of x is 50 y is 20
The next print statement will produce an error, because the variable y is
not accessible outside the def()
To access global variable inside the function prefix
keyword global with the variable
Eg:
x=50
def test ( ):
global x =5
y =2
print(‘value of x & y inside the function are ‘ , x , y)
Print(‘value of x outside function is ‘ ‘, )
This code will produce following output:
Value of x & y inside the function are 5 2
Value of x outside the function is 5
A default argument is a function parameter that has a default
value provided to it. If the user does not supply a value for this
parameter, the default value will be used. If the user does supply a
value for the default parameter, the user-supplied value is used.
Eg.
def greet (message, times=1):
print message * times
>>> greet (‘Welcome’) # function call with one argument value
>>> greet (‘Hello’, 2) # function call with both the argument values.
Output:
Welcome
HelloHello
 What is the difference between methods, functions & user
defined functions.
 Open help for math module
i. How many functions are there in the module?
ii. Describe how square root of a value may be calculated
without using a math module
iii. What are the two data constants available in math module.
 Create a python module to find the sum and product of digits (separately) and
imports in another program.
 Create a python function to find the a year is leap year of not a leap year
 What is local and global variable? Is global is keyword in python?
 Create a python module to find pow(x,n) and import in another program
 Write a function roll_D ( ), that takes 2 parameters- the no. of sides (with
default
value 6) of a dice, and the number of dice to roll-and generate random roll values
for each dice rolled. Print out each roll and then return one string “That’s all”.
Example roll_D (6, 3)
 4
 1
 6
Functions_in_Python.pdf text CBSE class 12
Ad

More Related Content

Similar to Functions_in_Python.pdf text CBSE class 12 (20)

Python modules
Python modulesPython modules
Python modules
Smt. Indira Gandhi College of Engineering, Navi Mumbai, Mumbai
 
Python module 3, b.tech 5th semester ppt
Python module 3, b.tech 5th semester pptPython module 3, b.tech 5th semester ppt
Python module 3, b.tech 5th semester ppt
course5325
 
Unit 2function in python.pptx
Unit 2function in python.pptxUnit 2function in python.pptx
Unit 2function in python.pptx
vishnupriyapm4
 
Using Python Libraries.pdf
Using Python Libraries.pdfUsing Python Libraries.pdf
Using Python Libraries.pdf
SoumyadityaDey
 
Modules in Python.docx
Modules in Python.docxModules in Python.docx
Modules in Python.docx
manohar25689
 
Introduction to Python External Course !!!
Introduction to Python External Course !!!Introduction to Python External Course !!!
Introduction to Python External Course !!!
SlrcMalgn
 
cbse class 12 Python Functions2 for class 12 .pptx
cbse class 12 Python Functions2 for class 12 .pptxcbse class 12 Python Functions2 for class 12 .pptx
cbse class 12 Python Functions2 for class 12 .pptx
tcsonline1222
 
Mastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_argumentsMastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_arguments
Ruth Marvin
 
Functions in python
Functions in pythonFunctions in python
Functions in python
colorsof
 
Functions and Modules.pptx
Functions and Modules.pptxFunctions and Modules.pptx
Functions and Modules.pptx
Ashwini Raut
 
7-_Modules__Packagesyyyyyyyyyyyyyyyyyyyyyyyyyyyyy.pptx
7-_Modules__Packagesyyyyyyyyyyyyyyyyyyyyyyyyyyyyy.pptx7-_Modules__Packagesyyyyyyyyyyyyyyyyyyyyyyyyyyyyy.pptx
7-_Modules__Packagesyyyyyyyyyyyyyyyyyyyyyyyyyyyyy.pptx
RoshanJoshuaR
 
Functions2.pdf
Functions2.pdfFunctions2.pdf
Functions2.pdf
prasnt1
 
pythontraining-201jn026043638.pptx
pythontraining-201jn026043638.pptxpythontraining-201jn026043638.pptx
pythontraining-201jn026043638.pptx
RohitKumar639388
 
VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1
YOGESH SINGH
 
CHAPTER 01 FUNCTION in python class 12th.pptx
CHAPTER 01 FUNCTION in python class 12th.pptxCHAPTER 01 FUNCTION in python class 12th.pptx
CHAPTER 01 FUNCTION in python class 12th.pptx
PreeTVithule1
 
Lecture_5_-_Functions_in_C_Detailed.pptx
Lecture_5_-_Functions_in_C_Detailed.pptxLecture_5_-_Functions_in_C_Detailed.pptx
Lecture_5_-_Functions_in_C_Detailed.pptx
Salim Shadman Ankur
 
Using python libraries.pptx , easy ppt to study class 12
Using python libraries.pptx , easy ppt to study class 12Using python libraries.pptx , easy ppt to study class 12
Using python libraries.pptx , easy ppt to study class 12
anikedheikhamsingh
 
Bcsl 031 solve assignment
Bcsl 031 solve assignmentBcsl 031 solve assignment
Bcsl 031 solve assignment
Indira Gnadhi National Open University (IGNOU)
 
Chapter - 4.pptx
Chapter - 4.pptxChapter - 4.pptx
Chapter - 4.pptx
MikialeTesfamariam
 
function.pptx
function.pptxfunction.pptx
function.pptx
SwapnaliGawali5
 
Python module 3, b.tech 5th semester ppt
Python module 3, b.tech 5th semester pptPython module 3, b.tech 5th semester ppt
Python module 3, b.tech 5th semester ppt
course5325
 
Unit 2function in python.pptx
Unit 2function in python.pptxUnit 2function in python.pptx
Unit 2function in python.pptx
vishnupriyapm4
 
Using Python Libraries.pdf
Using Python Libraries.pdfUsing Python Libraries.pdf
Using Python Libraries.pdf
SoumyadityaDey
 
Modules in Python.docx
Modules in Python.docxModules in Python.docx
Modules in Python.docx
manohar25689
 
Introduction to Python External Course !!!
Introduction to Python External Course !!!Introduction to Python External Course !!!
Introduction to Python External Course !!!
SlrcMalgn
 
cbse class 12 Python Functions2 for class 12 .pptx
cbse class 12 Python Functions2 for class 12 .pptxcbse class 12 Python Functions2 for class 12 .pptx
cbse class 12 Python Functions2 for class 12 .pptx
tcsonline1222
 
Mastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_argumentsMastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_arguments
Ruth Marvin
 
Functions in python
Functions in pythonFunctions in python
Functions in python
colorsof
 
Functions and Modules.pptx
Functions and Modules.pptxFunctions and Modules.pptx
Functions and Modules.pptx
Ashwini Raut
 
7-_Modules__Packagesyyyyyyyyyyyyyyyyyyyyyyyyyyyyy.pptx
7-_Modules__Packagesyyyyyyyyyyyyyyyyyyyyyyyyyyyyy.pptx7-_Modules__Packagesyyyyyyyyyyyyyyyyyyyyyyyyyyyyy.pptx
7-_Modules__Packagesyyyyyyyyyyyyyyyyyyyyyyyyyyyyy.pptx
RoshanJoshuaR
 
Functions2.pdf
Functions2.pdfFunctions2.pdf
Functions2.pdf
prasnt1
 
pythontraining-201jn026043638.pptx
pythontraining-201jn026043638.pptxpythontraining-201jn026043638.pptx
pythontraining-201jn026043638.pptx
RohitKumar639388
 
VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1
YOGESH SINGH
 
CHAPTER 01 FUNCTION in python class 12th.pptx
CHAPTER 01 FUNCTION in python class 12th.pptxCHAPTER 01 FUNCTION in python class 12th.pptx
CHAPTER 01 FUNCTION in python class 12th.pptx
PreeTVithule1
 
Lecture_5_-_Functions_in_C_Detailed.pptx
Lecture_5_-_Functions_in_C_Detailed.pptxLecture_5_-_Functions_in_C_Detailed.pptx
Lecture_5_-_Functions_in_C_Detailed.pptx
Salim Shadman Ankur
 
Using python libraries.pptx , easy ppt to study class 12
Using python libraries.pptx , easy ppt to study class 12Using python libraries.pptx , easy ppt to study class 12
Using python libraries.pptx , easy ppt to study class 12
anikedheikhamsingh
 

Recently uploaded (20)

ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdfML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
rameshwarchintamani
 
Dynamics of Structures with Uncertain Properties.pptx
Dynamics of Structures with Uncertain Properties.pptxDynamics of Structures with Uncertain Properties.pptx
Dynamics of Structures with Uncertain Properties.pptx
University of Glasgow
 
JRR Tolkien’s Lord of the Rings: Was It Influenced by Nordic Mythology, Homer...
JRR Tolkien’s Lord of the Rings: Was It Influenced by Nordic Mythology, Homer...JRR Tolkien’s Lord of the Rings: Was It Influenced by Nordic Mythology, Homer...
JRR Tolkien’s Lord of the Rings: Was It Influenced by Nordic Mythology, Homer...
Reflections on Morality, Philosophy, and History
 
Efficient Algorithms for Isogeny Computation on Hyperelliptic Curves: Their A...
Efficient Algorithms for Isogeny Computation on Hyperelliptic Curves: Their A...Efficient Algorithms for Isogeny Computation on Hyperelliptic Curves: Their A...
Efficient Algorithms for Isogeny Computation on Hyperelliptic Curves: Their A...
IJCNCJournal
 
Prediction of Flexural Strength of Concrete Produced by Using Pozzolanic Mate...
Prediction of Flexural Strength of Concrete Produced by Using Pozzolanic Mate...Prediction of Flexural Strength of Concrete Produced by Using Pozzolanic Mate...
Prediction of Flexural Strength of Concrete Produced by Using Pozzolanic Mate...
Journal of Soft Computing in Civil Engineering
 
Artificial intelligence and machine learning.pptx
Artificial intelligence and machine learning.pptxArtificial intelligence and machine learning.pptx
Artificial intelligence and machine learning.pptx
rakshanatarajan005
 
Surveying through global positioning system
Surveying through global positioning systemSurveying through global positioning system
Surveying through global positioning system
opneptune5
 
Understanding Structural Loads and Load Paths
Understanding Structural Loads and Load PathsUnderstanding Structural Loads and Load Paths
Understanding Structural Loads and Load Paths
University of Kirkuk
 
Water Industry Process Automation & Control Monthly May 2025
Water Industry Process Automation & Control Monthly May 2025Water Industry Process Automation & Control Monthly May 2025
Water Industry Process Automation & Control Monthly May 2025
Water Industry Process Automation & Control
 
Control Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptxControl Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptx
vvsasane
 
ATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ATAL 6 Days Online FDP Scheme Document 2025-26.pdfATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ssuserda39791
 
Slide share PPT of SOx control technologies.pptx
Slide share PPT of SOx control technologies.pptxSlide share PPT of SOx control technologies.pptx
Slide share PPT of SOx control technologies.pptx
vvsasane
 
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software ApplicationsJacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia
 
最新版加拿大魁北克大学蒙特利尔分校毕业证(UQAM毕业证书)原版定制
最新版加拿大魁北克大学蒙特利尔分校毕业证(UQAM毕业证书)原版定制最新版加拿大魁北克大学蒙特利尔分校毕业证(UQAM毕业证书)原版定制
最新版加拿大魁北克大学蒙特利尔分校毕业证(UQAM毕业证书)原版定制
Taqyea
 
Frontend Architecture Diagram/Guide For Frontend Engineers
Frontend Architecture Diagram/Guide For Frontend EngineersFrontend Architecture Diagram/Guide For Frontend Engineers
Frontend Architecture Diagram/Guide For Frontend Engineers
Michael Hertzberg
 
Slide share PPT of NOx control technologies.pptx
Slide share PPT of  NOx control technologies.pptxSlide share PPT of  NOx control technologies.pptx
Slide share PPT of NOx control technologies.pptx
vvsasane
 
A Survey of Personalized Large Language Models.pptx
A Survey of Personalized Large Language Models.pptxA Survey of Personalized Large Language Models.pptx
A Survey of Personalized Large Language Models.pptx
rutujabhaskarraopati
 
Lecture - 7 Canals of the topic of the civil engineering
Lecture - 7  Canals of the topic of the civil engineeringLecture - 7  Canals of the topic of the civil engineering
Lecture - 7 Canals of the topic of the civil engineering
MJawadkhan1
 
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
ijflsjournal087
 
Working with USDOT UTCs: From Conception to Implementation
Working with USDOT UTCs: From Conception to ImplementationWorking with USDOT UTCs: From Conception to Implementation
Working with USDOT UTCs: From Conception to Implementation
Alabama Transportation Assistance Program
 
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdfML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
rameshwarchintamani
 
Dynamics of Structures with Uncertain Properties.pptx
Dynamics of Structures with Uncertain Properties.pptxDynamics of Structures with Uncertain Properties.pptx
Dynamics of Structures with Uncertain Properties.pptx
University of Glasgow
 
Efficient Algorithms for Isogeny Computation on Hyperelliptic Curves: Their A...
Efficient Algorithms for Isogeny Computation on Hyperelliptic Curves: Their A...Efficient Algorithms for Isogeny Computation on Hyperelliptic Curves: Their A...
Efficient Algorithms for Isogeny Computation on Hyperelliptic Curves: Their A...
IJCNCJournal
 
Artificial intelligence and machine learning.pptx
Artificial intelligence and machine learning.pptxArtificial intelligence and machine learning.pptx
Artificial intelligence and machine learning.pptx
rakshanatarajan005
 
Surveying through global positioning system
Surveying through global positioning systemSurveying through global positioning system
Surveying through global positioning system
opneptune5
 
Understanding Structural Loads and Load Paths
Understanding Structural Loads and Load PathsUnderstanding Structural Loads and Load Paths
Understanding Structural Loads and Load Paths
University of Kirkuk
 
Control Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptxControl Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptx
vvsasane
 
ATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ATAL 6 Days Online FDP Scheme Document 2025-26.pdfATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ssuserda39791
 
Slide share PPT of SOx control technologies.pptx
Slide share PPT of SOx control technologies.pptxSlide share PPT of SOx control technologies.pptx
Slide share PPT of SOx control technologies.pptx
vvsasane
 
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software ApplicationsJacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia
 
最新版加拿大魁北克大学蒙特利尔分校毕业证(UQAM毕业证书)原版定制
最新版加拿大魁北克大学蒙特利尔分校毕业证(UQAM毕业证书)原版定制最新版加拿大魁北克大学蒙特利尔分校毕业证(UQAM毕业证书)原版定制
最新版加拿大魁北克大学蒙特利尔分校毕业证(UQAM毕业证书)原版定制
Taqyea
 
Frontend Architecture Diagram/Guide For Frontend Engineers
Frontend Architecture Diagram/Guide For Frontend EngineersFrontend Architecture Diagram/Guide For Frontend Engineers
Frontend Architecture Diagram/Guide For Frontend Engineers
Michael Hertzberg
 
Slide share PPT of NOx control technologies.pptx
Slide share PPT of  NOx control technologies.pptxSlide share PPT of  NOx control technologies.pptx
Slide share PPT of NOx control technologies.pptx
vvsasane
 
A Survey of Personalized Large Language Models.pptx
A Survey of Personalized Large Language Models.pptxA Survey of Personalized Large Language Models.pptx
A Survey of Personalized Large Language Models.pptx
rutujabhaskarraopati
 
Lecture - 7 Canals of the topic of the civil engineering
Lecture - 7  Canals of the topic of the civil engineeringLecture - 7  Canals of the topic of the civil engineering
Lecture - 7 Canals of the topic of the civil engineering
MJawadkhan1
 
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
ijflsjournal087
 
Ad

Functions_in_Python.pdf text CBSE class 12

  • 3.  A function is a named sequence of statement(s) that performs a computation. It contains  line of code(s) that are executed sequentially from top to bottom by Python interpreter.  They are the most important building blocks for any software in Python.
  • 4. Functions can be categorized as - i. Modules ii. Built in iii. User Defined
  • 5. A module is a file containing Python definitions (i.e. functions) and statements. Standard library of Python is extended as module(s) to a programmer. Definitions from the module can be used within the code of a program. To use these modules in the program, a programmer needs to import the module.
  • 6. There are many ways to import a module in your program, the one's which you should know are: Import From
  • 7. Import  It is simplest and most common way to use modules in our code.  Its syntax is:  import modulename1 [,modulename2, ---------]  Example  >>> import math  To use/ access/invoke a function, you will specify the module name and name of the  function- separated by dot (.). This format is also known as dot notation.  Example  >>> value= math.sqrt (25) # dot notation
  • 8. From Statement  It is used to get a specific function in the code instead of the complete module file. If we know beforehand which function(s), we will be needing, then we may use from. For modules having large no. of functions, it is recommended to use from instead of import. Its syntax is: >>> from modulename import functionname [, functionname…..] >>>from modulename import * ( Import everything from the file) Example  >>> from math import sqrt  value = sqrt (25)
  • 11.  Python modules are .py files that consist of Python code. Any Python file can be referenced as a module.  Some modules are available through the Python Standard Library and are therefore installed with your Python installation. Others can be installed with Python’s package manager pip. Additionally, you can create your own Python modules since modules are comprised of Python .py files.
  • 12.  Writing a module is just like writing any other Python file. Modules can contain definitions of functions, classes, and variables that can then be utilized in other Python programs. To begin, we’ll create a function that prints Hello, World!: hello.py # Define a function def world( ): print("Hello, World!") If we run the program on the command line with python hello.py nothing will happen since we have not told the program to do anything.
  • 13.  Let’s create a second file in the same directory called main_program.py so that we can import the module we just created, and then call the function. This file needs to be in the same directory so that Python knows where to find the module since it’s not a built-in module. main_program.py # Import hello module import hello # Call function hello.world() # or from hello import world
  • 16.  To append the path of a module to another programming file, you’ll start by importing the sys module alongside any other modules you wish to use in your main program file.  The sys module is part of the Python Standard Library and provides system-specific parameters and functions that you can use in your program to set the path of the module you wish to implement.  For example, let’s say we moved the hello.py file and it is now on the path /usr/sammy/ while the main_program.py file is in another directory.  In our main_program.py file, we can still import the hello module by importing the sys module and then appending /usr/sammy/ to the path that Python checks for files.
  • 17. main_program.py  import sys  sys.path.append('/user/sammy/')  import hello ...  As long as you correctly set the path for the hello.py file, you’ll be able to run the main_program.py file without any errors and receive the same output as above when hello.py was in the same directory.
  • 18. Built in Function Built in functions are the function(s) that are built into Python and can be accessed by a programmer. These are always available and for using them, we don’t have to import any module (file).
  • 22. To define a function keyword def is used After the keyword comes an identifier i.e. name of the function, followed by parenthesized list of parameters and the colon which ends up the line. Next follows the block of statement(s) that are the part of function.
  • 23. def sayHello ( ): # Header print “Hello World!” Example- def area (radius): a = 3.14*radius**2 returna Functioncall >>>printarea (5)
  • 24. The part of the program where a variable can be used is known as Scope of variable Two types of scopes : ● Global Scope ● Local Scope
  • 25. ● With global scope, variable can be used anywhere in the program eg: x=50 def test ( ): print(“inside test x is “, x) print(“value of x is “, x) Output: inside test x is 50 value of x is 50
  • 26. ● With local scope, variable can be used only within the function / block that it is created . Eg: X=50 def test ( ): y = 20 print(‘value of x is ’, X, ‘ y is ’ , y) print(‘value of x is ’, X, ‘ y is ‘ , y) On executing the code we will get Value of x is 50 y is 20 The next print statement will produce an error, because the variable y is not accessible outside the def()
  • 27. To access global variable inside the function prefix keyword global with the variable Eg: x=50 def test ( ): global x =5 y =2 print(‘value of x & y inside the function are ‘ , x , y) Print(‘value of x outside function is ‘ ‘, ) This code will produce following output: Value of x & y inside the function are 5 2 Value of x outside the function is 5
  • 28. A default argument is a function parameter that has a default value provided to it. If the user does not supply a value for this parameter, the default value will be used. If the user does supply a value for the default parameter, the user-supplied value is used. Eg. def greet (message, times=1): print message * times >>> greet (‘Welcome’) # function call with one argument value >>> greet (‘Hello’, 2) # function call with both the argument values. Output: Welcome HelloHello
  • 29.  What is the difference between methods, functions & user defined functions.  Open help for math module i. How many functions are there in the module? ii. Describe how square root of a value may be calculated without using a math module iii. What are the two data constants available in math module.
  • 30.  Create a python module to find the sum and product of digits (separately) and imports in another program.  Create a python function to find the a year is leap year of not a leap year  What is local and global variable? Is global is keyword in python?  Create a python module to find pow(x,n) and import in another program  Write a function roll_D ( ), that takes 2 parameters- the no. of sides (with default value 6) of a dice, and the number of dice to roll-and generate random roll values for each dice rolled. Print out each roll and then return one string “That’s all”. Example roll_D (6, 3)  4  1  6
  翻译: