SlideShare a Scribd company logo
Functions
Class-12
Functions in Python
• Function is a collection of statements which is made
to perform a specific task.
• To Execute function we have to call it in the
program.
• Instead of writing a large program we can write
small functions as a specific part of program to
accomplish the task.
• Once written a function can also be used in other
programs as library functions.
• Functions can be categorized in three types-
1. Built-in
2. Modules
3. User Defined
Built-in Functions
• These are the functions which are predefined
in python we have to just call them to use.
• Functions make any programming language
efficient and provide a structure to language.
• Python has many built-in functions which
makes programming easy, fast and efficient.
• They always reside in standard library and we
need not to import any module to use them.
• We will study about some built-in function in
next slide.
Built-in Functions. . .
1. Type Conversion Functions: These are the
functions which converts the values from one type to another-
1. int( ) – To convert the string into integer.
2. str( ) – To covert any value into string.
3. float( ) – To covert string into float.
2. Input Functions: This function is used to take input from
user in the form of string.
e.g. name=input(“Enter your name : “)
3. eval function: This function is used to evaluate the value
of a string.
e.g. x=eval(“45+10“)
print(x) # answer will be 55
Built-in Functions. . .
3. min Function: This
function returns the smallest
value among given list of
values.
4. max Function: This
function returns the biggest
value among given list of
values.
5. abs Function: This
function returns the absolute
value of any integer which is
always positive.
Built-in Functions. . .
6. type Function: This
function is used to identify the
type of any value or variable.
7. len Function: This
function returns the length of
given string.
8. round Function: This
function returns the rounded
number of given number up to
given position.
Built-in Functions. . .
9. range Function: If you
want the series between two
numbers then you can use this
function. This is good tool for
FOR Loop. Its syntax is -
range( start, stop, step)
This gives the series from
START to STOP-1 and the
interval between two numbers
of series will be STEP.
Python Modules
• Module is a .py file which contains the
definitions of functions and variables.
• Module is a simple python file.
• When we divide a program into modules then
each module contains functions and variables.
And each functions is made for a special task.
• Once written code in modules can be used in
other programs.
• When we make such functions which may be
used in other programs also then we write
them in module.
• We can import those module in any program
an we can use the functions.
Python Modules. . .
• Python provides two ways to import a
module -
• import statement: to import full module.
• from: To import all or selected functions from the module.
Python Modules. . .
• In last slide’s example first the math.py file
is searched.
• Then a space is created where all the
variables and functions of the math module
may be stored.
• Then statements of modules are executed.
math Module
• math module contains following
functions–
– ceil(x) returns integer bigger than x or x integer.
– floor(x) returns integer smaller than x or x integer.
– pow(x, n) returns xn.
– sqrt(x) returns square root of x.
– log10(x) returns logarithm of x with base-10
– cos(x) returns cosine of x in radians.
– sin(x) returns sine of x in radians.
– tan(x) returns tangent of x in radians.
help Function
• If you forgot that how a library function works then
help( ) is very useful for this situation.
• If this function is used with any module then it
provides the information and details of the given
module and its all the contents.
string Module
• We have already studied about string module in class XI. Here
are some other functions of string module.
– String.capitalize() Converts first character to Capital Letter
– String.find() Returns the Lowest Index of Substring
– String.index() Returns Index of Substring
– String.isalnum() Checks Alphanumeric Character
– String.isalpha() Checks if All Characters are Alphabets
– String.isdigit() Checks Digit Characters
– String.islower() Checks if all Alphabets in a String, are Lowercase
– String.isupper() returns if all characters are uppercase characters
– String.join() Returns a Concatenated String
– String.lower() returns lowercased string
– String.upper()returns uppercased string
– len()Returns Length of an Object
– ord()returns Unicode code point for Unicode character
– reversed()returns reversed iterator of a sequence
– slice()creates a slice object specified by range()
random Module
• When we require such numbers which are not known earlier
e.g. captcha or any type of serial numbers then in this
situation we need random numbers. And here random module
helps us. This contains following functions-
• randrange (): This method always returns any integer between given
lower and upper limit to this method. default lower value is zero (0) an
upper value is one(1).
• random (): This generates floating value between 0 and 1. it does not
require any argument.
random Module. . .
• randint (): This method takes 2 parameters a,b in which first one is lower
and second is upper limit. This may return any number between these two
numbers including both limits. This method is very useful for guessing
applications.
• uniform (): This method return any floating-point number between two
given numbers.
Neha Tyagi, PGT CS, KV No-5,Jaipur
random Module. . .
• choice (): this method is used for random selection from list, tuple or
string.
• shuffle (): this method can shuffle or swap the items of a given list.
Neha Tyagi, PGT CS, KV No-5,Jaipur
User-defined Functions
• These are the functions which are made by
user as per the requirement of the user.
• Function is a kind of collection of
statements which are written for a specific
task.
• We can use them in any part of our
program by calling them.
• defkeyword is used to make user defined
functions.
User-defined Functions. . .
• We use following syntax with def keyword
to prepare a user defined function.
def Function_Name(List_Of_Parameters):
”””docstring”””
statement(s)
Keyword
Function
Definition
After the line containing def
there should be a 4 spaces
indentation, which is also
know as the body or block
of the function.
User-defined Functions without argument
and without return (Practical)
Function
Definition
Function Call
User-defined Functions with argument and
without return (Practical)
In the case of arguments,
the values are passed in the
function’s parenthesis. And
they are declared in
definition as well.
User-defined Functions with argument and
with return value (Practical)
In the case of returning
value, the calculated value is
sent outside the function by
a returning statement. This
returned value will be hold
by a variable used in calling
statement.
User-defined Functions with multiple
return values (Practical)
In python a function
may return multiple
values. In multiple
returning it returns a
sequence of values to
the calling statement.
These returned values
may be used as per the
requirement.
User-defined Functions with multiple
return values (Practical)
Last program may also
be written as follows.
Result will be the tuple
here.
Parameters and Arguments in Functions
• When we write header of any function then the
one or more values given to its parenthesis ( )
are known as parameter
.
• These are the values which are used by the
function for any specific task.
• While argument
is the value passed at the time
of calling a function.
• In other words the arguments are used to
invoke a function.
• Formal parameters are said to be parameters
and actual parameters are said to be
arguments.
These are the parameters
.
These are the arguments
.
Parameters and Arguments in Functions . . .
Types of Arguments
• Python supports 4 types of arguments-
1. Positional Arguments
2. Default Arguments
3. Keyword Arguments
4. Variable Length Arguments
1. Positional Arguments
• These are the arguments which are passed
in correct positional order in function.
• If we change the position of the arguments
then the answer will be changed.
2. Default Arguments
• These are the arguments through which we
can provide default values to the function.
• If we don’t pass any value to the function
then it will take a pre defined value.
This is point to remember
that the default argument
should be given after non
default argument.
3. Keyword Arguments
• If a function have many arguments and we want to change the
sequence of them then we have to use keyword arguments.
• Biggest benefit of keyword argument is that we need not to
remember the position of the argument.
• For this whenever we pass the values to the function then we
pass the values with the argument name. e.g.
If you used one
argument with keyword
then others must also be
called with keywords
otherwise an error will be
raised.
4. Variable Length Arguments
• As we can assume by the name that we can pass any number of
arguments according to the requirement. Such arguments are
known as variable length arguments.
• We use (*) asterik to give Variable length argument.
You can notice here
that every time the
number of arguments
are different and the
answer is calculated
for each number of
arguments.
Passing ARRAYS /LISTS to Function
• In python we use list as array. Well we have to import numpy
module to use array in python.
• We will pass here list to the function. As we know that list is
better than array.
Scope of Variable
• Scope of variable means the part of program wher
the variable will be visible. It means where we can
use this variable.
• We can say that scope is the collection of variable
and their values.
• Scope can of two types -
• Global (module)
– All those names which are assigned at top level in module
or directly assigned in interpreter.
• Local (function)
– Those variables which are assigned within a loop of
function.
Using main() as a Function
• Main function is not necessary in python.
• For the convenience of Non-python programmers, we
can use it as follows-
Flow of Execution at the Time of
Function Call
• The execution of any program starts from the very first line and
this execution goes line by line.
• One statement is executed at a time.
• Function doesn’t change the flow of any program.
• Statements of function doesn’t start executing until it is called.
• When function is called then the control is jumped into the
body of function.
• Then all the statements of the function gets executed from top
to bottom one by one.
• And again the control returns to the place where it was called.
• And in this sequence the python program gets executed.
RECURSION
• In recursion, function calls itself until the condition is
not satisfied.
RECURSION. . .
• Recursion is one of the most powerful tools of the
programming language. When a function calls itself
within its body then this is know as recursion.
• There are two conditions to use recursion -
– There must be a terminating condition.
– There must be an if condition in recursive routine.
– Every recursive problem has a special feature its reversal in
the order of execution.
– As many times there is recursive call then every time a new
memory is allocated to the local variables of recursive
function.
– During recursion each value is pushed in a stack.
Drawbacks of RECURSION
• It uses more storage as each values is pushed in to stack.
•If recursive call is not checked carefully then your computer
may go out of memory.
• It is less efficient in terms of time and speed.
Ad

