Skip to content

Latest commit

 

History

5 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 

Repository files navigation

Python OOP Practice — Self, Constructor, Class & Instance Attributes

This repository contains beginner-friendly practice exercises covering the core building blocks of Object-Oriented Programming (OOP) in Python: classes, constructors (__init__), self, and instance attributes.

📄 File

  • why.py — Contains five independent practice programs, each demonstrating a different OOP concept.

📚 Topics Covered

1. Basic Class (Car)

Demonstrates how to define a class, initialize objects using a constructor, and create multiple independent instances.

2. Instance Attributes (BankAccount)

Simulates a simple banking system with deposit and withdraw methods, including a conditional check to prevent overdrawing the balance.

3. Multiple Instances (Book)

Shows how to create several objects of the same class, store them in a list, and iterate over them using a for loop.

4. Self-Modifying Methods (Counter)

Demonstrates how instance attributes can be updated over time using methods like increment() and reset().

5. Real-World Mini Challenge (Rectangle)

Covers methods that return calculated values (area() and perimeter()) based on instance attributes.

▶️ How to Run

Make sure you have Python 3 installed, then run:

python why.py

🖥️ Sample Output

bmw
X5
2022
porshe
X3
2021
------------------------------
500
Insufficient balance
500
------------------------------
aby hayat by umera ahmad has 3000 pages.
naval by nimra has 7000 pages.
bakt by mehru has 3000 pages.
------------------------------
5
0
------------------------------
8
12
18
18

🎯 Learning Outcomes

By working through this file, you will understand:

  • How to define a class and create objects (instances) from it
  • The role of the __init__ constructor in initializing object state
  • How self refers to the specific instance a method is called on
  • The difference between instance attributes and shared state
  • How to use conditionals inside methods to control object behavior
  • How to store multiple objects in a list and process them with a loop
  • How to write methods that return computed values

About

No description or website provided.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages