0% found this document useful (0 votes)
40 views14 pages

OOP Concepts in Python Training Report

1. The document is a training report submitted by Mohamed Abdulqadr Mohamed to Lovely Professional University on the topic of Object Oriented Programming in python. 2. It introduces the key concepts of OOP in python including classes, objects, methods, inheritance, encapsulation, and polymorphism. 3. The conclusion states that OOP makes programs easier to understand and more efficient by allowing code reuse through shareable classes and securing data through abstraction, while polymorphism allows a common interface for different objects.

Uploaded by

Abukar Hassan
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)
40 views14 pages

OOP Concepts in Python Training Report

1. The document is a training report submitted by Mohamed Abdulqadr Mohamed to Lovely Professional University on the topic of Object Oriented Programming in python. 2. It introduces the key concepts of OOP in python including classes, objects, methods, inheritance, encapsulation, and polymorphism. 3. The conclusion states that OOP makes programs easier to understand and more efficient by allowing code reuse through shareable classes and securing data through abstraction, while polymorphism allows a common interface for different objects.

Uploaded by

Abukar Hassan
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

Object Oriented programming in python

E-Box

A training report

Submitted in partial requirements for the award of degree of

Bachelor of Technology
(computer science and engineering)

Submitted to
LOVELY PROFESSIONALUNIVERSITY

PHAGWARA, PUNJAB

Submitted by:

Name of the student: Mohamed Abdulqadr Mohamed


Registration number: 11801223

1
[Link] TITLE PAGE

1 DECLARATION OF THE STUDENT 3


TRAINING CERTIFICATE FROM ORGANISATION
2 4
ACKNOWLEDGEMENT
3 5
INTRODUCTION OF THE COURSE UNDERTAKEN
4 6
CONTENTS OF THE COURSE UNDERTAKEN
5 7
CONCLUSION
6 14

2
To whom so ever it may concern,

I, Mohamed Abdulqadr Mohamed 11801223, hereby declare


that the work done by me on “Object Oriented Programming in Python”
is a record of original work for the partial fulfilment of the requirements for
the award of the degree, [Link] (CSE).

3
4
ACKNOWLEDGEMENT
I would like to express my deepest appreciation to all those who provided me the possibility
to complete this report. A special gratitude I give to my mentor whose contribution in
stimulating suggestions and encouragement helped me to coordinate my project especially in
writing this report. I express my thanks to my institution Lovely Professional University for
giving me an opportunity to learn this interesting topic. I also convey my regards to my
faculty assistance all through this training named “Object Oriented Programming”. Once
again I would like to thank all my supporters from the core of my heart.

5
Introduction to Object Oriented Programming:

Python is a multi-paradigm programming language. It supports different programming


approaches. One of the finest approaches to solve a programming problem is by creating
objects which is known as Object Oriented Programming (OOP).

An object has two characteristics:

• Attributes
• Behaviour

We can take anything which has a name, age, colour as its attributes and the things which it
does as behaviour.

6
Class:

A class is like a blueprint for the object.

We can think of class as a sketch of a animal with labels. It contains all the details about
name, colour, size etc. Based on this information we can study about that particular animal.
Then that particular animal is called the object.

Here we use the class keyword to define an empty class MyClass. From class, we construct
instances. An instance is a specific object created from a particular class.

Object:

An object (instance) is an instantiation of a class. When class is defined, only the description
for the object is defined. Therefore, no memory or storage is allocated.

The example for object of parrot class can be:

Here, obj is an object of class parrot.


Suppose we have details of parrots. Now, we are going to show how to build the class and
objects of parrots.

7
#program for creating class and object in python

#output:

In the above program, we created a class with the name parrot. Then, we defineattributes.
The attributes are a characteristic of an object.
These attributes are defined inside the init method of the class. It is the initializer
method that is first run as soon as the object is created.
Then, we create instances of the Parrot class. Here, blu and woo are references (value) to our
new objects.

8
We can access the class attribute using class .species . Class attributes are the same for all
instances of a class. Similarly, we access the instance attributes using [Link] and
[Link]. However, instance attributes are different for every instance of a class.

Methods:
Methods are functions defined inside the body of a class. They are used to define the
behaviours of an object.

#creating methods in python:

#output:

In the above program, we define two methods i.e., sing() and dance(). These are called
instance methods because they are called on an instance object i.e., blu.

9
Inheritance:

Inheritance is a way of creating a new class for using details of an existing class without
modifying it. The newly formed class is a derived class (or child class). Similarly, the
existing class is a base class (or parent class).

#use of inheritance in python

10
#output

In the above program, we created two classes i.e. Bird (parent class) and Penguin (child
class). The child class inherits the functions of parent class. We can see this fromthe
swim() method.
Again, the child class modified the behaviour of the parent class. We can see this from the
whoisthis() method. Furthermore, we extend the functions of the parent class, by creating a
new run() method.

Additionally, we use the super() function inside the init () method. This allows us to run the
init () method of the parent class inside the child class.

Encapsulation

