0% found this document useful (0 votes)
10 views41 pages

PPS - Notes - Unit 5

The document outlines the vision and mission of the Sinhgad Institute of Technology's Department of Engineering Sciences, emphasizing the holistic development of students and the incorporation of effective teaching methods. It details short and long-term goals, program educational objectives, and outcomes related to the Programming and Problem Solving course, focusing on Python programming and object-oriented programming principles. Additionally, it discusses various programming paradigms and their advantages and disadvantages, particularly highlighting the features and benefits of object-oriented programming.

Uploaded by

atharvsalunke999
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views41 pages

PPS - Notes - Unit 5

The document outlines the vision and mission of the Sinhgad Institute of Technology's Department of Engineering Sciences, emphasizing the holistic development of students and the incorporation of effective teaching methods. It details short and long-term goals, program educational objectives, and outcomes related to the Programming and Problem Solving course, focusing on Python programming and object-oriented programming principles. Additionally, it discusses various programming paradigms and their advantages and disadvantages, particularly highlighting the features and benefits of object-oriented programming.

Uploaded by

atharvsalunke999
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 41

SINHGAD TECHNICAL EDUCATION SOCIETY’S

SINHGAD INSTITUTE OF TECHNOLOGY


Kusgaon (Bk), Lonavala 410401

DEPARTMENT OF ENGINEERING SCIENCES

Programming and Problem Solving [110005]

F.E. Eng Sci. (SEM –I)

AY 2022-23

Prepared By

Prof. S.B.Patil
Vision and Mission of Institute

VISION

उत्तमपुरुषान् उत्तमाभियंतृन् निर्मांतु कटिबद्धाःवयम्

“We are committed to produce not only good engineers but good human beings, also.”

MISSION

• We believe in and work for the holistic development of students and teachers.
• We strive to achieve this by imbibing a unique value system, transparent work culture,
excellent academic and physical environment conducive to learning, creativity and
technology transfer.

Vision and Mission of the Department

VISION

The department of Engineering Sciences is committed to support the core engineering programs
with fundamental knowledge and skills with acumen to be leaders amongst the generation of
engineers.
MISSION
The department of Engineering Sciences strives to incorporate the best pedagogical methods to
deliver basic sciences to engineering students and to guide them to be proactive learners, deep
thinkers and responsible citizens in the early stages of their engineering education.

Short Term Goals

 To foster students of first year engineering for the respective programs of engineering
through expert lectures, seminars & mini projects.
 To implement activity plan for overall development of students.
 To develop laboratories for meaningful implementation of curriculum and then for
research.

Prof. S. B. Patil, Mechanical Engineering Dept, SIT Lonavala. 2


 To encourage continuous up-gradation of faculty members through higher education and
external interface with other universities.

Long Term Goals

 To apply project and problem based leaning approach for first year engineering program
to create a bridge to support to core departments for guiding projects of students.
 To cultivate research in the field of Engineering Sciences for the benefit of society.

Program Educational Objectives: PEO’s

I. To provide opportunity to acquire strong theoretical and sound practical knowledge of


basic sciences, so as to enable them to excel in further education.
II. To develop talent among students to innovate, communicate, analyze, interpret and apply
technical concepts to solve real time engineering problems.
III.To aware and accomplish scientific and engineering breadth amongst student through
various curricular, co-curricular and extra-curricular activities.
IV. To inculcate professional and ethical attitude among students for providing engineering
solution in a global and societal context.
V. To accomplish the overall development of students with the aid of project based learning
environment.
Program Outcomes: POs

POs are statements that describe what students are expected to know and be able to do upon
graduating from the program. These relate to the skills, knowledge, analytical ability attitude and
behavior that students acquire through the program.

Engineering Knowledge: Apply the knowledge of mathematics, science, engineering


fundamentals, and an engineering specialization to the solution of complex engineering
problems.

i) Problem Analysis: Identify, formulate, review research literature, and analyze complex
engineering problems reaching substantiated conclusions using first principles of mathematics,
natural sciences and engineering sciences.
ii) Design/Development of Solutions: Design solutions for complex engineering problems and
design system components or processes that meet the specified needs with appropriate
consideration for the public health and safety, and the cultural, societal, and environmental
considerations.

Prof. S. B. Patil, Mechanical Engineering Dept, SIT Lonavala. 3


iii) Conduct Investigations of Complex Problems: Use research-based knowledge and research
methods including design of experiments, analysis and interpretation of data, and synthesis of
the information to provide valid conclusions for complex problems:
 that cannot be solved by straightforward application of knowledge, theories and techniques
applicable to the engineering discipline as against problems given at the end of chapters in a
typical text book that can be solved using simple engineering theories and techniques;
 that may not have a unique solution. For example, a design problem can be solved in many
ways and lead to multiple possible solutions;
 that require consideration of appropriate constraints / requirements not explicitly given in
the problem statement such as cost, power requirement, durability, product life, etc.;
 which need to be defined (modelled) within appropriate mathematical framework; and
 that often require use of modern computational concepts and tools, for example, in the
design of an antenna or a DSP filter.
iv) Modern Tool Usage: Create, select, and apply appropriate techniques, resources, and
modern engineering and IT tools including prediction and modelling to complex engineering
activities with an understanding of the limitations.
v) The Engineer and Society: Apply reasoning informed by the contextual knowledge to
assess societal, health, safety, legal and cultural issues and the consequent responsibilities
relevant to the professional engineering practice.
vi) Environment and Sustainability: Understand the impact of the professional engineering
solutions in societal and environmental contexts, and demonstrate the knowledge of, and
need for sustainable development.
vii) Ethics: Apply ethical principles and commit to professional ethics and responsibilities and
norms of the engineering practice.
viii) Individual and Team Work: Function effectively as an individual, and as a member or
leader in diverse teams, and in multidisciplinary settings.
ix) Communication: Communicate effectively on complex engineering activities with the
engineering community and with society at large, such as, being able to comprehend and
write effective reports and design documentation, make effective presentations, and give and
receive clear instructions.
x) Project Management and Finance: Demonstrate knowledge and understanding of the
engineering and management principles and apply these to one’s own work, as a member
and leader in a team, to manage projects and in multidisciplinary environments.
xi) Life-long Learning: Recognize the need for, and have the preparation and ability to engage
in independent and lifelong learning in the broadest context of technological change.
Program Specific Outcomes: PSOs

1. Get solid foundation in basic sciences along with engineering fundamentals for a
successful professional career.
2. Able to co-relate engineering issues to broad social context.

Faculty of Engineering Savitribai Phule Pune


University

Prof. S. B. Patil, Mechanical Engineering Dept, SIT Lonavala. 4


110005: Programming and Problem Solving

Credit: 04

Teaching Scheme: Examination Scheme:

Lecture : 03hr/week In-Sem: 30 Marks

End-Sem: 70 Marks

Course Objectives: Prime objective is to give students a basic introduction to programming and
problem solving with computer language Python. And to introduce students not merely to the
coding of computer programs, but to computational thinking, the methodology of computer
programming, and the principles of good program design including modularity and
encapsulation.
1) To understand problem solving, problem solving aspects, programming and to know about
various program design tools.
2) To learn problem solving with computers
3) To learn basics, features and future of Python programming.
4) To acquaint with data types, input output statements, decision making, looping and functions
in Python
5) To learn features of Object Oriented Programming using Python
6) To acquaint with the use and benefits of files handling in Python

Course Outcomes:

On completion of the course, student will be able to

CO1. Inculcate and apply various skills in problem solving.

CO2. Choose most appropriate programming constructs and features to solve the problems in
diversified domains.

CO3. Exhibit the programming skills for the problems those require the writing of well-
documented programs including use of the logical constructs of language, Python.

CO4. Demonstrate significant experience with the Python program development environment

Prof. S. B. Patil, Mechanical Engineering Dept, SIT Lonavala. 5


FE -2019 Patten
Savitribai Phule University, Pune
Subject: Programming and Problem Solving [110005]

Unit V : Object Oriented Programming

Contents: Programming Paradigms- monolithic, procedural, structured and


