Functions 05.06.2019
Functions 05.06.2019
SANTOSH VERMA
Functions in Python
A function is a set of statements that do some specific
computation and produces output.
For example:
def maxVal(x, y):
if x>y:
return x
else:
return y
# Driver code
evenOdd(2)
evenOdd(3)
Pass by Reference or pass by
value?
One important thing to note is, in Python every
variable name is a reference.
Output:
# Driver code
lst = [10, 11, 12, 13, 14, 15]
myFun(lst);
print(lst)
Output:[10, 11, 12, 13, 14, 15]
# Keyword arguments
student(firstname =‘Santosh', lastname =‘verma')
student(lastname =‘verma', firstname =‘Santosh')
Variable number of arguments:
We can have both normal and keyword variable
number of arguments.
def myFun(*args): def myFun(**kwargs):
for arg in args: for key, value in kwargs.items():
print (arg) print ("%s == %s" %(key, value))
Output: Output:
FDP last == verma
on mid == kumar
Python first == santosh
Programming
Anonymous functions/ Lambda
Abstraction
Anonymous function means that a function is without a name.
Python allows users to handle files i.e., to read and write files, along
with many other file handling options, to operate on files.
Each line of code includes a sequence of characters and they form text
file. Each line of a file is terminated with a special character, called the
EOL or End of Line characters. It ends the current line and tells the
interpreter a new one has begun.
READ FILE
file = open('C:\\Users\\santosh_home\\Desktop\\fdp.txt', 'r')
# This will print every line one by one in the file
print (file.read())