diff --git a/lectures/chap5/notes/codes/Book.py b/lectures/chap5/notes/codes/Book.py new file mode 100644 index 0000000000000000000000000000000000000000..abc5671d43247f6a912fad5270319334bc21016f --- /dev/null +++ b/lectures/chap5/notes/codes/Book.py @@ -0,0 +1,50 @@ +class Book: + def __init__(self, title=None, edition=None, price=None, currency=None): + # instance attributes + self.title = title + self.authors = [] + self.chapters = [] + self.edition = edition + self.price = price + self.currency = currency + + # Getters and setters for instance attributes + def get_title(self): + return self.title + + def set_title(self, title): + self.title = title + + def get_pages(self): + return self.pages + + def set_pages(self, pages): + self.pages = pages + + def add_author(self,author): + self.authors.append(author) + + def add_chapter(self,chapter): + self.chapters.append(chapter) + + def get_edition(self): + return self.edition + + def set_edition(self, edition): + self.edition = edition + + def get_price(self): + return self.price + + def set_price(self, price): + self.price = price + + def get_currency(self): + return self.currency + + def set_currency(self, currency): + self.currency = currency + + # Magic methods + def __str__(self): + return f'{self.title}, {self.authors}, {self.edition}, {self.price:0.2f} {self.currency}, {self.chapters}' \ No newline at end of file diff --git a/lectures/chap5/notes/codes/book_test.ipynb b/lectures/chap5/notes/codes/book_test.ipynb new file mode 100644 index 0000000000000000000000000000000000000000..7080f93572b5f5d7ff2deb75056a60028ff16099 --- /dev/null +++ b/lectures/chap5/notes/codes/book_test.ipynb @@ -0,0 +1,76 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Starting out with Python, ['Tony Gaddis'], 4th, 739.00 NOK, ['Introduction to Programming', 'Introduction to Python', 'Control Statements', 'Functions', 'Introduction to Programming', 'Introduction to Python', 'Control Statements', 'Functions']\n", + "Starting out with Python, [], 4th, 126.65 USD, []\n", + "Starting out with Python, ['Tony Gaddis'], 4th, 739.00 NOK, ['Introduction to Programming', 'Introduction to Python', 'Control Statements', 'Functions', 'Introduction to Programming', 'Introduction to Python', 'Control Statements', 'Functions']\n", + "Title = Starting out with Python, Auhor = ['Tony Gaddis'], Edition = 4th, Price = 739, Currency = NOK\n", + "Starting out with Python, ['Tony Gaddis'], 5th, 985.60 NOK, ['Introduction to Programming', 'Introduction to Python', 'Control Statements', 'Functions', 'Introduction to Programming', 'Introduction to Python', 'Control Statements', 'Functions']\n" + ] + } + ], + "source": [ + "from Book import Book\n", + "\n", + "\n", + "sowp_akademika = Book(title=\"Starting out with Python\", edition='4th')\n", + "sowp_akademika.set_price(739)\n", + "sowp_akademika.set_currency('NOK')\n", + "sowp_akademika.add_author('Tony Gaddis')\n", + "sowp_akademika.set_pages(896)\n", + "sowp_akademika.add_chapter('Introduction to Programming')\n", + "sowp_akademika.add_chapter('Introduction to Python')\n", + "sowp_akademika.add_chapter('Control Statements')\n", + "sowp_akademika.add_chapter('Functions')\n", + "\n", + "sowp_amazon = Book(title=\"Starting out with Python\", edition='4th')\n", + "sowp_amazon.set_price(126.65)\n", + "sowp_amazon.set_currency('USD')\n", + "sowp_akademika.add_chapter('Introduction to Programming')\n", + "sowp_akademika.add_chapter('Introduction to Python')\n", + "sowp_akademika.add_chapter('Control Statements')\n", + "sowp_akademika.add_chapter('Functions')\n", + "\n", + "print(sowp_akademika)\n", + "print(sowp_amazon)\n", + "\n", + "print(str(sowp_akademika))\n", + "print(repr(sowp_akademika))\n", + "\n", + "sowp_akademika(price=896*1.1, edition='5th')\n", + "\n", + "print(sowp_akademika)\n" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.2" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +}