0% found this document useful (0 votes)
0 views9 pages

Unit 5 - OOPs in Python

The document provides supplementary learning material for a course on Python for Data Science, focusing on Object Oriented Programming (OOP) concepts. It contrasts OOP with Procedural Programming, detailing features such as classes, objects, inheritance, polymorphism, and encapsulation. Additionally, it includes examples and a mini project to illustrate the application of these concepts in Python.

Uploaded by

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

Unit 5 - OOPs in Python

The document provides supplementary learning material for a course on Python for Data Science, focusing on Object Oriented Programming (OOP) concepts. It contrasts OOP with Procedural Programming, detailing features such as classes, objects, inheritance, polymorphism, and encapsulation. Additionally, it includes examples and a mini project to illustrate the application of these concepts in Python.

Uploaded by

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

Supplementary Learning Material

[email protected]
J732KOTCSB

Program: MCA
Specialization: Data Science
Semester: 2
Course Name: Python for Data Science
Course Code: 21VMT5S204
Unit Name: OOPs in Python

Proprietary content. All rights reserved. Unauthorized use or distribution prohibited.

This file is meant for personal use by [email protected] only.


Sharing or publishing the contents in part or full is liable for legal action.
Unit 5:
OOPs IN PYTHON
Overview:
All programming languages are part of a certain paradigm. One paradigm
might lay emphasis on step by step execution or a different approach over the
other. For instance, Java follows Procedural Programming, and Python follows
Object Oriented Programming. Even though they are two languages, their way
of approaching situations is different.

Learning Outcomes:
- Procedural Programming.
- Object Oriented Programming
- OOPs vs Procedural Programming
- Features of OOPs
- Classes:
[email protected]
J732KOTCSB
 Syntax of classes
 Example
- Objects
 Syntax of objects
 Example
- Difference between classes and objects
- Mini project.

Proprietary content. All rights reserved. Unauthorized use or distribution prohibited.

This file is meant for personal use by [email protected] only.


Sharing or publishing the contents in part or full is liable for legal action.
Procedural Programming:
Procedural Programming is a programming paradigm. It breaks down a
program into parts and evaluates it. It carries out the program based on a
certain procedure. There is less abstraction between the code and the
machine. It treats data and modules/operations separately. Programming
languages like Java and C use procedural programming.
When you give your machine the procedure and the steps, it executes
programs in a top-down approach. The reason why Python is not an entirely
procedural programming language is because they don’t protect the data as
well as programs in other paradigms do. It gives emphasis on how the
operation is supposed to be conducted, than the data that has to be sought
after.
For such reasons, Object Oriented Programming is used.

Object Oriented Programming:


[email protected]
J732KOTCSB
Python is a multi-paradigm programming language. It uses classes and objects
in its programs. Object Oriented Programming approaches programming
problems by creating objects. Unlike procedural programming, which is not
based on the real world, it aides in using real-world structures like
polymorphism, encapsulation, etc.
These structures are usually used to increased readability and code efficiency.
OOPs concepts also help us in reusing the code, thus reducing the size of the
program and helps in finding solutions to problems. It is also easy
maintenance. A lot of applications today are developed using an object-
oriented approach.
When we say real-world structures above, we mean how one thing that is
distinct has its own features and attributes. For instance a bike is a distinct
entity and its attributes include size, color, mileage, etc .

Proprietary content. All rights reserved. Unauthorized use or distribution prohibited.

This file is meant for personal use by [email protected] only.


Sharing or publishing the contents in part or full is liable for legal action.
The major difference between both the approaches are:
1. The way they divide a program. OOPs divides a program into objects.
Procedural divides them into functions.
2. OOPs follows a bottom-up approach for its programming problems
unlike procedural which follows top-down approach.
3. In OOPs there are access specifiers which tell us where and in what parts
of a program will a function be accessible. Access specifies are keywords
like private, public, etc. this feature is not available in procedural
programming.
4. OOPs are more secure than procedural programming because they
provide the option of data hiding.
5. OOPs lays emphasis on the data over the procedure. Procedural
programming lays emphasis on the procedure over the data.
6. OOPs uses real-world entities, whereas procedural is based on the
unreal world.

Object Oriented Programming has 5 main features:


[email protected]
J732KOTCSB - Classes
- Objects
- Inheritance
- Polymorphism
- Encapsulation
A few other features include method, instance, dynamic binding, message passing and data
abstraction.

Class:
A class is said to be a blueprint for an object. It is a user-defined data type. It is
a collection of objects. Data members and functions are held within a class.
For example, in a company, we evaluate two departments- HR and Accounting,
and we want to check which department a person works in. We write the
program. If say we enter the employ ID of person XYZ, we need to retrieve the
name of the employee and the department in which he works. For one entity,
it is easy to do so, but for multiple entries, in 1000s, it becomes very difficult.