Using OOP in Python, we can restrict access to methods and variables. This prevents data
from direct modification which is called encapsulation. In Python, we denote private
attributes using underscore as the prefix i.e., single _ ordouble

11
#data encapsulation in python.

#output

In the above program, we defined a computer class.


We used init () method to store the maximum selling price of computer. We tried to
modify the price. However, we can't change it because Python treats the maxprice() as
private attributes.

As shown, to change the value, we have to use a setter function i.e., setMaxprice() which
takes price as a parameter.

12
Polymorphism
Polymorphism is an ability (in OOP) to use a common interface for multiple forms (data
types).

Suppose, we need to color a shape, there are multiple shape options (rectangle, square,
circle). However we could use the same method to color any shape. This concept is called
Polymorphism.

#Using Polymorphism in Python

13
#Output

Parrot can f l y
Penguin ca n ' t fly

In the above program, we defined two classes parrot and penguin. Each of them have a
common fly() method. However, their functions are different.
To use polymorphism, we created a common interface i.e., flying_test() function that takes
any object and calls the object's fly() method. Thus, when we passed the blu and preggy
objects in the flying_test() function, it ran effectively.

Conclusion
Object-Oriented Programming makes the program easy to understand as well as efficient.
Since the class is sharable, the code can be reused. Data is safe and secure with data
abstraction. Polimorphism allows the same interface for different objects, so programmers
can write efficient code.

***THE END***

14

Common questions

Powered by AI

In Python's Object-Oriented Programming, attributes differentiate between instance attributes and class attributes based on their scope and association. Instance attributes are defined inside the constructor method (__init__) and are unique to each object instance. In contrast, class attributes are shared across all instances of a class and are defined within the class body but outside any methods. Methods inside the class can modify instance attributes (e.g., self.name), but class attributes (e.g., ClassName.attribute) remain consistent unless specifically altered .

Python's Object-Oriented approach facilitates efficient code sharing and reusability primarily through class definitions and inheritance, which allow for encapsulation of functionalities and attributes into reusable templates. This organization means that once a class is defined, it can be instantiated multiple times across different programs without redundant code. Inheritance extends these capabilities by enabling new classes to adopt properties and methods of existing ones, fostering dynamic and collaborative code reuse without refactoring .

Polymorphism significantly impacts the flexibility and maintainability of Python code by allowing multiple object types to be handled through a unified interface. This capability minimizes the necessity for type-specific code blocks and promotes generic programming practices. By using polymorphic methods, developers can craft extensible code structures that accommodate new behavior without altering the existing codebase, thus significantly enhancing maintainability and adaptability to changing requirements .

Object-Oriented Programming in Python ensures data security and abstraction through the use of encapsulation and data hiding. Encapsulation protects object data by defining methods that control access and modification, often using private attributes (prefixed with '_') to prevent unauthorized access. Abstraction, on the other hand, allows developers to present only relevant data and hide unnecessary details, simplifying interface interactions and safeguarding the internal state from direct manipulation .

The '__init__' method in Python classes is a special initializer method that is automatically invoked when a new object is instantiated from a class. It contributes to object instantiation by setting initial state, such as defining and initializing attributes specific to the object instance. By taking initial values as arguments (e.g., name, age), the '__init__' method ensures that each object starts with its own unique state, facilitating the creation of dynamic and tailored object behaviors .

The 'super()' function in Python provides a mechanism to call methods and constructors of parent classes, enhancing the inheritance model by promoting code reusability and reducing redundancy. It allows extended functionality in derived classes without explicitly referencing the base class, thus supporting single and multiple inheritance scenarios. Utilizing 'super()' ensures that the parent class methods are properly executed, maintaining a coherent initialization sequence among class hierarchies .

Method overriding in Python enhances functionality in a child class by allowing it to provide a specific implementation for a method that is already defined in its parent class. This feature helps in refining or extending the original behavior of methods to fit the child's specific needs. For example, a child class can override a method to modify or substitute the functionality originally specified by a parent class, thereby tailoring or improving the response to suit new requirements without changing the parent class's code .

Inheritance in Python allows a new class to use the details and methods of an existing class without modifying it, termed as the base class, thus promoting code reusability. The child class, or derived class, inherits attributes and methods from the parent class, allowing developers to create new functionalities on top of existing ones without rewriting code. This mechanism enables efficient code maintenance and less redundant code .

Polymorphism in Python allows the implementation of a common interface for multiple data types, enabling methods to be used interchangeably across different object types. This is demonstrated by the ability to define a common function, like the 'flying_test(Creature)', which can execute the 'fly()' method for any object passed to it that implements this method. This capability streamlines code by using a unified interface to handle different object interactions .

Encapsulation in Python's Object-Oriented Programming is significant as it restricts access to certain components of an object, thereby enhancing security by preventing accidental or unauthorized modifications. It is implemented by using private attributes denoted with a prefix underscore (e.g., _ or __), and providing controlled access via methods such as getters and setters. This mechanism allows the internal representation of an object to be hidden from the outside world .

You might also like