Built in Functions
Built in Functions
A function typically performs a specific task that you can run in your program.
Defining a Function
You can define functions to provide the required functionality. Here are simple rules to
define a function in Python.
Function blocks begin with the keyword def followed by the function name and
parentheses ().
Any input parameters or arguments should be placed within these parentheses. You
can also define parameters inside these parentheses.
The first statement of a function can be an optional statement - the documentation
string of the function or docstring.
The code block within every function starts with a colon (:) and is indented.
The statement return [expression] exits a function, optionally passing back an
expression to the caller. A return statement with no arguments is the same as return
None.
Syntax:
def functionname( parameters ):
"function_docstring"
function_suite
return [expression]
By default, parameters have a positional behavior and you need to inform them in the same
order that they were defined.
Example:
Result:
I'm first call to user defined function!
Again second call to the same function
Function Arguments
You can call a function by using the following types of formal arguments −
1
Function and Built in functions in python
1. Required arguments
2. Keyword arguments
3. Default arguments
4. Variable-length arguments
1. Required arguments:
Required arguments are the arguments passed to a function in correct positional order. Here,
the number of arguments in the function call should match exactly with the function
definition.
def add(a,b):
c=a+b
print(c)
add(5,7)
add(10,20)
result:
12
30
2. Keyword arguments:
Keyword arguments are related to the function calls. When you use keyword arguments in a
function call, the caller identifies the arguments by the parameter name.
This allows you to skip arguments or place them out of order because the Python interpreter
is able to use the keywords provided to match the values with parameters.
Example:
def display(x,y):
print("x=",x)
print("y=",y)
display(10,20)#required argument
display(x=50,y=70)#keyword argument
display(y=12,x=40)#keyword argument
Result:
x= 10
y= 20
x= 50
y= 70
x= 40
2
Function and Built in functions in python
y= 12
3. Default arguments:
A default argument is an argument that assumes a default value if a value is not provided in
the function call for that argument.
Example:
def display(x,y=0):
print("x=",x)
print("y=",y)
display(10,20)#required argument
display(x=50,y=70)#keyword argument
display(y=12,x=40)#keyword argument
display(37)# default argument
Result:
x= 10
y= 20
x= 50
y= 70
x= 40
y= 12
x= 37
y= 0
4. Variable-length arguments:
You may need to process a function for more arguments than you specified while defining
the function. These arguments are called variable-lengtharguments and are not named in the
function definition, unlike required and default arguments.
Syntax:
"function_docstring"
function_suite
return [expression]
Example:
def display(*arg):
3
Function and Built in functions in python
for i in arg:
print(i)
print("First call")
display(20)
print("Second call")
display(30,40,50)
Result:
First call
20
Second call
30
40
50
You can return multiple values from a function by separating them with a comma. This
actually creates a tuple, which you can use as is, or unpack in order to use the individual
values.
Example:
4
Function and Built in functions in python
Result:
(33, 90, 0.1, -27)
a= 33
b= 90
c= 0.1
d= -27
Built in functions:
5
Function and Built in functions in python
False
>>>
3 any() Return True if any element of >>> l=[1,2,3,4,5]
the iterable is true. If the >>> l1=[]
iterable is empty, return False. >>> l2=[-5,6,7,-9,0]
>>> l3=[True,False]
>>> any(l1)
False
>>> any(l)
True
>>> any(l2)
True
>>> any(l3)
True
>>>
4 ascii() return a string containing a
printable representation of an
object, but escape the non-
ASCII characters in the string
returned by repr() using \x, \u
or \U escapes
The non-ASCII characters in
the string is escaped using \x, \u
or \U.
5 bin() Convert an integer number to a >>> bin(10)
binary string prefixed with “0b” '0b1010'
>>> bin(-6)
'-0b110'
6 bool() Return a Boolean value, i.e. one >>> bool(10)
of True or False. f x is false or True
omitted, this returns False; >>> bool(-9)
otherwise it returns True True
>>> bool()
False
>>>
7 chr(i) Return the string representing a >>> chr(97)
character whose Unicode code 'a'
point is the integ >>> chr(89)
'Y'
9
Function and Built in functions in python
10
Function and Built in functions in python
>>> l2=[0,0,0]
>>> y=zip(l,l2)
>>> y
<zip object at
0x022700A8>
>>> list(y)
[(1, 0), (2, 0), (3, 0)]
11