object oriented, Features of Object oriented programming- classes, objects,
methods and message passing, inheritance, polymorphism, containership,
reusability, delegation, data abstraction and encapsulation. Classes and Objects:
classes and objects, class method and self object, class variables and object
variables, public and private members, class methods.

Unit Objectives :
On completion the students will be able to :
1. Understand the different programming Paradigms.
2. Understand the various features of OOP.
3. Get standard Classes and Objects.
Unit outcomes:
Students are able to understand features of OOP:
1. Understand use of Classes, Objects, Methods and Inheritance, Polymorphism
using Pyhton program.
2. Exhibit the programming skills for the problems those require to use Classes and
Objects using Python program.
Start to apply various skills in problem solving using OOP features.

Outcome Mapping: Mapping of PEO,PO, CO, PSO


PEOs: I, II POs: i, ii, iii PSOs: 3,4 COs:3

Books :
1. Reema Thareja, “Python Programming Using Problem Solving Approach”,
Oxford University Press

Prof. S. B. Patil, Mechanical Engineering Dept, SIT Lonavala. 6


Object-Oriented Programming
Turtles are fun to program because they make drawing easy by
remembering many of the details of generating computer graphics for us.
That’s really the motivation behind object-oriented programming and
using objects. What we’ve seen in this short chapter are all the
mechanics for creating and using objects. Objects make our lives as
programmers simpler. Every object maintains some state information,
its data, and every object lets us either access that data through an
accessor function or it allows its data to be changed by calling mutator
methods. Many objects have both accessor and mutator methods. The
power of object-oriented programming is in the ability to organize the
data in our programs into logical entities that somehow make sense. A
turtle is a great way to embody many of the elements of graphics
programming while giving us a way of visualizing how the turtle works
by thinking about how a real turtle might leave marks in the sand.

Introduction to Programming Paradigm


• A programming paradigm is a philosophy, style, or general
approach to writing code.
• Paradigm can also be termed as method to solve some problem or
do some task.
• Programming can be expressed in various ways.
• Over the years, people have improved programming in various
ways.
• Paradigm means “type” or “ideal example”.
• Majorly there are four types of programming.

Prof. S. B. Patil, Mechanical Engineering Dept, SIT Lonavala. 7


1. Monolithic programming
2. Procedural programming
3. Structured programming (Paradigm)
4. Object Oriented Programming

1. Monolithic programming
• Here complete program is written as a sequence.
• There are no modules or functions used.
• Example: Assembly Language and BASIC.
Advantage :
• It is simplest way of programming.
Disadvantage :
• Its problem is there can be lot of repetition of code when same
operation has to be done many times.
• The global data can be accessed and modified from any part of
the program, thereby, posing a serious threat to its integrity.
2. Procedural programming
• Here program is divided in procedures or functions.
• Function is a small unit of programming logic.
Advantages :
• Same function can be used multiple times in one program
whenever the operation is needed.
Prof. S. B. Patil, Mechanical Engineering Dept, SIT Lonavala. 8
• It reduces redundancy in program.
• It makes program readable.
Disadvantages
• Data can be modified by any procedure.
• Data is not protected properly.
• It may include ‘goto’ like statements which makes program un-
structured and poor in readability.
3. Structured programming (Paradigm)
• Here program is written in a structured format compulsorily.
• Now most of the procedural programming languages support
structured programming paradigm.
• Structured programming, also referred to as modular
programming.
• The primary focus is on functions (Modules).
• The technique of hierarchical decomposition has been used to
specify the tasks to be completed for solving a problem.
• Structured programming, organizing instructions into groups
known as functions.
• Each function may have its own local data.
Advantages
• It is readable program.
• It is easy to find errors or especially logical errors.

Prof. S. B. Patil, Mechanical Engineering Dept, SIT Lonavala. 9


