{"id":72,"date":"2025-10-21T04:31:59","date_gmt":"2025-10-21T04:31:59","guid":{"rendered":"https:\/\/codetypingpro.com\/?p=72"},"modified":"2025-12-17T07:47:41","modified_gmt":"2025-12-17T07:47:41","slug":"lesson-14-object-oriented-programming-oop-in-python","status":"publish","type":"post","link":"https:\/\/codetypingpro.com\/?p=72","title":{"rendered":"Lesson 14: Object-Oriented Programming (OOP) in Python"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">\ud83c\udfaf <strong>Lesson Objective<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To understand the concepts of <strong>classes, objects, and OOP principles<\/strong> in Python, allowing you to structure programs efficiently using real-world models.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\udde9 <strong>1. What Is Object-Oriented Programming (OOP)?<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">OOP is a programming paradigm that <strong>models real-world entities as objects<\/strong>.<br>It focuses on <strong>data (attributes)<\/strong> and <strong>behavior (methods)<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Key concepts:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Class<\/strong>: Blueprint for creating objects.<\/li>\n\n\n\n<li><strong>Object<\/strong>: Instance of a class.<\/li>\n\n\n\n<li><strong>Attributes<\/strong>: Variables in a class (properties).<\/li>\n\n\n\n<li><strong>Methods<\/strong>: Functions inside a class (behaviors).<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2699\ufe0f <strong>2. Creating a Class and Object<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>class Person:\n    def __init__(self, name, age):  # Constructor\n        self.name = name\n        self.age = age\n\n    def greet(self):\n        print(f\"Hello, my name is {self.name} and I am {self.age} years old.\")\n\n# Create object\np1 = Person(\"Sameer\", 25)\np1.greet()\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Hello, my name is Sameer and I am 25 years old.\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd04 <strong>3. Class Attributes vs Instance Attributes<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>class Dog:\n    species = \"Canine\"  # Class attribute\n\n    def __init__(self, name, age):\n        self.name = name  # Instance attribute\n        self.age = age\n\nd1 = Dog(\"Buddy\", 5)\nd2 = Dog(\"Rocky\", 3)\n\nprint(d1.name, d1.species)\nprint(d2.name, d2.species)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Buddy Canine\nRocky Canine\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\uddf0 <strong>4. Methods and <code>self<\/code><\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>self<\/code> refers to the <strong>current object instance<\/strong>.<\/li>\n\n\n\n<li>Methods define <strong>behaviors<\/strong> of an object.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>class Circle:\n    pi = 3.14159\n\n    def __init__(self, radius):\n        self.radius = radius\n\n    def area(self):\n        return Circle.pi * self.radius ** 2\n\nc = Circle(5)\nprint(\"Area:\", c.area())\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Area: 78.53975\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\uddf1 <strong>5. Inheritance<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Inheritance allows a class to <strong>reuse attributes and methods<\/strong> of another class.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Animal:\n    def __init__(self, name):\n        self.name = name\n\n    def speak(self):\n        print(f\"{self.name} makes a sound\")\n\nclass Dog(Animal):  # Dog inherits from Animal\n    def speak(self):\n        print(f\"{self.name} barks\")\n\nd = Dog(\"Buddy\")\nd.speak()\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Buddy barks\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd04 <strong>6. Encapsulation<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Encapsulation restricts direct access to attributes using <strong>private variables<\/strong> (<code>__name<\/code>).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class BankAccount:\n    def __init__(self, balance):\n        self.__balance = balance  # private attribute\n\n    def deposit(self, amount):\n        self.__balance += amount\n\n    def get_balance(self):\n        return self.__balance\n\nacc = BankAccount(1000)\nacc.deposit(500)\nprint(acc.get_balance())  # 1500\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\uddee <strong>7. Polymorphism<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Polymorphism allows <strong>different classes to have methods with the same name<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Cat:\n    def speak(self):\n        print(\"Meow\")\n\nclass Dog:\n    def speak(self):\n        print(\"Bark\")\n\nanimals = &#91;Cat(), Dog()]\nfor animal in animals:\n    animal.speak()\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Meow\nBark\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83c\udf0d <strong>8. Real-Life Example \u2013 Student Class<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>class Student:\n    def __init__(self, name, marks):\n        self.name = name\n        self.marks = marks\n\n    def average(self):\n        return sum(self.marks) \/ len(self.marks)\n\ns1 = Student(\"Ali\", &#91;80, 90, 85])\ns2 = Student(\"Sara\", &#91;75, 88, 92])\n\nprint(f\"{s1.name}'s Average:\", s1.average())\nprint(f\"{s2.name}'s Average:\", s2.average())\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Ali's Average: 85.0\nSara's Average: 85.0<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\ud83c\udfaf Lesson Objective To understand the concepts of classes, objects, and OOP principles in Python, allowing you to structure programs efficiently using real-world models. \ud83e\udde9 1. What Is Object-Oriented Programming (OOP)? OOP is a programming paradigm that models real-world entities as objects.It focuses on data (attributes) and behavior (methods). Key concepts: \u2699\ufe0f 2. Creating a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,1],"tags":[],"class_list":["post-72","post","type-post","status-publish","format-standard","hentry","category-python-easy-course-outline","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/72","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=72"}],"version-history":[{"count":1,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/72\/revisions"}],"predecessor-version":[{"id":73,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/72\/revisions\/73"}],"wp:attachment":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=72"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=72"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=72"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}