More Related Content

Similar to Functions-.pdf (20)

Py-Slides-3 difficultpythoncoursefforbeginners.ppt
Py-Slides-3 difficultpythoncoursefforbeginners.pptPy-Slides-3 difficultpythoncoursefforbeginners.ppt
Py-Slides-3 difficultpythoncoursefforbeginners.ppt
mohamedsamydeveloper
 
Learn more about the concepts Functions of Python
Learn more about the concepts Functions of PythonLearn more about the concepts Functions of Python
Learn more about the concepts Functions of Python
PrathamKandari
 
Python programming variables and comment
Python programming variables and commentPython programming variables and comment
Python programming variables and comment
MalligaarjunanN
 
Functions_new.pptx
Functions_new.pptxFunctions_new.pptx
Functions_new.pptx
Yagna15
 
ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3
ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3
ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3
prasadmutkule1
 
UNIT 3 python.pptx
UNIT 3 python.pptxUNIT 3 python.pptx
UNIT 3 python.pptx
TKSanthoshRao
 
Python for Data Science function third module ppt.pptx
Python for Data Science  function third module ppt.pptxPython for Data Science  function third module ppt.pptx
Python for Data Science function third module ppt.pptx
bmit1
 
Functions
FunctionsFunctions
Functions
Gaurav Subham
 