• Efficient, correct programs that are easy to understand, debug and
change.
• Modules enhance programmer’s productivity.
Advantages
• Many programmers can work on a single, large program.
• Each module performs a specific task.
Disadvantage
• Data can be modified by any procedure or part of the program.
• Not data-centered.
• Global data are more vulnerable to an inadvertent change by a
function.
4. Object Oriented Programming
• Here data is major focus.
• Programs are divided in terms of objects and not procedures.
• Data and functions are put together to avoid misuse or un-
intentional modifications in data.
• Data is hidden and not accessible by external functions.
• New data and functions can be easily added as and when required.
• Here “Class” puts together data and functions.
• Only functions belonging to that class can modify the data.
• Also OOP supports features like inheritance, abstraction, etc.
• Follows a bottom-up approach for problem solving.
Prof. S. B. Patil, Mechanical Engineering Dept, SIT Lonavala. 10
Advantages
• OOP features enable re-use of programs very easy.
• Data is protected here.
Disadvantage
• Dependence of a class on other class may lead to inefficient
programs.

Features of Object Oriented Programming:


 Classes
• Objects
• Methods
• Message Passing
• Inheritance
• Polymorphism
• Containership
• Reusability
• Delegation
• Data Abstraction and Encapsulation
1. Class
• A class is a blue print to create objects or instances.
• A class is used to describe a real time entity in the world, such as
occurrences, things, external entities, and so on.

Prof. S. B. Patil, Mechanical Engineering Dept, SIT Lonavala. 11


• A class provides a template that describes the structure and
behavior of a set of similar objects.
• It is model which is used to generate exact same objects or
instances.
• In a class variable and functions can be put together.
• Thus any object of given class is having its variables and its
methods or functions together.
• Once we have the definition for a class, a specific instance of the
class can be easily created.
Advantages :
• Class allows creating user defined data structure.
• Putting variables and functions together make data protection
easy.
• It also helps in simulating real world scenario.
• Syntax:
• class class_name:
• <statement-1>
• .
• .
• <statement-N>

Prof. S. B. Patil, Mechanical Engineering Dept, SIT Lonavala. 12


• Example 1:
• class Employee:
• id=10;
• name=“Farendra”
• def display (self):
• print(self.id, self.name)

Example 2:

Output:

Prof. S. B. Patil, Mechanical Engineering Dept, SIT Lonavala. 13


2. Objects
• Object is a instance of a class.
• A class needs to be instantiated if we want to use the class
attributes in another class or method.
• A class can be instantiated by calling the class using the
class name.
• A class can have multiple instances or objects.
• The syntax to create the instance of the class is <object-
name> = <class-name>
• Every object contains some data and procedures. They are
also called methods.
• It contains all variables and methods together.
• For example, class is like plan of a building and object is
real building.
• One plan can be used to generate multiple buildings.
• In following code three objects of Sample class are created
with different values.
Example1:

Prof. S. B. Patil, Mechanical Engineering Dept, SIT Lonavala. 14


Example 2:
class Employee:

id = 10;

name = “Shital"

def display (self):

print("ID: %d \nName: %s"%(self.id,self.name))

emp = Employee()

emp.display()

Output:

ID: 10

Name: Shital

3. Methods and Message Passing


• A method is a function associated with a class.
• Methods are defined inside a class.
• They can work on the data or variables inside the class.
• They can be used to modify or read different variables of
any object.
• Methods use values of variables which are stored inside
current object (calling object).
• So same method called from different objects will give
different results.
• It defines the operations that the object can execute when it
receives a message.

Prof. S. B. Patil, Mechanical Engineering Dept, SIT Lonavala. 15


• In Python, a method is not unique to class instances. Any
object type can have methods.
• In OOP language, only methods of the class can access and
manipulate the data stored in an instance of the class (or
object).
• Two objects can communicate with each other through
messages. An object asks another object to invoke one of
its methods by sending it a message.
• Class methods (or functions defined in the class) are
exactly same as ordinary functions that we have been
defining so far with just one small difference. Class
methods must have the first argument named as self.
• This is the first argument that is added to the beginning of
the parameter list. Moreover, you do not pass a value for
this parameter when you call the method. Python provides
its value automatically. The self argument refers to the
object itself. That is, the object that has called the method.
• This means that even if a method that takes no arguments,
should be defined to accept the self. Similarly, a function
defined to accept one parameter will actually take two- self
and the parameter, so on and so forth.

Prof. S. B. Patil, Mechanical Engineering Dept, SIT Lonavala. 16


class ABC():

var=10

def display(self):

print(“In Class Method……”)

