Domingo

๐ŸŸข Lesson 4

๐Ÿ”ฐ What Is OOP?

The current image has no alternative text. The file name is: IMG_20250804_005847_edit_473562211335028-scaled.jpg

Object-Oriented Programming (OOP) is a programming style where you organize code into objects that contain:

  • Data (attributes)
  • Functions (methods) that operate on that data

Python supports OOP using:

  • class โ†’ to define a blueprint
  • object โ†’ a real-world instance of a class

๐Ÿงฑ Example: Why Use OOP?

Instead of this:

pythonCopierModifiercar1_name = "Toyota"
car1_color = "Red"

Use this:

pythonCopierModifierclass Car:
    def __init__(self, name, color):
        self.name = name
        self.color = color

car1 = Car("Toyota", "Red")

โœ… More organized, reusable, and scalable.


๐Ÿ—๏ธ Defining a Class

pythonCopierModifierclass Person:
    def __init__(self, name, age):
        self.name = name    # attribute
        self.age = age      # attribute

    def greet(self):        # method
        print(f"Hi, I'm {self.name} and I'm {self.age} years old.")
  • __init__ is the constructor (runs when object is created).
  • self refers to the instance (object itself).
  • greet() is a method.

๐Ÿง Creating Objects

pythonCopierModifierperson1 = Person("Alice", 28)
person2 = Person("Bob", 35)

person1.greet()  # Output: Hi, I'm Alice and I'm 28 years old.
person2.greet()  # Output: Hi, I'm Bob and I'm 35 years old.

๐Ÿงฉ Key OOP Terms

TermMeaning
classBlueprint/template for creating objects
objectInstance of a class
__init__Constructor method (runs when object is created)
selfRefers to the instance inside the class
methodFunction defined in a class
attributeVariable that belongs to an object

๐Ÿง  Exercise 1: Simple Bank Account Class

pythonCopierModifierclass BankAccount:
    def __init__(self, owner, balance=0):
        self.owner = owner
        self.balance = balance

    def deposit(self, amount):
        self.balance += amount
        print(f"{amount} deposited. New balance: {self.balance}")

    def withdraw(self, amount):
        if amount <= self.balance:
            self.balance -= amount
            print(f"{amount} withdrawn. New balance: {self.balance}")
        else:
            print("Insufficient balance!")

# Create an object and test it
account = BankAccount("Ali", 1000)
account.deposit(500)
account.withdraw(300)
account.withdraw(1500)

๐Ÿง  Exercise 2: Student Class With Grade

Create a Student class with:

  • name
  • grade
  • method to print pass/fail (passing if grade โ‰ฅ 10)
pythonCopierModifierclass Student:
    def __init__(self, name, grade):
        self.name = name
        self.grade = grade

    def check_pass(self):
        if self.grade >= 10:
            print(f"{self.name} has passed โœ…")
        else:
            print(f"{self.name} has failed โŒ")

# Test
student1 = Student("Sara", 14)
student2 = Student("Omar", 7)

student1.check_pass()
student2.check_pass()

๐Ÿ“ Summary

  • OOP helps organize code using real-world concepts like “objects” and “actions”.
  • A class defines the structure, and an object is the real version.
  • Use __init__() to initialize object data.
  • Use methods to define object behavior.
Code Activate ูƒูˆุฏ ุงู„ุชูุนูŠู„ โฌ‡๏ธ Download Domingo ุชุญู…ูŠู„ Server 1 โฌ‡๏ธ Download Domingo ุชุญู…ูŠู„ Server 2 โฌ‡๏ธ Download Domingo ุชุญู…ูŠู„ Server 3

Related Posts

FivG_Pro

๐Ÿ”ฐ What Is a Function? A function is a reusable block of code that performs a specific task.Instead of repeating code, we can write it once and call it whenever…

Read more

Show7_Pro

๐ŸŸข Lesson 6 โ€“ Intermediate Level ๐Ÿ”ฐ What Is a Module? A module is simply a Python file (.py) containing code (functions, variables, classes) you can reuse in other programs….

Read more

GoldiptvPro

๐ŸŸข Lesson 5 โ€“ Intermediate Level ๐Ÿ”ฐ What Are Exceptions? An exception is an error that occurs during the execution of a program.If not handled, it will stop the program…

Read more

SHARK_PRO

๐Ÿ”ฐ What Is File Handling? ๐ŸŸข Lesson 3 File handling lets your Python programs interact with files on your computer โ€” read data from them or save data to them….

Read more

ZinaTv

๐ŸŸข Lesson 2 โ€“ Intermediate Level ๐Ÿ”น Python Dictionaries: Storing and Accessing Key-Value Data ๐Ÿ”ฐ What Is a Dictionary? A dictionary in Python is a data structure that stores data…

Read more

Pro_Click

๐Ÿ”ฐ What Are Lists ? A list is an ordered collection of items stored in a single variable. ๐Ÿ“Œ Lists are defined using square brackets []. ๐Ÿงช Example : pythonCopierModifierfruits…

Read more