Chapter-2 Function
Chapter-2 Function
Chapter-2 Function
1. Built-in
2. Modules
3. User Defined
• 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.
• P ython 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.
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.
e.g. i) name=input(‚Enter your name : ‚)
ii) n= int(input(‘Enter number:’))
3. eval function: This function is used to evaluate the value
of a string.
e.g. x=eval(‚10+10‚)
print(x) # answer will be 20
4. min Function: This
function returns the smallest
value among given list of
values.
5. max Function: This
function returns the biggest
value among given list of
values.
6. abs Function: This
function returns the absolute
value of any integer which is
always positive.
7.type Function: This
function is used to identify the
type of any value or variable.
8.len Function: This
function returns the length of
given string.
9.round Function: This
function returns the rounded
number of given number up to
given position.
10. 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 S T E P .
• Module is a .py file which contains the definitions
of functions and variables.
• random (): This generates floating value between 0 and 1. it does not
require any argument.
• 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.
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
• These are the functions which are made by
user as per the requirement of the user.
def Function_Name(List_Of_Parameters):
‛‛‛docstring‛‛‛
statement(s)
After the line containing def
there should be a 4 spaces
Keyword indentation, which is also
know as the body or block
of the function. Function
Definition
without argument without return
Function
Definition
Function Call
with argument without return
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.
with multiple return values
def list_avg(li):
sum=0
for i in li:
sum=sum+i
return sum/n
li=[]
n=int(input('Enter size of elements to be enter in list:'))
c=0
while c<n:
a=int(input('Enter numbers in list:'))
li.append(a)
c=c+1
print(li)
avg1=list_avg(li)
print('Average of list is;',round(avg1,2))
*
Enter size of elements to be enter in
list:4
Enter numbers in list:34
Enter numbers in list:5
Enter numbers in list:23
Enter numbers in list:56
[34, 5, 23, 56]
Average of list is; 29.5
• 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.
• 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.
Difference between Global variable and local variable
ANSWER IS:
1->7->8->9->1->2->3->4->9->10->11->12->13->1->2->3->4->14->15->16
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.