Proprietary content. All rights reserved. Unauthorized use or distribution prohibited.

This file is meant for personal use by [email protected] only.


Sharing or publishing the contents in part or full is liable for legal action.
In such situations we group entries in something called as a class, which is a
feature of OOPs.
In the above example, the class can be employee_details() and it could contain
the employee ID, name, and the department in which he works. Therefore, we
can say that a class is a blueprint because it contains the objects and attributes
of a function. It helps organise the data which defines the capability of an
object helps a programmer to reuse elements while making new instances of
the class.

Syntax of Class:

class employee_details():

To define a class, you simply write class, which is a keyword and the
className() fillowed by a semicolon. Inside the class you can write multiple
functions that are part of the class. According to access specifiers, the
functions within the class are accessible or inaccessible to rest of the program.
[email protected]
J732KOTCSBInside a class, make sure that everything is properly indented, just like in a
conditional statement.

Example of a class:

Objects:
Unless an object is created inside a class, no memory is allocated to the class.
Instantiation is creating an object of a class. The objector instance contains the
actual information.

Proprietary content. All rights reserved. Unauthorized use or distribution prohibited.

This file is meant for personal use by [email protected] only.


Sharing or publishing the contents in part or full is liable for legal action.
Syntax to create an object

object_name = class_name()

Creating an object of a class:

In the above example, to assign an object to a class, simply equate the object
name to the classname(). To display the value of the string inside
employee_details, you must simply print objectName.variableName.

[email protected]
J732KOTCSB

Difference between an object and class:

Class Object

A class is like a template for object Object is an instance of class.


declaration.
Classes are not allocated memory Objects are allocated memory when
upon creation. they are created.

Proprietary content. All rights reserved. Unauthorized use or distribution prohibited.

This file is meant for personal use by [email protected] only.


Sharing or publishing the contents in part or full is liable for legal action.
Classes are declared once. Objects have to be declared
numerous times as and when
needed.
Classes cannot be manipulated. Objects can be manipulated.
class is a keyword. There is no keyword to create
objects.
Syntax: Syntax:
class class_name(): object_name = class_name()

Class is used to bind all the data into Objects are like variables of class.
a single unit.
It is a logical entity It is a physical entity.
Example: Example:
employee_details is a class employee_id, employee_name,
department.

The above examples are simple and are not usually used in python. For real
world applications, we use the __init__() function.
__init__() function:
[email protected]
J732KOTCSB
All classes have an __init__() function that is executed when the class is
intialised. It is called automatically whenever a class is used to create a new
object.
The __init__() function is similar to constructors. Constructors in java are used
to initialise the state of the object. It contains a set of statements that are
executed when the object is created and is run when the object is instantiated.

Example:

Proprietary content. All rights reserved. Unauthorized use or distribution prohibited.

This file is meant for personal use by [email protected] only.


Sharing or publishing the contents in part or full is liable for legal action.
In the above example, a class is called employee_details. An employee’s name
is created via the object obj. While creating the name, “Yash” is passed as an
argument. This argument will be passed in the __init__ method to initialise the
object. The ‘self’ keyword represents the instance of the class and binds the
attributes with the mentioned arguments. Similarly, many objects of
employee_details class can be created by passing different employee names as
arguments.
The first argument of every class, including __init__ is always a reference to
the current instance of that class. Conventionally, it is always named ‘self’. In
__init__ , self means the newly created object and in other classes, it means
the instance whose method was called. It represents the object which inherits
the properties of the class. One must note, that this is not a compulsion, it can
have any name. however, the first argument in a method is a reference to the
object. For methods, in general, you do not need to include self, however if
you don’t provide self in the __init__ function then you will get an error.

Mini Project:
[email protected]
J732KOTCSB

Example 2: Display 3 book names and their page counts using classes and
objects.

Proprietary content. All rights reserved. Unauthorized use or distribution prohibited.

This file is meant for personal use by [email protected] only.


Sharing or publishing the contents in part or full is liable for legal action.
In the above example, we declare a class called book. Inside the class we
define the __init__() function. We define self and other arguments within
__init__ and then we define the description function which displays the book
name and the page count of the book. We make 3 objects with their respective
names and page counts. Then we call the object.
Book name and page count for the book is created via the object b1, b2, b3.
While creating the book name and page count, (“Gone with the wind”,
“1448”), (“Romeo and Juliet”,”92”), (“Merchant Of Venice”,”173”) are passed
as arguments. These arguments will be passed in the __init__ method to
initialise the object. The ‘self’ keyword represents the instance of the class and
binds the attributes with the mentioned arguments.

[email protected]
J732KOTCSB

Proprietary content. All rights reserved. Unauthorized use or distribution prohibited.

This file is meant for personal use by [email protected] only.


Sharing or publishing the contents in part or full is liable for legal action.

You might also like