obj=ABC()

print(obj.var)

obj.display()

Output:

10

In Class Method…..

Example :

• In following code Employee class is shown.

• It has three methods

1. __init__(self, name, salary) :: This is a constructor method

2. displayCount() :: This is a class method

3. displayEmployee(self) :: This is a instance method

Prof. S. B. Patil, Mechanical Engineering Dept, SIT Lonavala. 17


4. Inheritance
• The mechanism of deriving a new class from an old one is
called Inheritance.
• In OOP inheritance is just like real world inheritance. Here
child class inherits properties from a parent or base class.
Methods of parent class can be directly accessed in child
class.
• The old class is called base class and the new one is called
derived class or subclass.
• The derived class inherits the features of base class.
• Also all public variables of parent class are accessible. It is
used to re-use methods of parent class easily. Constructor
of parent class can be called within child class constructor
so that data members of parent class can be set.
Prof. S. B. Patil, Mechanical Engineering Dept, SIT Lonavala. 18
• “super()” keyword is used to access parent class
constructor.
• In python all classes are child class of “object” class.
• Example: Point is base class and Line is derived class.
Advantages :
• Inheritance increase the functionality of previously defined
classes without having to rewrite the original class
declaration. This can be a greater time saver in writing
applications.
• “Inheritance is the mechanism to provides the power of
reusability and extendibility.”
• “Inheritance is the process by which one object can acquire
the properties of another object.”
5. Polymorphism
• Polymorphism refers to having several different forms. It
is related to methods.
• It means same method behaves differently for different
inputs.
• Polymorphism is a concept that enables the programmers
to assign a different meaning or usage to a method in
different contexts.
• Polymorphism can also be applied to operators. .

Prof. S. B. Patil, Mechanical Engineering Dept, SIT Lonavala. 19


• For example, “+” (plus) operator adds two integers and
returns a number. But in case of strings same operator “+”
concatenates two strings.
Advantages :
• Code reusability.
• Operators behave according to input so readability of
program increases.
Real life example of Polymorphism
• Suppose if you are in class room that time you behave like
a student, when you are in market at that time you behave
like a customer, when you at your home at that time you
behave like a son or daughter, Here one person have
different-different behaviors.
6. Containership
• Containership is the ability of a class to contain object(s)
of one or more classes as member data.
• Here object of a class can be part of another class.
• Also there can be a class within a class.
• For example, class One can have an object of class Two as
its data member. This would allow the object of class One
to call the public functions of class Two. Here, class One
becomes the container, whereas class Two becomes the
contained class. Containership is also called composition.
Prof. S. B. Patil, Mechanical Engineering Dept, SIT Lonavala. 20
• Example of containership can be, a department which
contains objects of classes and labs.
Advantage :
• Real world scenario can be better modeled here.
7. Re-usability
• OOP’s most important application is re-usability.
• Reusability means developing codes that can be reused
either in the same program or in different programs.
• A code in class or function can be reused by inheritance or
by overriding.
• Python gives due importance to building programs that are
reusable.
• Reusability is attained through inheritance, containership,
and polymorphism.
• It makes creating libraries and use of libraries very easy.
• It increases robustness and reliability of code.
8. Delegation
• In OOP code is highly modular.
• Here each class has well defined functionalities.
• So it becomes easy to just access the functionality
anywhere.
Prof. S. B. Patil, Mechanical Engineering Dept, SIT Lonavala. 21
• No other class needs to rework again for same
functionality.
• Delegation is supported here by allowing ease of access to
different modules.
• So, each module can have fixed small functionality.
• It helps in generating readable code.
• Provides maximum flexibility to programmers and allow
them to generate reusable code.
• In delegation more than one object is involved in handling
a request. The object that receives the request for a service,
delegates it to another object called its delegate.
• Property of delegation emphasizes on complex object
made of several simpler object.
• Has-a relationship.
9. Data-Abstraction
• OOP allows different access to data.
• Abstraction is the concept of exposing only the required
essential characteristics and behavior with respect to a
context (and the implementation details are hidden).
• Hiding of data is known as data abstraction.
• Some data can be public means it can be accessed and
modified by anyone.
Prof. S. B. Patil, Mechanical Engineering Dept, SIT Lonavala. 22
• Also there can be some data which is not available for
everyone.
• So, only data which is relevant to current class is given.
• User can access data only according to his access level.
• Also Abstract Class helps create standard classes.
• Such Abstract classes force implementation in future child
classes.
• Also, Abstract classes give abstract overview of required
standard methods.

