BankAccount Class

Write a Python program to create a class BankAccount with attributes for account_number and balance. Write methods deposit, withdraw, and check_balance to deposit an amount, withdraw an amount, and check the current balance, respectively.

Example 1:

Input: account = BankAccount(123456789, 5000), account.deposit(2000), account.withdraw(1500)

Output: Balance: 5500

Example 2:

Input: account = BankAccount(987654321, 10000), account.withdraw(3000), account.check_balance()

Output: Balance: 7000

Define the class BankAccount and initialize account_number and balance in the __init__ function. Then, create the methods deposit, withdraw, and check_balance to handle the operations.

class BankAccount:
    def __init__(self, account_number, balance):
        self.account_number = account_number
        self.balance = balance
    
    def deposit(self, amount):
        self.balance += amount
    
    def withdraw(self, amount):
        if amount <= self.balance:
            self.balance -= amount
        else:
            print('Insufficient balance')
    
    def check_balance(self):
        return self.balance

account = BankAccount(123456789, 5000)
account.deposit(2000)
account.withdraw(1500)
print('Balance:', account.check_balance())  # Output: Balance: 5500

account = BankAccount(987654321, 10000)
account.withdraw(3000)
print('Balance:', account.check_balance())  # Output: Balance: 7000

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!