diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a54ae2b --- /dev/null +++ b/.gitignore @@ -0,0 +1,220 @@ +# Created by https://www.toptal.com/developers/gitignore/api/macos,python,jupyternotebooks +# Edit at https://www.toptal.com/developers/gitignore?templates=macos,python,jupyternotebooks + +### JupyterNotebooks ### +# gitignore template for Jupyter Notebooks +# website: http://jupyter.org/ + +.ipynb_checkpoints +*/.ipynb_checkpoints/* + +# IPython +profile_default/ +ipython_config.py + +# Remove previous ipynb_checkpoints +# git rm -r .ipynb_checkpoints/ + +### macOS ### +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +### macOS Patch ### +# iCloud generated files +*.icloud + +### Python ### +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook + +# IPython + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide +.pdm.toml + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ + +### Python Patch ### +# Poetry local configuration file - https://python-poetry.org/docs/configuration/#local-configuration +poetry.toml + +# ruff +.ruff_cache/ + +# LSP config files +pyrightconfig.json + +# End of https://www.toptal.com/developers/gitignore/api/macos,python,jupyternotebooks \ No newline at end of file diff --git a/lab-python-oop.ipynb b/lab-python-oop.ipynb index c13bc58..dc8280b 100644 --- a/lab-python-oop.ipynb +++ b/lab-python-oop.ipynb @@ -56,21 +56,51 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "id": "21625526-3fae-4c55-bab5-f91940070681", "metadata": {}, "outputs": [], "source": [ - "# your code goes here\n", - "\n" + "class BankAccount:\n", + " account_count = 0\n", + " \n", + " def __init__(self, balance=0):\n", + " BankAccount.account_count += 1\n", + " self.account_number = BankAccount.account_count\n", + " self.balance = balance\n", + "\n", + " def deposit(self, amount):\n", + " self.balance += amount\n", + "\n", + " def withdraw(self, amount):\n", + " self.balance -= amount\n", + "\n", + " def get_balance(self):\n", + " return self.balance\n", + "\n", + " def get_account_number(self):\n", + " return self.account_number" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 73, "id": "ee789466-d4cf-4dd8-b742-6863d42c3e5c", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Account 1 balance: 1000\n", + "Account 1 number: 1\n", + "Account 2 balance: 500\n", + "Account 2 number: 2\n", + "Account 1 balance after transactions: 1300\n", + "Account 2 balance after transactions: -100\n" + ] + } + ], "source": [ "# Testing the BankAccount class\n", "# Creating two instances of the BankAccount class with initial balances of 1000 and 500\n", @@ -117,12 +147,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "id": "4f8848b5-05d3-4259-9e24-914537926778", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "class SavingsAccount(BankAccount):\n", + " def __init__(self, balance=0, interest_rate=0.01):\n", + " super().__init__(balance)\n", + " self.interest_rate = interest_rate\n", + "\n", + " def add_interest(self): \n", + " self.balance = self.balance + (self.balance * self.interest_rate)\n", + "\n", + " def get_interest_rate(self):\n", + " return self.interest_rate" ] }, { @@ -151,12 +190,62 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "id": "bccc7f6d-d58c-4909-9314-aaf4afc1cd30", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "account3s = SavingsAccount(100, 0.02)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "5390c05a-0619-4161-8480-7b418e8ff906", + "metadata": {}, + "outputs": [], + "source": [ + "account3s.deposit(50)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "e5c3295d-2779-48ab-9caa-f5e19cde94b3", + "metadata": {}, + "outputs": [], + "source": [ + "account3s.withdraw(25)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "a4a058be-4fd0-42e2-9775-72ed4af50e45", + "metadata": {}, + "outputs": [], + "source": [ + "account3s.add_interest()" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "d6a974b4-d9ca-464a-a24f-8c6598c7e0e0", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Balance: 127.5\n", + "Interest rate: 0.02\n" + ] + } + ], + "source": [ + "print(f\"Balance: {account3s.balance}\")\n", + "print(f\"Interest rate: {account3s.interest_rate}\")" ] }, { @@ -189,12 +278,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "id": "3c883c6e-3cb8-4043-92d3-12409668a28e", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "class CheckingAccount(BankAccount):\n", + " def __init__(self, balance=0, transaction_fee=1):\n", + " super().__init__(balance)\n", + " self.transaction_fee = transaction_fee\n", + " self.transaction_count = 0\n", + "\n", + " def deduct_fees(self):\n", + " fees = self.transaction_fee * self.transaction_count\n", + " if self.balance >= fees:\n", + " self.balance -= fees\n", + " print(f\"Transaction fees of {fees}$ have been deducted from your account balance.\")\n", + " self.transaction_count = 0\n", + " else:\n", + " print(\"Insufficient balance\")\n", + "\n", + " def reset_transactions(self):\n", + " self.transaction_count = 0\n", + "\n", + " def get_transaction_count(self):\n", + " return self.transaction_count\n", + "\n", + " # Overide BankAccount functions to count transactions:\n", + " def deposit(self, amount):\n", + " self.balance += amount\n", + " self.transaction_count += 1\n", + "\n", + " def withdraw(self, amount):\n", + " self.balance -= amount\n", + " self.transaction_count += 1" ] }, { @@ -234,20 +351,168 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 81, "id": "faa5b148-c11b-4be0-b810-de8a7da81451", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "account4 = CheckingAccount(500, 2)" + ] + }, + { + "cell_type": "code", + "execution_count": 82, + "id": "9f86c523-b12c-47e4-98e2-c6dccba94ebc", + "metadata": {}, + "outputs": [], + "source": [ + "account4.deposit(100)" + ] + }, + { + "cell_type": "code", + "execution_count": 83, + "id": "ffaa0427-fbc5-44f6-90b5-f36ff445c28a", + "metadata": {}, + "outputs": [], + "source": [ + "account4.withdraw(50)" + ] + }, + { + "cell_type": "code", + "execution_count": 84, + "id": "f0f0956c-1175-46a1-a04f-75103e26e872", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Transaction fees of 4$ have been deducted from your account balance.\n" + ] + } + ], + "source": [ + "account4.deduct_fees()" + ] + }, + { + "cell_type": "code", + "execution_count": 86, + "id": "ff156bb2-5057-4a18-bc84-9aa43f5554a7", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Current balance: 546\n" + ] + } + ], + "source": [ + "print(f\"Current balance: {account4.get_balance()}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 87, + "id": "a7e9c64b-8220-43d6-a7b3-2160d4f1d1f6", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Transaction count: 0\n" + ] + } + ], + "source": [ + "print(f\"Transaction count: {account4.get_transaction_count()}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 88, + "id": "4292d68e-a5c3-4383-a4b9-4d4923058824", + "metadata": {}, + "outputs": [], + "source": [ + "account4.deposit(200)" + ] + }, + { + "cell_type": "code", + "execution_count": 89, + "id": "140f693a-8385-4e65-b519-822e0bcc9bf0", + "metadata": {}, + "outputs": [], + "source": [ + "account4.withdraw(75)" + ] + }, + { + "cell_type": "code", + "execution_count": 90, + "id": "c8385a13-7dfb-46a7-b950-3a2792a1a0af", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Transaction fees of 4$ have been deducted from your account balance.\n" + ] + } + ], + "source": [ + "account4.deduct_fees()" + ] + }, + { + "cell_type": "code", + "execution_count": 91, + "id": "de9cfe7b-4af3-4727-9072-8e8713667dbf", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Current balance: 667\n" + ] + } + ], + "source": [ + "print(f\"Current balance: {account4.get_balance()}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 92, + "id": "165cfab6-cd2d-42f1-9403-9a6464f4a269", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Transaction count: 0\n" + ] + } + ], + "source": [ + "print(f\"Transaction count: {account4.get_transaction_count()}\")" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python [conda env:base] *", "language": "python", - "name": "python3" + "name": "conda-base-py" }, "language_info": { "codemirror_mode": { @@ -259,7 +524,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.7" } }, "nbformat": 4,