Functions in Python and its types for beginners
Functions in Python and its types for beginnersFunctions in Python and its types for beginners
Functions in Python and its types for beginners
Mohammad Usman
 
Dive into Python Functions Fundamental Concepts.pdf
Dive into Python Functions Fundamental Concepts.pdfDive into Python Functions Fundamental Concepts.pdf
Dive into Python Functions Fundamental Concepts.pdf
SudhanshiBakre1
 
FUNCTION CPU
FUNCTION CPUFUNCTION CPU
FUNCTION CPU
Krushal Kakadia
 
Python_Unit_2.pdf
Python_Unit_2.pdfPython_Unit_2.pdf
Python_Unit_2.pdf
alaparthi
 
Functions Programming in Python Language
Functions Programming in Python LanguageFunctions Programming in Python Language
Functions Programming in Python Language
BalaSubramanian376976
 
functions- best.pdf
functions- best.pdffunctions- best.pdf
functions- best.pdf
MikialeTesfamariam
 
Functions
FunctionsFunctions
Functions
Golda Margret Sheeba J
 
Functions and modular programming.pptx
Functions and modular programming.pptxFunctions and modular programming.pptx
Functions and modular programming.pptx
zueZ3
 
functions modules and exceptions handlings.ppt
functions modules and exceptions handlings.pptfunctions modules and exceptions handlings.ppt
functions modules and exceptions handlings.ppt
Rajasekhar364622
 
_Python_ Functions _and_ Libraries_.pptx
_Python_ Functions _and_ Libraries_.pptx_Python_ Functions _and_ Libraries_.pptx
_Python_ Functions _and_ Libraries_.pptx
yaramahsoob
 
Python Functions.pptx
Python Functions.pptxPython Functions.pptx
Python Functions.pptx
AnuragBharti27
 
Python Functions.pptx
Python Functions.pptxPython Functions.pptx
Python Functions.pptx
AnuragBharti27
 
Py-Slides-3 difficultpythoncoursefforbeginners.ppt
Py-Slides-3 difficultpythoncoursefforbeginners.pptPy-Slides-3 difficultpythoncoursefforbeginners.ppt
Py-Slides-3 difficultpythoncoursefforbeginners.ppt
mohamedsamydeveloper
 
Learn more about the concepts Functions of Python
Learn more about the concepts Functions of PythonLearn more about the concepts Functions of Python
Learn more about the concepts Functions of Python
PrathamKandari
 
Python programming variables and comment
Python programming variables and commentPython programming variables and comment
Python programming variables and comment
MalligaarjunanN
 
Functions_new.pptx
Functions_new.pptxFunctions_new.pptx
Functions_new.pptx
Yagna15
 
ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3
ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3
ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3
prasadmutkule1
 
Python for Data Science function third module ppt.pptx
Python for Data Science  function third module ppt.pptxPython for Data Science  function third module ppt.pptx
Python for Data Science function third module ppt.pptx
bmit1
 
Functions in Python and its types for beginners
Functions in Python and its types for beginnersFunctions in Python and its types for beginners
Functions in Python and its types for beginners
Mohammad Usman
 
Dive into Python Functions Fundamental Concepts.pdf
Dive into Python Functions Fundamental Concepts.pdfDive into Python Functions Fundamental Concepts.pdf
Dive into Python Functions Fundamental Concepts.pdf
SudhanshiBakre1
 
Python_Unit_2.pdf
Python_Unit_2.pdfPython_Unit_2.pdf
Python_Unit_2.pdf
alaparthi
 
Functions Programming in Python Language
Functions Programming in Python LanguageFunctions Programming in Python Language
Functions Programming in Python Language
BalaSubramanian376976
 
Functions and modular programming.pptx
Functions and modular programming.pptxFunctions and modular programming.pptx
Functions and modular programming.pptx
zueZ3
 
functions modules and exceptions handlings.ppt
functions modules and exceptions handlings.pptfunctions modules and exceptions handlings.ppt
functions modules and exceptions handlings.ppt
Rajasekhar364622
 
