Python Module 4
Python Module 4
Python Module 4
User defined Functions: - The functions which are developed by programmer explicitly
according to business requirements are called user defined functions.
Syntax:
def functionname(parameters):
statements
return
Parameters: - Parameters are inputs to the function. If a function contains parameters, then
at the time of calling, compulsory we should provide values otherwise we will get error.
Ex:
def wish(name):
print("Hello",name," Good morning")
wish("satish")
Ex:
def square(number):
print("The square of ",number," is",number*number)
square(6)
return statement: - The return statement is used to exit a function and go back to the place
from where it was called.
return [expressionlist];
This statement can contain expression which gets evaluated and the value is returned.
Example 1:
def addition(x,y):
return a+y
a,b=10,20
sum=addition(a ,b)
print (sum)
Ex:
def big(a,b):
if a>b:
return a
else:
return b
print(big(10,20))
print(big(50,40))
Types of arguments:
def f1(a,b):
-------
-------
f1(10,20)
→a,b are formal arguments whereas 10,20 are actual arguments.
There are 4 types of actual arguments are allowed in Python.
1. Positional arguments.
2. Keyword arguments.
3. Default arguments.
4. Variable length arguments.
Positional arguments: - This is the default method. These are the arguments passed to
function in correct positional order.
Ex:
def cal(a,b):
s=a-b
return s
print(cal(100,10))
print(cal(10,100))
→The number of arguments and position of arguments must be matched. If we change the
order then result may be changed.
→If we change the number of arguments then we will get error.
Keyword Arguments: - The keywords are mentioned during the function call along with
their corresponding values. These keywords are mapped with the function arguments so the
function can easily identify the corresponding values even if the order is not maintained
during the function call.
Using the Keyword Argument, the argument passed in function call is matched with function
definition on the basis of the name of the parameter.
Ex:
def cal(a,b,c):
s=a+b+c
return s
print(cal(c=33,a=23,b=55))
default: - Function arguments can have default values in Python. We can provide a default
value to an argument by using the equal operator.
Ex:
def cal(a=100,b=200,c=300):
s=a+b+c
return s
print(cal(10,44,55))
print(cal())
print(cal(10))
print(cal(10,44))
var-arg: -
→var-arg means variable length argument.
→Sometimes, we don’t know in advance the number of arguments that will be passed into a
function Python allows us to handle this type of situation through function calls with arbitrary
number of arguments.
→In this function, we use “*” before the parameter.
Ex:
def disp(*var):
for i in var:
print("var arg=",i)
disp()
disp(10,20,30)
disp(10,20.3,"satish")
Ex:
def sum(*a):
s=0
for x in a:
s=s+x
return s
print(sum(10,20))
print(sum(10,20,45,66))
print (sum())
Types of variables:
Global variables: - A variable which is declared outside of the function or in global scope is
known as global variable. That means, global variable can be accessed inside or outside of
the function.
Ex:
def wish():
print("Inner:",x)
x=10
wish()
print("Outer:",x)
Local variables: - A variable which is declared inside the function body or in a local scope is
known as local variables. These variables are accessed within the function.
Ex:
def f1():
y=10
print("Inner=",y)
f1()
print("outer=",y)
global keyword: -
global keyword allows to modify the variable outside of the function.
Rules:
→When we create a variable inside a function, it is local by default.
→ When we define a variable outside a function, it is global by default.
→We use global to read and write a global variable inside a function.
→Use of global keyword outside a function has no effect.
Ex:
def wish():
global x
x=10
print("Inner x:",x)
wish()
print("Outer x:",x)
Recursive functions: -
A function called by itself is known as recursion.
It reduces length of the code and improves readability. We can solve complex problems very
easily.
Ex: Write a program for factorial of a given number
def fact(n):
if n==0:
res=1
else:
res=n*fact(n-1)
return res
print(fact(5))
print(fact(6))
Ex:
s=lambda n:n*n
print(s(4))
print(s(6))
g=mygen()
print(type(g))
print(next(g))
print(next(g))
print(next(g))
#print(next(g)) #StopIteration
Files
As the part of programming requirement, we have to store our data permanently for future
purpose. For this requirement we should go for files.
File is a name location on disk to store related information. It is used to permanently store
data in a non-volatile memory.
File types: -
→In Python, a file is categorized as either text or binary files.
→Text files are structured as a sequence of characters. Each line is terminated with a
special character, called EOL or End Of Line character.
→A binary file is any type of file that is not a text file. Binary files are used to create non-text
files like image or exe files.
Closing a file:
After completing our operations on the file, it is highly recommended to close the file. For
this we have to use close() function.
Syntax:
fileobject.close()
Ex:
f.close()
In the above program all lines write in a file as single line. We can use \n to convert line
by line.
Ex:
f=open("test.txt",'w')
f.write("Python \n")
f.write("Java \n")
f.close()
print("Write operation completed")
Ex:
f=open("test.txt",'r')
print(f.read())
f.close()
→readline method by default provide new line at the end of the line. That’s every time two
lines created. To overcome the problem we can use end attribute in print statement.
Ex:
f=open("test.txt",'r')
line=f.readline()
print(line,end='')
line=f.readline()
print(line, end='')
f.close()
readlines(): - To read all the lines into list.
Ex:
f=open("test.txt",'r')
print(f.readlines())
f.close()
#output will be in form list
Ex:
f=open("test.txt",'r')
lines=f.readlines()
for line in lines:
print(line)
f.close()