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

FlyFeditv

📘 Introduction In this project, you’ll create a command-line Expense Tracker that allows users to: ✅ Add expenses with category, amount, and description✅ View total spending and category breakdown✅ Automatically…

Read more

NetCinFly

📘 Introduction In this project, you’ll develop a Student Grades Analyzer in Python that can: This project helps you strengthen your skills with lists, dictionaries, loops, and data analysis logic….

Read more

Eagle_Pro

🌐 Introduction In this project, you’ll create a Web Scraper App in Python that extracts quotes, authors, and tags from a live website.You’ll use the BeautifulSoup and Requests libraries to…

Read more

Cobra_Pro

🌦️ Introduction In this project, you’ll build a Weather App in Python that retrieves live weather information from an online API.You’ll learn how to work with HTTP requests, JSON data,…

Read more

Show7-Pro

Concepts Covered: 🎯 Objective: Create a simple, text-based contact book application that allows users to: 💡 Code (contact_book.py): 🧠 Example Usage Console Output Example: 💾 Notes ⬇️ Download Show7-Pro تحميل…

Read more

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