_Python_ Functions _and_ Libraries_.pptx
_Python_ Functions _and_ Libraries_.pptx_Python_ Functions _and_ Libraries_.pptx
_Python_ Functions _and_ Libraries_.pptx
yaramahsoob
 

Recently uploaded (20)

behiriskfactorsxyzkskeb210217133906 (1).pdf
behiriskfactorsxyzkskeb210217133906 (1).pdfbehiriskfactorsxyzkskeb210217133906 (1).pdf
behiriskfactorsxyzkskeb210217133906 (1).pdf
ShakibulHasan14
 
Download Chimera Tool Setup V42.47.0924 [Latest Version]
Download Chimera Tool Setup V42.47.0924 [Latest Version]Download Chimera Tool Setup V42.47.0924 [Latest Version]
Download Chimera Tool Setup V42.47.0924 [Latest Version]
Designer
 
Minimalist Pitch Deck by slide Slidesgo.pptx
Minimalist Pitch Deck by slide Slidesgo.pptxMinimalist Pitch Deck by slide Slidesgo.pptx
Minimalist Pitch Deck by slide Slidesgo.pptx
ESTEFANOANDREYGARCIA
 
Internet Download Manager Crack Patch Latest IDM Free Download
Internet Download Manager Crack Patch Latest IDM Free DownloadInternet Download Manager Crack Patch Latest IDM Free Download
Internet Download Manager Crack Patch Latest IDM Free Download
Designer
 
𝗗𝗲𝘀𝗶𝗴𝗻𝗶𝗻𝗴 𝗳𝗼𝗿 𝗮𝗹𝗹: 𝗜𝗻𝗰𝗹𝘂𝘀𝗶𝘃𝗲 𝗣𝗿𝗮𝗰𝘁𝗶𝗰𝗲 𝗳𝗼𝗿 𝗕𝗲𝘁𝘁𝗲𝗿 𝗘𝘅𝗽𝗲𝗿𝗶𝗲𝗻𝗰𝗲𝘀
𝗗𝗲𝘀𝗶𝗴𝗻𝗶𝗻𝗴 𝗳𝗼𝗿 𝗮𝗹𝗹: 𝗜𝗻𝗰𝗹𝘂𝘀𝗶𝘃𝗲 𝗣𝗿𝗮𝗰𝘁𝗶𝗰𝗲 𝗳𝗼𝗿 𝗕𝗲𝘁𝘁𝗲𝗿 𝗘𝘅𝗽𝗲𝗿𝗶𝗲𝗻𝗰𝗲𝘀𝗗𝗲𝘀𝗶𝗴𝗻𝗶𝗻𝗴 𝗳𝗼𝗿 𝗮𝗹𝗹: 𝗜𝗻𝗰𝗹𝘂𝘀𝗶𝘃𝗲 𝗣𝗿𝗮𝗰𝘁𝗶𝗰𝗲 𝗳𝗼𝗿 𝗕𝗲𝘁𝘁𝗲𝗿 𝗘𝘅𝗽𝗲𝗿𝗶𝗲𝗻𝗰𝗲𝘀
𝗗𝗲𝘀𝗶𝗴𝗻𝗶𝗻𝗴 𝗳𝗼𝗿 𝗮𝗹𝗹: 𝗜𝗻𝗰𝗹𝘂𝘀𝗶𝘃𝗲 𝗣𝗿𝗮𝗰𝘁𝗶𝗰𝗲 𝗳𝗼𝗿 𝗕𝗲𝘁𝘁𝗲𝗿 𝗘𝘅𝗽𝗲𝗿𝗶𝗲𝗻𝗰𝗲𝘀
Friends of Figm a, Sydney
 
Steam-Education-PowerPoint-Templates.pptx
Steam-Education-PowerPoint-Templates.pptxSteam-Education-PowerPoint-Templates.pptx
Steam-Education-PowerPoint-Templates.pptx
andripapa1
 
Doodle Table of Contents Infographics by Slidesgo.pptx
Doodle Table of Contents Infographics by Slidesgo.pptxDoodle Table of Contents Infographics by Slidesgo.pptx
Doodle Table of Contents Infographics by Slidesgo.pptx
binhyennghlu
 
mid-term all revisions g11 s1.pmdzs,zxptx
mid-term all revisions g11 s1.pmdzs,zxptxmid-term all revisions g11 s1.pmdzs,zxptx
mid-term all revisions g11 s1.pmdzs,zxptx
omar164646
 
Lori Vanzant Portfolio. Please take a look !
Lori Vanzant  Portfolio. Please take a look !Lori Vanzant  Portfolio. Please take a look !
Lori Vanzant Portfolio. Please take a look !
vanzan01
 
