python exp4
python exp4
EXPERIMENT NO: 4
Date of Performance :
Date of Submission :
THEORY:
A function is a block of organized, reusable code that is used to perform a single, related action.
Functions provide better modularity for your application and a high degree of code reusing.
Python gives you many built- in functions like print(), etc. but you can also create your own
functions. These functions are called user- defined functions.
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 theseparentheses.
● 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 isindented.
● 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 returnNone.
Syntax def functionname( parameters ):
"function_docstring" function_suite return [expression]
EXAMPLE def printme( str ):
"This prints a passed string into this function" print str return
Calling a Function
Once the basic structure of a function is finalized, you can execute it by calling it from another
function or directly from the Python prompt. Following is the example to call printme() def
printme( str ):
`
"This prints a passed string into this function" print str return; # Now you can call
printme function printme("I'm first call to user defined function!") printme("Again
second call to
the same function") Pass by reference vs value
All parameters (arguments) in the Python language are passed by reference. It means if you
change what a parameter refers to within a function, the change also reflects back in the calling
function.
Function Arguments
You can call a function by using the following types of formal arguments −
● Required arguments
● Keyword arguments
● Default arguments
● Variable-length arguments
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
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.
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.
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-length arguments and are not named in the
function definition, unlike required and default arguments.
Syntax for a function with non-keyword variable arguments is this –
def functionname([formal_args,] *var_args_tuple ):
"function_docstring" function_suite
return [expression]
An asterisk (*) is placed before the variable name that holds the values of all non-keyword
variable arguments. This tuple remains empty if no additional arguments are specified during the
function call.
`
Accessing Attributes
You access the object's attributes using the dot operator with object. Class variable would be
accessed using class name as follows –
emp1.displayEmployee()
emp2.displayEmployee()
print "Total Employee %d" % Employee.empCount
INPUT/OUTPUT
Write a python program that accepts two strings as input and find the string with max
length
Write a python program to print dictionary where keys are no’s between 1 and
20(both included) & values are square of keys
`
Write a python program to implement Object Oriented Programming concept like Classes,
encapsulation, inheritance and multiple inheritance
R1 R2 R3 R4 Total Signature
(3 Marks) (3 Marks) (3 (1 (10 Marks)
Marks) Mar
k)