Functions 2
Functions 2
Chapter
Functions
Function Introduction
Types of functions:
1.Built- in functions
2.Functions defined in module
3.User defined functions
1). Built-in Functions:
Functions which are already written or defined in python. As these
functions are already defined so we do not need to define these
functions. Below are some built-in functions of Python. Function
name Description
len() It returns the length of an object/value.
list() It returns a list.
max() It is used to return maximum value from a sequence
(list,sets) etc.
min() It is used to return minimum value from a sequence
(list,sets) etc.
open() It is used to open a file.
print() It is used to print statement.
str() It is used to return string object/value.
sum() It is used to sum the values inside sequence.
type() It is used to return the type of object.
tuple() It is used to return a tuple.
2). Functions defined in module:
A module is a file consisting of Python code. A module can
define functions, classes and variables.
Save this code in a file named
mymodule.py def greeting(name):
print("Hello, " + name)
import mymodule
mymodule.greeting(“India")
3). User defined function:
Functions that we define ourselves to do certain specific task
are referred as user-defined functions because these are not
already available.
Creating & calling a
Function
(user defined)/Flow of execution
a, b, x, y = 1, 2, 3,4
fun(50, 100) #passing value 50 and 100 in parameter x
and y of function fun()
print(a, b, x, y)
Variable’s Scope in
function
#Find the output of below program
def fun(x, y): # argument /parameter
x and y global a a = 10 x,y = y,x b =
20 b = 30 c = 30 print(a,b,x,y)
a, b, x, y = 1, 2, 3,4
fun(50, 100) #passing value 50 and 100 in parameter x and y of
function fun() print(a, b, x, y)
OUTPUT :-
10 30 100 50
10 2 3 4
Variable’s Scope in
function print("After calling
Functio
n
#Default #Variable length
arguments def
arguments / sum( *vartuple ): s=0
#Default Parameter for var in vartuple:
def sum(x=3,y=4): s=s+int(var)
z=x+y return s;
return z
r=sum( 70, 60,
r=sum() 50 ) print(r)
print(r) r=sum(4,5)
r=sum(x=4) print(r)
print(r)
r=sum(y=45
) print(r) #now the above function
sum() can sum n number of
#default value of x and y is values
being used when it is not
passed
Lamda
Python Lambda
A lambda function is a small anonymous function
which can take any number of arguments, but can
only have one expression. E.g.
x = lambda a, b : a
* b print(x(5, 6))
OUTPUT:30
Mutable/immutable
properties of data
objects w/r function
Mathematical functions:
Mathematical functions are available under math
module.To use mathematical functions under this
module, we have to import the module using import
math.
For e.g.
To use sqrt() function we have to write statements
like given below.
import math
r=math.sqrt(4)
print(r)
OUTPUT :
2.0
Functions using libraries
Functions available in Python Math
Module
Functions using libraries(System defined function)
String functions:
String functions are available in python standard
module.These are always availble to use.
OUTPUT:
I love
programming
Functions using
libraries
String functions:
Method Description
capitalize() Converts the first character to upper case
casefold()
Converts string into lower case
center()
count() Returns a centered string
encode() Returns the number of times a specified value occurs in
endswith() a string
find()
Returns an encoded version of the string
format()
index() Returns true if the string ends with the specified value
Searches the string for a specified value and returns the
position of where it was found
Formats specified values in a string
Searches the string for a specified value and returns the
position of where it was found
Functions using
libraries
String functions:
Method Description
isalnum() Returns True if all characters in the string are
isalpha() alphanumeric
isdecimal( Returns True if all characters in the string are in the
) isdigit() alphabet
isidentifie
Returns True if all characters in the string are decimals
r()
islower() Returns True if all characters in the string are digits
isnumeric( Returns True if the string is an identifier
) Returns True if all characters in the string are lower
isprintabl case
e() Returns True if all characters in the string are numeric
isspace()
Returns True if all characters in the string are printable
istitle()
isupper() Returns True if all characters in the string are
join() whitespaces
ljust() Returns True if the string follows the rules of a title
lower() Returns True if all characters in the string are upper
lstrip() case
partition() Joins the elements of an iterable to the end of the string
Returns a left justified version of the string
Converts a string into lower case
Returns a left trim version of the string
Returns a tuple where the string is parted into three
parts
Functions using
libraries
String functions:
Method Description