EEE178-PPT-Theme iasodhajsdkjashdlaskdjbaksdkashdlkasdlkja;dj;kdada.pptx.pdf
EEE178-PPT-Theme iasodhajsdkjashdlaskdjbaksdkashdlkasdlkja;dj;kdada.pptx.pdfEEE178-PPT-Theme iasodhajsdkjashdlaskdjbaksdkashdlkasdlkja;dj;kdada.pptx.pdf
EEE178-PPT-Theme iasodhajsdkjashdlaskdjbaksdkashdlkasdlkja;dj;kdada.pptx.pdf
CastroAngeloReoD
 
Lori Vanzant's portfolio. Please take a look!
Lori Vanzant's portfolio. Please take a look!Lori Vanzant's portfolio. Please take a look!
Lori Vanzant's portfolio. Please take a look!
vanzan01
 
An updated content measurement model - Elle Geraghty Content Strategy.pdf
An updated content measurement model - Elle Geraghty Content Strategy.pdfAn updated content measurement model - Elle Geraghty Content Strategy.pdf
An updated content measurement model - Elle Geraghty Content Strategy.pdf
Elle Geraghty
 
COTTER and KNUCKleeeeeeeeeeeeeeeeeee.pptx
COTTER and  KNUCKleeeeeeeeeeeeeeeeeee.pptxCOTTER and  KNUCKleeeeeeeeeeeeeeeeeee.pptx
COTTER and KNUCKleeeeeeeeeeeeeeeeeee.pptx
ayushjadon04
 
19 Best B,u,y Verified Cash App Accounts
19 Best B,u,y Verified Cash App Accounts19 Best B,u,y Verified Cash App Accounts
19 Best B,u,y Verified Cash App Accounts
https://sellsusa.com/product/buy-verified-cash-app-accounts/
 
Designing Interactive and Engaging Museum Exhibits
Designing Interactive and Engaging Museum ExhibitsDesigning Interactive and Engaging Museum Exhibits
Designing Interactive and Engaging Museum Exhibits
Peach Prime Consultancy
 
2nd taxonomy, nomen microorganisms-.pptx
2nd  taxonomy, nomen  microorganisms-.pptx2nd  taxonomy, nomen  microorganisms-.pptx
2nd taxonomy, nomen microorganisms-.pptx
ayeleasefa2
 
Prof House interior Design Project exter
Prof House interior Design Project exterProf House interior Design Project exter
Prof House interior Design Project exter
NagudiBridget
 
Baby panda 400.pdf de ciencias naturales
Baby panda 400.pdf de ciencias naturalesBaby panda 400.pdf de ciencias naturales
Baby panda 400.pdf de ciencias naturales
debbie loaiza
 
Oversized Off White Pulka Dot Cotton Shirt
Oversized Off White Pulka Dot Cotton ShirtOversized Off White Pulka Dot Cotton Shirt
Oversized Off White Pulka Dot Cotton Shirt
ZNKL.in
 
Lori Vanzant Portfolio. Take a look! ty.
Lori Vanzant Portfolio. Take a look! ty.Lori Vanzant Portfolio. Take a look! ty.
Lori Vanzant Portfolio. Take a look! ty.
vanzan01
 
behiriskfactorsxyzkskeb210217133906 (1).pdf
behiriskfactorsxyzkskeb210217133906 (1).pdfbehiriskfactorsxyzkskeb210217133906 (1).pdf
behiriskfactorsxyzkskeb210217133906 (1).pdf
ShakibulHasan14
 
Download Chimera Tool Setup V42.47.0924 [Latest Version]
Download Chimera Tool Setup V42.47.0924 [Latest Version]Download Chimera Tool Setup V42.47.0924 [Latest Version]
Download Chimera Tool Setup V42.47.0924 [Latest Version]
Designer
 
Minimalist Pitch Deck by slide Slidesgo.pptx
Minimalist Pitch Deck by slide Slidesgo.pptxMinimalist Pitch Deck by slide Slidesgo.pptx
Minimalist Pitch Deck by slide Slidesgo.pptx
ESTEFANOANDREYGARCIA
 
Internet Download Manager Crack Patch Latest IDM Free Download
Internet Download Manager Crack Patch Latest IDM Free DownloadInternet Download Manager Crack Patch Latest IDM Free Download
Internet Download Manager Crack Patch Latest IDM Free Download
Designer
 