10. Encapsulation
• As discussed in definition of class, OOP allows data
encapsulation.
• Encapsulation is a process of wrapping of data and
methods in a single unit.
• Combining of state and behavior in a single container is
known as encapsulation.
• Here data and functions are put together in a class.
• Users are allowed to execute only a restricted set of
operations (class methods) on the data members of the
class.

Prof. S. B. Patil, Mechanical Engineering Dept, SIT Lonavala. 23


• Encapsulation organizes the data and methods into a
structure that prevents data access by any function (or
method) that is not specified in the class.
• This allows access and modification of private data ONLY
by relevant functions.
• It increases data security.
11. Message Passing
• Meaning of message passing between classes is calling
methods.
• In Python any class can call methods of any other class
using object of that class.
• All methods are public so it becomes very easy to use
classes in different programs.
• To class from other module or file, that module needs to be
imported.
• Once import is done then that class and its methods can be
accessed.

Prof. S. B. Patil, Mechanical Engineering Dept, SIT Lonavala. 24


Classes and Objects :

Most of the part of classes & object covered above.

Class methods (static methods)


• Class methods are also known as static methods.
• These are bound to class.
• So, they can be called directly by using class name.
• No need to create object to call these methods.
• These are shared among all objects of a class.
• In python all methods which do not contain ‘self’ as
argument are class methods.
• Class methods are little different from these ordinary
methods. First, they are called by a class (not by instance of
the class). Second, the first argument of the class method is
cls not the self.
• Class methods are widely used for factory methods, which
instantiate an instance of a class, using different parameters
than those usually passed to the class constructor.
• Class methods are marked with a classmethod decorator.

Prof. S. B. Patil, Mechanical Engineering Dept, SIT Lonavala. 25


• A decorator is syntactic convenience that takes in a
function, adds some functionality to it and then returns it.
Class methods
• Syntax: use @ character as a prefix to the function
@classmethod
def function name(arg..):
statements
• Example:
class Fruit:
name = 'Fruitas'
@classmethod
def printName(cls):
print('The name is:', cls.name) Fruit.printName()
• Output:
('The name is:', 'Fruitas')

Instance methods (non-static methods)


• Instance methods are also known as non-static methods.
• These are bound to every instance or object of the class.

Prof. S. B. Patil, Mechanical Engineering Dept, SIT Lonavala. 26


• So, they can be called ONLY by using an object.
• We need to create object to call these methods.
• In python all methods which do contain ‘self’ as FIRST
argument are class methods.
THE __INIT__() METHOD (THE CLASS
CONSTRUCTOR)
• The __init () method has a special significance in Python
classes. The__init__() method is automatically executed
when an object of a class is created.
• The method is useful to initialize the variables of the class
object.
• Note the init () is prefixed as well as suffixed by double
underscores.
• Example:
class ABC():
def __init__(self, val):
print(“In Class Method……”)
Self.val = val
print(“The value is :”, val)
obj=ABC()
Output:
Prof. S. B. Patil, Mechanical Engineering Dept, SIT Lonavala. 27
In Class Method……
The value is : 10

Self object
• ‘self’ is an important keyword in python.
• ‘self’ is reference to current object calling method.
• ‘self’ reference or object gives access to all values stored
in an object.
• ‘self’ can be used ONLY within an instance method within
a class.
• ‘self’ is also used as first default parameter to every
function in a class.
Class variables (static variables)
• Class variable or Static variables are variables which are
created once for a class.
• Such variables are common to all objects of a class.
• In python class variables are declared outside any function
in a class.
• Class variables are accessed using name of the class.
• Class variables do not require object of class to access
them.
Prof. S. B. Patil, Mechanical Engineering Dept, SIT Lonavala. 28
• ALL class variables are PUBLIC.
• Accessing, modification and deletion of class variables is
same as instance variables (non-static) variables, as
discussed in next part.
• Only difference in accessing, modification and deletion is
CLASS_NAME has to be used before class variable and in
instance variable self is used.
Example:

