0% found this document useful (0 votes)
2 views

python exp4

The document outlines an experiment focused on creating functions, classes, objects, and inheritance in Python. It explains the definition and calling of functions, types of function arguments, and the creation and usage of classes and objects. Additionally, it includes sample input/output tasks and concludes with a note on successful program execution.

Uploaded by

waghjayesh07
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

python exp4

The document outlines an experiment focused on creating functions, classes, objects, and inheritance in Python. It explains the definition and calling of functions, types of function arguments, and the creation and usage of classes and objects. Additionally, it includes sample input/output tasks and concludes with a note on successful program execution.

Uploaded by

waghjayesh07
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

`

EXPERIMENT NO: 4
Date of Performance :
Date of Submission :

AIM: Creating functions, classes, objects and inheritance in python

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.
`

Python class and objects:


Creating Classes
The class statement creates a new class definition. The name of the class immediately follows the
keyword class followed by a colon as follows– class ClassName:
'Optional class documentation string' class_suite
● The class has a documentation string, which can be accessed via ClassName. doc.
● The class_suite consists of all the component statements defining class members, data attributes
and functions

Create instance object


To create instances of a class, you call the class using class name and pass in whatever
arguments itsinit methodaccepts.
"This would create first object of Employee class" emp1 = Employee("Zara", 2000)
"This would create second object of Employee class" emp2 = Employee("Manni", 5000)

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

Built-In Class Attributes


Every Python class keeps following built-in attributes and they can be accessed using dot
operator like any other attribute − ● dict − Dictionary containing the class'snamespace.
● doc − Class documentation string or none, ifundefined.
● name − Classname.
● module− Module name in which the class is defined. This attribute is " main " in
interactive mode.
● bases − A possibly empty tuple containing the base classes, in the order of their occurrence
in the base classlist.
`

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

CONCLUSION: Program executed successfully

R1 R2 R3 R4 Total Signature
(3 Marks) (3 Marks) (3 (1 (10 Marks)
Marks) Mar
k)

You might also like