𝗗𝗲𝘀𝗶𝗴𝗻𝗶𝗻𝗴 𝗳𝗼𝗿 𝗮𝗹𝗹: 𝗜𝗻𝗰𝗹𝘂𝘀𝗶𝘃𝗲 𝗣𝗿𝗮𝗰𝘁𝗶𝗰𝗲 𝗳𝗼𝗿 𝗕𝗲𝘁𝘁𝗲𝗿 𝗘𝘅𝗽𝗲𝗿𝗶𝗲𝗻𝗰𝗲𝘀
𝗗𝗲𝘀𝗶𝗴𝗻𝗶𝗻𝗴 𝗳𝗼𝗿 𝗮𝗹𝗹: 𝗜𝗻𝗰𝗹𝘂𝘀𝗶𝘃𝗲 𝗣𝗿𝗮𝗰𝘁𝗶𝗰𝗲 𝗳𝗼𝗿 𝗕𝗲𝘁𝘁𝗲𝗿 𝗘𝘅𝗽𝗲𝗿𝗶𝗲𝗻𝗰𝗲𝘀𝗗𝗲𝘀𝗶𝗴𝗻𝗶𝗻𝗴 𝗳𝗼𝗿 𝗮𝗹𝗹: 𝗜𝗻𝗰𝗹𝘂𝘀𝗶𝘃𝗲 𝗣𝗿𝗮𝗰𝘁𝗶𝗰𝗲 𝗳𝗼𝗿 𝗕𝗲𝘁𝘁𝗲𝗿 𝗘𝘅𝗽𝗲𝗿𝗶𝗲𝗻𝗰𝗲𝘀
𝗗𝗲𝘀𝗶𝗴𝗻𝗶𝗻𝗴 𝗳𝗼𝗿 𝗮𝗹𝗹: 𝗜𝗻𝗰𝗹𝘂𝘀𝗶𝘃𝗲 𝗣𝗿𝗮𝗰𝘁𝗶𝗰𝗲 𝗳𝗼𝗿 𝗕𝗲𝘁𝘁𝗲𝗿 𝗘𝘅𝗽𝗲𝗿𝗶𝗲𝗻𝗰𝗲𝘀
Friends of Figm a, Sydney
 
Steam-Education-PowerPoint-Templates.pptx
Steam-Education-PowerPoint-Templates.pptxSteam-Education-PowerPoint-Templates.pptx
Steam-Education-PowerPoint-Templates.pptx
andripapa1
 
Doodle Table of Contents Infographics by Slidesgo.pptx
Doodle Table of Contents Infographics by Slidesgo.pptxDoodle Table of Contents Infographics by Slidesgo.pptx
Doodle Table of Contents Infographics by Slidesgo.pptx
binhyennghlu
 
mid-term all revisions g11 s1.pmdzs,zxptx
mid-term all revisions g11 s1.pmdzs,zxptxmid-term all revisions g11 s1.pmdzs,zxptx
mid-term all revisions g11 s1.pmdzs,zxptx
omar164646
 
Lori Vanzant Portfolio. Please take a look !
Lori Vanzant  Portfolio. Please take a look !Lori Vanzant  Portfolio. Please take a look !
Lori Vanzant Portfolio. Please take a look !
vanzan01
 
EEE178-PPT-Theme iasodhajsdkjashdlaskdjbaksdkashdlkasdlkja;dj;kdada.pptx.pdf
EEE178-PPT-Theme iasodhajsdkjashdlaskdjbaksdkashdlkasdlkja;dj;kdada.pptx.pdfEEE178-PPT-Theme iasodhajsdkjashdlaskdjbaksdkashdlkasdlkja;dj;kdada.pptx.pdf
EEE178-PPT-Theme iasodhajsdkjashdlaskdjbaksdkashdlkasdlkja;dj;kdada.pptx.pdf
CastroAngeloReoD
 
Lori Vanzant's portfolio. Please take a look!
Lori Vanzant's portfolio. Please take a look!Lori Vanzant's portfolio. Please take a look!
Lori Vanzant's portfolio. Please take a look!
vanzan01
 
An updated content measurement model - Elle Geraghty Content Strategy.pdf
An updated content measurement model - Elle Geraghty Content Strategy.pdfAn updated content measurement model - Elle Geraghty Content Strategy.pdf
An updated content measurement model - Elle Geraghty Content Strategy.pdf
Elle Geraghty
 
COTTER and KNUCKleeeeeeeeeeeeeeeeeee.pptx
COTTER and  KNUCKleeeeeeeeeeeeeeeeeee.pptxCOTTER and  KNUCKleeeeeeeeeeeeeeeeeee.pptx
COTTER and KNUCKleeeeeeeeeeeeeeeeeee.pptx
ayushjadon04
 
Designing Interactive and Engaging Museum Exhibits
Designing Interactive and Engaging Museum ExhibitsDesigning Interactive and Engaging Museum Exhibits
Designing Interactive and Engaging Museum Exhibits
Peach Prime Consultancy
 
2nd taxonomy, nomen microorganisms-.pptx
2nd  taxonomy, nomen  microorganisms-.pptx2nd  taxonomy, nomen  microorganisms-.pptx
2nd taxonomy, nomen microorganisms-.pptx
ayeleasefa2
 
Prof House interior Design Project exter
Prof House interior Design Project exterProf House interior Design Project exter
Prof House interior Design Project exter
NagudiBridget
 
