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

OOPS in Pythonn

The document provides an overview of Object-Oriented Programming (OOP) in Python, explaining key concepts such as classes, objects, attributes, and methods. It details the role of constructors and the self parameter, along with examples of class and instance attributes. The document emphasizes the organization and reusability of code through the use of OOP principles.
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)
3 views24 pages

OOPS in Pythonn

The document provides an overview of Object-Oriented Programming (OOP) in Python, explaining key concepts such as classes, objects, attributes, and methods. It details the role of constructors and the self parameter, along with examples of class and instance attributes. The document emphasizes the organization and reusability of code through the use of OOP principles.
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

OOPS IN PYTHON

• What is OOP?
• OOP stands for Object-Oriented Programming.
• Python is an object-oriented language, allowing you to structure
your code using classes and objects for better organization and
reusability.
• Important OOP concepts include class, object, constructor,
encapsulation, inheritance, and polymorphism.
• Classes and Objects are the basic concepts of Object-Oriented
Programming (OOP).
They are used to represent real-world entities and concepts.
A class represents a group of objects.
An object is an instance of a class.
• For example, in a company, Employee is a class.
A particular employee named Priya is an object of the Employee class.
• An object has the following two characteristics:
• -Attribute
• -Behavior
• For example, vehicle is a class, A Car is an object, as it has the
following properties:
• -name, price, color as attributes
• -breaking, acceleration as behavior
class
• A class in Python is a user-defined template used to define a new type
of object. It groups related data and functions together, which makes
the program organized and easy to manage. When we create a class,
we define the structure and properties of real-world entity.
• Classes are created using the class keyword.
• The basic syntax is:
• Class classname:
• Class student:
pass
Objects and Attributes

• An object is a specific instance of a class. It is like a real-world thing


created using the class blueprint. We can create multiple objects
from the same class. Each object can have its own data.
• Attributes are variables defined inside a class that store information
about an object. They define the characteristics of the object.
• These attributes can be accessed using the dot (.) operator.
• Think of a blueprint for a Student. It describes:
• Attributes: name, roll_number, course
Methods: study(), attend_class(), display_details()
• # define a class
• class Student:
• college = "KS College" # class attribute
• class Car:
• color1 = "red" # class attribute
• color2 = "blue" # class attribute

• obj1 = Car() # object


• obj2 = Car() # object

• print(obj1.color1)
• print(obj2.color2)
• red
• blue
Methods

• Methods are functions defined inside a class.


They represent the actions or behaviors that an object can perform.
• For example, in a Student class, methods can be:
• study()
• attend_class()
• display_details()
• These actions describe what a student can do.
• class Student:
• college = "KS College" # class attribute

• def study(self): #class methods


• print("Student is studying in", [Link])

• def attend_class(self): # class methods
• print("Student is attending class at", [Link])

• s1 = Student() # object
• [Link]()
• s1.attend_class()
• Student is studying in KS College
• Student is attending class at KS College
Self parameter
• The self parameter is a reference to the current instance (object) of
the class.
• It is used to access the attributes and methods of that object.
• self is not keyword in python. It is just a naming convention.
• It helps the method know which object's data it should work with.
• It does not have to be named self , you can call it whatever you like,
but it has to be the first parameter of any function in the class
• class faculty:
• def putdata(self): # method1
• [Link] = int(input("enter faculty id"))
• [Link] = input("enter name")
• [Link] = float(input("enter faculty salary"))
• def display(self): # method2
• print("faculty id:", [Link])
• print("faculty name:", [Link])
• print("faculty salary:", [Link])
• a = faculty()
• [Link]()
• [Link]()
Constructor

• A constructor is a special method inside a class that is automatically


called when an object is created.
• It is used to initialize the attributes (properties) of the object.
• It is mainly used to assign values to object properties when the object
is created.
• In Python, the constructor is written as __init__().
• It runs automatically when we create an object from a class.
• class Person:
• def __init__(self, name, age):
• [Link] = name
• [Link] = age

p1 = Person("Ashish", 20)
• print([Link])
• print([Link])
• Ashish 20
• class Student:

• def __init__(self, name, rollno):
• [Link] = name
• [Link] = rollno

• def display(self):
• print("Name:", [Link])
• print("Roll Number:", [Link])

• s1 = Student("Aachal", 101)
• [Link]()
• Name: Aachal
• Roll Number: 101
Attributes and Methods

• Attributes
• Attributes are variables that store data inside a class.
• They represent the properties or characteristics of an object.
• Example:
• name
• price
• Methods
• Methods are functions defined inside a class.
• They represent the actions or behavior of an object.
• Example:
• display()
• study()
• show_price()
• class Fruit:

• def __init__(self, name, price):
• [Link] = name # attribute
• [Link] = price # attribute

• def display(self): # method
• print("Name:", [Link])
• print("Price:", [Link])

• f1 = Fruit("Apple", 120)
• [Link]()
Class Attribute and Instance Attribute

• Class Attribute
• A class attribute is a variable that belongs to the class.
• It is shared by all objects created from that class.
• Every object gets the same value of the class attribute.
• If we change the class attribute value, it changes for all objects
(unless changed separately for one object).
• class Student:
• school = "ABC School" # Class attribute (common for all students)

• # Creating objects
• s1 = Student()
• s2 = Student()

• # Accessing class attribute

• print([Link]) # Output: ABC School


• print([Link]) # Output: ABC School
• class Student:
• school = "ABC School" # Class attribute (common for all students)

# Creating objects
• s1 = Student()
• s2 = Student()

# Accessing class attribute
• [Link]="ks"
• print([Link]) # Output: ks
• print([Link]) # Output: ABC School

• Instance Attribute
• An instance attribute belongs to a specific object.
• It is usually defined inside the __init()__ method.
• Each object can have a different value for instance attributes.
• These attributes are unique for every object created from the class.
• class Student:

• def __init__(self, name):
• [Link] = name # Instance attribute (different for each object)

• # Creating objects with different names


• s1 = Student("Aachal")
• s2 = Student("Riya")

• # Accessing instance attributes


• print([Link]) # Output: Aachal
• print([Link]) # Output: Riya
• class Student:

• school = "ABC School" # Class attribute (common)

• def __init__(self, name):
• [Link] = name # Instance attribute (individual)

• # Creating objects
• s1 = Student("Aachal")
• s2 = Student("Riya")

• # Printing both class and instance attributes


• print([Link], [Link]) # ABC School Aachal
• print([Link], [Link]) # ABC School Riya

You might also like