Functions in Python
Functions in Python
Built-in Functions
•These are the functions which are predefined in python we have to just call them
to use.
•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.
e.g. x=eval(“45+10“)
print(x) # answer will be 55
math Module
•math module contains following
functions–
–ceil(x) returns integer bigger than x or x integer.
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) and
upper value is one(1).
•random (): This generates floating value between 0 and 1. it does not
require any argument.
•We can use them in any part of our program by calling them.
•We use following syntax with def keyword to prepare a user defined
function.
def Function_Name(List_Of_Parameters):
statements
User-defined Functions without argument and without return
User-defined Functions with argument and without return
User-defined Functions with argument and with return value
User-defined Functions with multiple return values
User-defined Functions with multiple return values
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.
Types of Arguments
Python supports following types of arguments-
1.Positional Arguments
2.Default Arguments
3.Keyword 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.
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.
Passing ARRAYS /LISTS to Function
•In python we use list as array. Well we have to import numpy
module to use array in python.
Scope of Variable
•Scope of variable means the part of program where the variable will
be visible. It means where we can use this variable.
•We can say that scope is the collection of variables and their values.
•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.
•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.
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.