Baby panda 400.pdf de ciencias naturales
Baby panda 400.pdf de ciencias naturalesBaby panda 400.pdf de ciencias naturales
Baby panda 400.pdf de ciencias naturales
debbie loaiza
 
Oversized Off White Pulka Dot Cotton Shirt
Oversized Off White Pulka Dot Cotton ShirtOversized Off White Pulka Dot Cotton Shirt
Oversized Off White Pulka Dot Cotton Shirt
ZNKL.in
 
Lori Vanzant Portfolio. Take a look! ty.
Lori Vanzant Portfolio. Take a look! ty.Lori Vanzant Portfolio. Take a look! ty.
Lori Vanzant Portfolio. Take a look! ty.
vanzan01
 
Ad

Functions-.pdf

  • 2. Functions in Python • Function is a collection of statements which is made to perform a specific task. • To Execute function we have to call it in the program. • Instead of writing a large program we can write small functions as a specific part of program to accomplish the task. • Once written a function can also be used in other programs as library functions. • Functions can be categorized in three types- 1. Built-in 2. Modules 3. User Defined
  • 3. Built-in Functions • These are the functions which are predefined in python we have to just call them to use. • Functions make any programming language efficient and provide a structure to language. • Python has many built-in functions which makes programming easy, fast and efficient. • They always reside in standard library and we need not to import any module to use them. • We will study about some built-in function in next slide.
  • 4. Built-in Functions. . . 1. Type Conversion Functions: These are the functions which converts the values from one type to another- 1. int( ) – To convert the string into integer. 2. str( ) – To covert any value into string. 3. float( ) – To covert string into float. 2. Input Functions: This function is used to take input from user in the form of string. e.g. name=input(“Enter your name : “) 3. eval function: This function is used to evaluate the value of a string. e.g. x=eval(“45+10“) print(x) # answer will be 55
  • 5. Built-in Functions. . . 3. min Function: This function returns the smallest value among given list of values. 4. max Function: This function returns the biggest value among given list of values. 5. abs Function: This function returns the absolute value of any integer which is always positive.
  • 6. Built-in Functions. . . 6. type Function: This function is used to identify the type of any value or variable. 7. len Function: This function returns the length of given string. 8. round Function: This function returns the rounded number of given number up to given position.
  • 7. Built-in Functions. . . 9. range Function: If you want the series between two numbers then you can use this function. This is good tool for FOR Loop. Its syntax is - range( start, stop, step) This gives the series from START to STOP-1 and the interval between two numbers of series will be STEP.
  • 8. Python Modules • Module is a .py file which contains the definitions of functions and variables. • Module is a simple python file. • When we divide a program into modules then each module contains functions and variables. And each functions is made for a special task. • Once written code in modules can be used in other programs. • When we make such functions which may be used in other programs also then we write them in module. • We can import those module in any program an we can use the functions.
  • 9. Python Modules. . . • Python provides two ways to import a module - • import statement: to import full module. • from: To import all or selected functions from the module.
  • 10. Python Modules. . . • In last slide’s example first the math.py file is searched. • Then a space is created where all the variables and functions of the math module may be stored. • Then statements of modules are executed.
  • 11. math Module • math module contains following functions– – ceil(x) returns integer bigger than x or x integer. – floor(x) returns integer smaller than x or x integer. – pow(x, n) returns xn. – sqrt(x) returns square root of x. – log10(x) returns logarithm of x with base-10 – cos(x) returns cosine of x in radians. – sin(x) returns sine of x in radians. – tan(x) returns tangent of x in radians.
  • 12. help Function • If you forgot that how a library function works then help( ) is very useful for this situation. • If this function is used with any module then it provides the information and details of the given module and its all the contents.
  • 13. string Module • We have already studied about string module in class XI. Here are some other functions of string module. – String.capitalize() Converts first character to Capital Letter – String.find() Returns the Lowest Index of Substring – String.index() Returns Index of Substring – String.isalnum() Checks Alphanumeric Character – String.isalpha() Checks if All Characters are Alphabets – String.isdigit() Checks Digit Characters – String.islower() Checks if all Alphabets in a String, are Lowercase – String.isupper() returns if all characters are uppercase characters – String.join() Returns a Concatenated String – String.lower() returns lowercased string – String.upper()returns uppercased string – len()Returns Length of an Object – ord()returns Unicode code point for Unicode character – reversed()returns reversed iterator of a sequence – slice()creates a slice object specified by range()
  • 14. random Module • When we require such numbers which are not known earlier e.g. captcha or any type of serial numbers then in this situation we need random numbers. And here random module helps us. This contains following functions- • randrange (): This method always returns any integer between given lower and upper limit to this method. default lower value is zero (0) an upper value is one(1). • random (): This generates floating value between 0 and 1. it does not require any argument.
  • 15. random Module. . . • randint (): This method takes 2 parameters a,b in which first one is lower and second is upper limit. This may return any number between these two numbers including both limits. This method is very useful for guessing applications. • uniform (): This method return any floating-point number between two given numbers. Neha Tyagi, PGT CS, KV No-5,Jaipur
  • 16. random Module. . . • choice (): this method is used for random selection from list, tuple or string. • shuffle (): this method can shuffle or swap the items of a given list. Neha Tyagi, PGT CS, KV No-5,Jaipur
  • 17. User-defined Functions • These are the functions which are made by user as per the requirement of the user. • Function is a kind of collection of statements which are written for a specific task. • We can use them in any part of our program by calling them. • defkeyword is used to make user defined functions.
  • 18. User-defined Functions. . . • We use following syntax with def keyword to prepare a user defined function. def Function_Name(List_Of_Parameters): ”””docstring””” statement(s) Keyword Function Definition After the line containing def there should be a 4 spaces indentation, which is also know as the body or block of the function.
  • 19. User-defined Functions without argument and without return (Practical) Function Definition Function Call
  • 20. User-defined Functions with argument and without return (Practical) In the case of arguments, the values are passed in the function’s parenthesis. And they are declared in definition as well.
  • 21. User-defined Functions with argument and with return value (Practical) In the case of returning value, the calculated value is sent outside the function by a returning statement. This returned value will be hold by a variable used in calling statement.
  • 22. User-defined Functions with multiple return values (Practical) In python a function may return multiple values. In multiple returning it returns a sequence of values to the calling statement. These returned values may be used as per the requirement.
  • 23. User-defined Functions with multiple return values (Practical) Last program may also be written as follows. Result will be the tuple here.
  • 24. Parameters and Arguments in Functions • When we write header of any function then the one or more values given to its parenthesis ( ) are known as parameter . • These are the values which are used by the function for any specific task. • While argument is the value passed at the time of calling a function. • In other words the arguments are used to invoke a function. • Formal parameters are said to be parameters and actual parameters are said to be arguments.
  • 25. These are the parameters . These are the arguments . Parameters and Arguments in Functions . . .
  • 26. Types of Arguments • Python supports 4 types of arguments- 1. Positional Arguments 2. Default Arguments 3. Keyword Arguments 4. Variable Length Arguments
  • 27. 1. Positional Arguments • These are the arguments which are passed in correct positional order in function. • If we change the position of the arguments then the answer will be changed.
  • 28. 2. Default Arguments • These are the arguments through which we can provide default values to the function. • If we don’t pass any value to the function then it will take a pre defined value. This is point to remember that the default argument should be given after non default argument.
  • 29. 3. Keyword Arguments • If a function have many arguments and we want to change the sequence of them then we have to use keyword arguments. • Biggest benefit of keyword argument is that we need not to remember the position of the argument. • For this whenever we pass the values to the function then we pass the values with the argument name. e.g. If you used one argument with keyword then others must also be called with keywords otherwise an error will be raised.
  • 30. 4. Variable Length Arguments • As we can assume by the name that we can pass any number of arguments according to the requirement. Such arguments are known as variable length arguments. • We use (*) asterik to give Variable length argument. You can notice here that every time the number of arguments are different and the answer is calculated for each number of arguments.
  • 31. Passing ARRAYS /LISTS to Function • In python we use list as array. Well we have to import numpy module to use array in python. • We will pass here list to the function. As we know that list is better than array.
  • 32. Scope of Variable • Scope of variable means the part of program wher the variable will be visible. It means where we can use this variable. • We can say that scope is the collection of variable and their values. • Scope can of two types - • Global (module) – All those names which are assigned at top level in module or directly assigned in interpreter. • Local (function) – Those variables which are assigned within a loop of function.
  • 33. Using main() as a Function • Main function is not necessary in python. • For the convenience of Non-python programmers, we can use it as follows-
  • 34. Flow of Execution at the Time of Function Call • The execution of any program starts from the very first line and this execution goes line by line. • One statement is executed at a time. • Function doesn’t change the flow of any program. • Statements of function doesn’t start executing until it is called. • When function is called then the control is jumped into the body of function. • Then all the statements of the function gets executed from top to bottom one by one. • And again the control returns to the place where it was called. • And in this sequence the python program gets executed.
  • 35. RECURSION • In recursion, function calls itself until the condition is not satisfied.
  • 36. RECURSION. . . • Recursion is one of the most powerful tools of the programming language. When a function calls itself within its body then this is know as recursion. • There are two conditions to use recursion - – There must be a terminating condition. – There must be an if condition in recursive routine. – Every recursive problem has a special feature its reversal in the order of execution. – As many times there is recursive call then every time a new memory is allocated to the local variables of recursive function. – During recursion each value is pushed in a stack.
  • 37. Drawbacks of RECURSION • It uses more storage as each values is pushed in to stack. •If recursive call is not checked carefully then your computer may go out of memory. • It is less efficient in terms of time and speed.