Spaces:
Sleeping
Sleeping
Download examples/python_classes.py from fariasultanacodes/isolated-sandbox: direct link, hf CLI and curl.
- Browser
- Download file 1.21 kB
-
https://huggingface.co/spaces/fariasultanacodes/isolated-sandbox/resolve/main/examples/python_classes.py
- Command line
-
hf download hf://spaces/fariasultanacodes/isolated-sandbox/examples/python_classes.py
-
curl -L -o python_classes.py https://huggingface.co/spaces/fariasultanacodes/isolated-sandbox/resolve/main/examples/python_classes.py
1.21 kB
| # Python Example: Classes and Object-Oriented Programming | |
| class BankAccount: | |
| def __init__(self, owner, balance=0): | |
| self.owner = owner | |
| self.balance = balance | |
| self.transactions = [] | |
| def deposit(self, amount): | |
| if amount > 0: | |
| self.balance += amount | |
| self.transactions.append(f"Deposited: ${amount}") | |
| return True | |
| return False | |
| def withdraw(self, amount): | |
| if amount <= self.balance and amount > 0: | |
| self.balance -= amount | |
| self.transactions.append(f"Withdrew: ${amount}") | |
| return True | |
| return False | |
| def get_balance(self): | |
| return f"${self.balance}" | |
| def show_transactions(self): | |
| print(f"Transaction history for {self.owner}:") | |
| for transaction in self.transactions: | |
| print(f" - {transaction}") | |
| # Create account | |
| account = BankAccount("John Doe", 1000) | |
| print(f"Account created for {account.owner}") | |
| print(f"Initial balance: {account.get_balance()}") | |
| # Perform transactions | |
| account.deposit(500) | |
| account.withdraw(200) | |
| account.deposit(100) | |
| print(f"Current balance: {account.get_balance()}") | |
| account.show_transactions() |