Object-Oriented Programming in Python

 

 Object-Oriented Programming in Python

By: Bindeshwar Singh Kushwaha
Institute: PostNetwork Academy


What is Object-Oriented Programming (OOP)?

  • OOP is a programming paradigm.
  • It is based on the concept of “objects”.
  • Helps model real-world entities like BankAccount, Student, Car.
  • Makes code more organized, reusable, and easier to maintain.

Class in OOP

  • A class is a blueprint or template.
  • It defines attributes (data) and methods (functions).
  • Example: BankAccount, Student, Car.

Object in OOP

  • An object is an instance of a class.
  • It has its own data and can use class methods.
  • Think of a class as a blueprint, and the object as the house.

Class Declaration

class BankAccount:
    """Class to simulate bank account"""

Explanation: Defines a class to simulate a basic bank account.

Constructor Method

def __init__(self, password):
    self.balance = 0
    self.password = password.strip()

Explanation: Initializes the balance and stores the password securely.

Deposit Method

def deposit(self, amount):
    self.balance += amount
    return self.balance

Explanation: Adds amount to the balance and returns updated balance.

Withdraw Method

def withdraw(self, amount):
    user_password = input("Enter password to withdraw: ").strip()
    if user_password != self.password:
        return "Incorrect password. Withdrawal denied."
    if amount > self.balance:
        return "Insufficient Balance"
    self.balance = self.balance - amount
    return self.balance

Explanation: Allows withdrawal after verifying password and checking balance.

Account Creation

ac101 = BankAccount(password="1234")

Explanation: Creates an account object with the password “1234”.

Main Menu Loop

while True:
    print("\n==== Bank Menu ====")
    print("1. Deposit")
    print("2. Withdraw")
    print("3. Check Balance")
    print("4. Exit")
    choice = input("Enter your choice (1-4): ").strip()

Explanation: Displays a menu and runs until the user chooses to exit.

Deposit Option

if choice == "1":
    amount = float(input("Enter amount to deposit: "))
    ac101.deposit(amount)
    print("Amount deposited. Current balance:", ac101.balance)

Explanation: Takes deposit amount and updates the account balance.

Withdraw Option

elif choice == "2":
    amount = float(input("Enter amount to withdraw: "))
    result = ac101.withdraw(amount)
    print("Withdrawal result:", result)
    print("Balance after withdrawal:", ac101.balance)

Explanation: Takes withdrawal amount, verifies password, and processes the transaction.

Check Balance / Exit

elif choice == "3":
    print("Current balance:", ac101.balance)
elif choice == "4":
    print("Thank you for banking with us.")
    break
else:
    print("Invalid choice. Please select from 1 to 4.")

Explanation: Option 3 displays balance, option 4 exits, other inputs show error.

Video


PDF
oopsbank

🔗 Reach PostNetwork Academy

🙏 Thank You!

©Postnetwork-All rights reserved.