Class variables and Object variables:


• Basically, these variables are of two types- class variables
and object variables.
• Class variables are owned by the class and object variables
are owned by each object. What this specifically means can
be understood using following points.

Prof. S. B. Patil, Mechanical Engineering Dept, SIT Lonavala. 29


• If a class has n objects, then there will be n separate copies
of the object variable as each object will have its own
object variable.
• The object variable is not shared between objects.
• A change made to the object variable by one object will not
be reflected in other objects.
• If a class has one class variable, then there will be one
copy only for that variable. All the objects of that class will
share the class variable.
• Since there exists a single copy of the class variable, any
change made to the class variable by an object will be
reflected to all other objects.

• Example of Class variables and Object variables :


Class ABC():
class_var=0 #class variable
def _init_(self,var):
ABC.class_var += 1
self.var = var #object variable
print(“The object value is :”, var)

Prof. S. B. Patil, Mechanical Engineering Dept, SIT Lonavala. 30


print(“The value of class variable is :”,
ABC.class_var)
obj1=ABC(10)
obj1=ABC(20)
obj1=ABC(30)
Output:
The object value is : 10
The value of class variable is : 1
The object value is : 20
The value of class variable is : 2
The object value is : 30
The value of class variable is : 3

Prof. S. B. Patil, Mechanical Engineering Dept, SIT Lonavala. 31


Public members
• All variables and methods in class are public by default.
• They can be accessed or modified anywhere in the
program or script.
• They can be modified by any other part of the program.
Advantage
• Keeping all variables public help in easy access of data
and function.
Disadvantage
• Data privacy and security is a big problem in python.

Prof. S. B. Patil, Mechanical Engineering Dept, SIT Lonavala. 32


Private members
• Private variables, on the other hand, are those variables
that are defined in the class with a double score prefix (__).
These variables can be accessed only from within the class
and from nowhere outside the class.
• Example:
Class ABC():
def __init__(self, var1, var2):
self.var1 = var1
self.__var2 = var2
def display(self):
print(“from class method, var1=”, self.var1)
print(“from class method, var2=”, self.__var2)
obj= ABC(10,20)
obj.display()
print(“from main module, var1=”, obj.var1)
print(“from main module, var2=”, obj.__var2)
• Output:
from class method, var1= 10
from class method, var2= 20

Prof. S. B. Patil, Mechanical Engineering Dept, SIT Lonavala. 33


from main module, var1= 10
from main module, var2=
Traceback (most recent call last):
File “c:\python34\Try.py”, line 11, in <module>
print(“from main module, var2=”, obj.__var2)
AttributeError: ABC instance has no attribute ‘__var2’

Python: Types of Access Modifiers


• There are 3 types of access modifiers for a class in Python.
These access modifiers define how the members of the
class can be accessed. Of course, any member of a class is
accessible inside any member function of that same class.

• Access Modifier: Public The members declared as Public


are accessible from outside the Class through an object of
the class.
Prof. S. B. Patil, Mechanical Engineering Dept, SIT Lonavala. 34
• Access Modifier: Protected The members declared as
Protected are accessible from outside the class but only in a
class derived from it that is in the child or subclass.
• Access Modifier: Private These members are only
accessible from within the class. No outside Access is
allowed.

EXAMPLES

Public Access Modifier By default, all the variables and


member functions of a class are public in a python program.

Protected Access Modifier According to Python convention


adding a prefix _(single underscore) to a variable name makes it
protected. Yes, no additional keyword required.

Prof. S. B. Patil, Mechanical Engineering Dept, SIT Lonavala. 35


Private Access Modifier While the addition of prefix __(double
underscore) results in a member variable or function becoming
private.

Prof. S. B. Patil, Mechanical Engineering Dept, SIT Lonavala. 36


All Public, Protected and Private data members in one
Example:

Prof. S. B. Patil, Mechanical Engineering Dept, SIT Lonavala. 37


Summary of Chapter:

A. Important points:
1. Programming Paradigm
2. Features of Object oriented programming- classes, objects, methods and message
passing,
3. inheritance, polymorphism,
4. containership, reusability, delegation,
5. data abstraction and encapsulation,
6. class method and self object
7. class variables and object variables
8. public and private members

B. Important Questions (University already Asked)

Unit -V Object Oriented Programming


Question Bank

Sr.No. Question Marks

Lecture 1
1 Write about monolithic programming 5

2 Differentiate structured vs object oriented programming 6

3 Give brief of classification of programming paradigm 5

4 What do you understand by term programming paradigm? Explain any three 6


programming paradigm.

5 Differentiate procedural programming vs object oriented programming. 6

6 Discuss any three Programming paradigm in detail. 6

7 What do you understand by the term "programming paradigm"? 5

Prof. S. B. Patil, Mechanical Engineering Dept, SIT Lonavala. 38


Lecture 2
8 Write a class that displays students information. 5

9 List and explain concepts of OOP. 5

10 Explain the main features of Object Oriented Programming Language? 5

11 What is class? How to create object and call methods of a class. 6

12 Differentiate between class and object. 6

13 Explain any four features of object oriented programming? 6

14 Explain the state of the objects in Python. 5

15 What is class and object in python Explain in detail. 6

16 Write a python program to create class car with two attributes name and 6
cost. Create two objects and display information.

17 Explain Class, Objects and Methods in Python. 6

18 Write a python program that demonstrate the use of class, object and 6
method.

19 List and Explain any 4 features of object oriented programming using 6


example.

Lecture 3
20 Define Terms 1) Polymorphism 2) Inheritance 6

21 Justify the statement "Inheritance help to make reusable Code". 5

22 Explain method and message passing in python 6

23 How to implement inheritance in Python? Explain with example. 6

24 What do you understand by the term polymorphism. 6

Lecture 4
25 Explain the terms. 1. Inheritance 2. Data abstraction 3. Encapsulation 4. 5

Prof. S. B. Patil, Mechanical Engineering Dept, SIT Lonavala. 39


polymorphism 5. Containership

26 Explain the three levels of data protection with example. 6

27 What does self argument signify in the class. Give example. 6

28 Explain Data Abstraction and Encapsulation. 6

29 Explain Inheritance & Data Abstraction. 6

30 Write a note on: 1.containership 2. Delegation. 6

31 Illustrate the concept of Containership . Write a program in python that uses 6


complex objects.

Lecture 5
32 Explain class methods and self object with examples. 5

33 Write a short note on Special class methods- __init__() and __del__() ? 6

34 With the help of example explain the concept of Instance Methods , 6


classmethods and staticmethods.

35 Explain the concept of class methods and self object with suitable example. 6

36 With the help of an example explain the significance of the __init__( ) 6


method.

37 What does the self argument signify in the class methods? 6

38 Explain how to create a method in a class with example 5

Lecture 6
39 What is class method? Give proper syntax for explanation 5

40 Differentiate between class variable and instance variable or what are the 6
types of variables in class.

41 Explain __init__() method with example. 6

42 Explain Class variables and Object Variables using example. 6

43 Explain with example: defining a class and creating of object. 5

44 Explain the concept of class variable and object variable with suitable 5

Prof. S. B. Patil, Mechanical Engineering Dept, SIT Lonavala. 40


example.

Lecture 7
45 Explain the concept of public and private members using proper python 6
program.

46 Write a program to deposit or withdraw money in a bank account. 6

47 What are class members? How can you access them? 6

48 Differentiate Between public and private variables. 6

Lecture 8
49 Explain/Differentiate between public and private variables? 6

50 What is the result of the below lines of code?class Test(object): def 5


__init__(self): self.x = 1 t = Test()print t.xprint t.xprint t.xprint t.x

51 write a python program with class employee that keeps a track of the numbr 6
of employees in an organization and also stores their name, designation and
salary details

52 Write a python program to create a class Student to store name and 6


marks.use list to store the marks in three subjects

Prof. S. B. Patil, Mechanical Engineering Dept, SIT Lonavala. 41

You might also like