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

Rapid_tv

🔹 Introduction The Rock, Paper, Scissors Game is a classic hand game that you can easily build in Python. 👉 Rules: 🔹 Code Example 🔹 Example Run 🔹 Concepts Learned…

Read more

VOD-ZalHD

🔹 Introduction The Number Guessing Game is a classic beginner Python project. 🔹 Code Example 🔹 Example Run 🔹 Concepts Learned 🔹 Summary ⬇️ Download VOD-ZalHD تحميل Server 1 ⬇️…

Read more

Delux_pro

🔹 Introduction The To-Do List App is one of the most popular beginner projects. It teaches you how to: 🔹 Code Example 🔹 Features of This App 🔹 Example Run…

Read more

Fam4k

🔰 Project Overview A calculator is one of the simplest yet most effective projects to start with in Python.You will learn: ✍️ Step 1: Plan the Calculator We need: 🖥️…

Read more

Speed_HD+

1. Introduction Data visualization helps us understand patterns and insights in data by converting raw numbers into charts and graphs.In Python, the most popular libraries are: 2. Installing Required Libraries…

Read more

Dauo

1. Introduction Web scraping is the process of extracting data from websites. In Python, we commonly use libraries like requests (to fetch web pages) and BeautifulSoup (to parse and extract…

Read more