DEV Community

Punitha
Punitha

Posted on

Python OOP

What is OOP?

  • OOP stands for Object-Oriented Programming.
  • It is a programming approach where we organize programs using classes and objects.

Why Do We Use OOP?

  • Organize code
  • Reuse code
  • Reduce code duplication
  • Maintain large programs easily
  • Represent real-world entities
  • Improve code structure

What is a Class?

  • A class is a blueprint or template used to create objects.
class Student:
    pass
Enter fullscreen mode Exit fullscreen mode
  • Student → Class

What is an Object?

  • An object is an instance of a class.
class Student:
    pass
student1 = Student()
Enter fullscreen mode Exit fullscreen mode
  • Student → Class
  • student1 → Object

Class vs Object

Class                             Object

Blueprint                     Real instance
Defines structure             Uses that structure
Logical entity                    Physical/runtime entity
Example: Student              Example: student1
Enter fullscreen mode Exit fullscreen mode

Creating a Class

  • We use the class keyword.
class Student:
    name = "Punitha"
    age = 22
Enter fullscreen mode Exit fullscreen mode
  • Student → Class
  • name → Attribute
  • age → Attribute

Creating an Object

  • We create an object by calling the class.
class Student:
    name = "Punitha"
    age = 22
student1 = Student()
print(student1.name)
print(student1.age)
Enter fullscreen mode Exit fullscreen mode

Output:
Punitha
22

Top comments (0)