0% found this document useful (0 votes)
3 views5 pages

Python Programming

The document provides an overview of object-oriented programming principles in Python, emphasizing classes, objects, attributes, methods, and key concepts such as inheritance, encapsulation, and polymorphism. It explains how to create classes, define attributes and methods, and instantiate objects, along with examples like employee and book classes. Additionally, it discusses the importance of instance methods in manipulating object states and behaviors.

Uploaded by

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

Python Programming

The document provides an overview of object-oriented programming principles in Python, emphasizing classes, objects, attributes, methods, and key concepts such as inheritance, encapsulation, and polymorphism. It explains how to create classes, define attributes and methods, and instantiate objects, along with examples like employee and book classes. Additionally, it discusses the importance of instance methods in manipulating object states and behaviors.

Uploaded by

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

TUTORIAL-1

CSE3011-PYTHON PROGRAMMING
NAME-SARVESHA SINHA
REGISTRATION NUMBER-22BCE10091

1. EXPLAIN THE PRINCIPLES OF OBJECT- ORIENTED PROGRAMMING IN


PYTHON.
Python supports object-oriented programming along with other programming styles. Object-
oriented programming is a way of designing and organizing code which involves creating
blueprints in order to manage and create data.
Object-oriented programming has some advantages over other design patterns. It is relatively
faster and cheaper, with better software maintainability. This leads to higher-quality software,
which is also extensible with new methods and attributes.

The main principles of object-oriented programming in python are as follows-


a) CLASSES AND OBJECTS-

Class- It defines a data structure that includes attributes (fields) and methods (functions).
A class is a user-defined blueprint or prototype from which objects are created.

Object-An object is an instance of a class. It is an entity created from the class blueprint,
possessing the attributes and behaviors defined in the class.

Attributes are variables that store data. They represent the characteristics or properties of
an object. They can be either class attributes (shared by all instances) or instance
attributes (specific to each instance).
Methods are functions defined within a class. They represent the actions or behaviors
associated with the objects created from the class.
The __init__ method is a special method in a class that gets called when an object is
created. It is used to initialize the attributes of the object. We also have class and instance
variables, class variables are shared among all instances of a class, while instance
variables are specific to each instance.
2. CLASSES IN PYTHON WITH EXAMPLES.
In Python, a class is a code template for creating objects. Objects have member variables and
have behavior associated with them in the form of methods.

Key properties-
a) Class definition-A class is defined using the class keyword followed by the class
name.
b) Attributes-Attributes are variables that store data. They define the properties of the
class. Attributes can be either class attributes or instance attributes.

c) Methods-Methods are functions defined within a class. They define the behavior of
the class and operate on the attributes of instances.
d) Constructor- The __init__ method is a special method in Python classes. It gets called
when an object is created and is used for initializing the object's attributes.

e) Instance creation-Instances of a class are created by calling the class as if it were a


function. The __init__ method is automatically called during instance creation to
initialize attributes.
f) Accessing attributes and methods- Attributes and methods of an object are accessed
using dot notation.
g) Inheritance-Inheritance is a feature that allows a class to inherit attributes and
methods from another class.

h) Encapsulation-Encapsulation involves bundling the data (attributes) and methods that


operate on the data within a single unit (class)
i) Polymorphism-Polymorphism allows objects of different classes to be treated as
objects of a common base class.
Examples-
1. We can have a class representing an employee with attributes like name, position, and
methods like work.
2. We can have a class representing a book with attributes like title, author, and methods
like get_info.

CODE-
OUTPUT-

3. CREATING CLASSES WITH EXAMPLE.

Creating classes in Python involves several steps, including defining the class, adding
attributes and methods, and creating instances of the class.

1. Defining the class- The “class” keyword is used in order to define a new class and
then a name is assigned to it.
2. Adding attributes- Attributes are variables that store data within the class. We can add
class attributes for all instances as well as instance attributes defined for a particular
instance.
3. Adding Methods-Methods are functions defined within the class. They perform
actions or operations associated with the class.
4. Creating Instances-Instances of a class are created by calling the class as if it were a
function. The __init__ method (constructor) is automatically called during instance
creation to initialize attributes.
5. Accessing attributes and methods-Attributes and methods of an object are accessed
using dot notation.

Examples-
a) We can create a class called rectangle and with attributes like length, width and
methods such as calculate_area and calculate_perimeter. We can then access the
attributes and call the methods.

CODE-

OUTPUT-
b) We can create a class called BankAccount with attributes such as balance and
methods such as withdraw and deposit. We can then access the attributes and call the
methods.

CODE-

OUTPUT-

4.EXPLAIN THE INSTANCE METHODS.Instance methods are an essential part of object-


oriented programming in Python. They allow us to define behavior associated with instances of a
class and provide a way to interact with and manipulate the state of objects. Instance methods
can access both instance and class variables, providing flexibility in designing classes and their
behavior

You might also like