{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Problem 1\n", "\n", "Create a Python class called `BasicClass` that just contains a static variable called `theVariable`. Assign `theVariable` a value of `1`." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "class BasicClass():\n", " theVariable = 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Problem 2\n", "\n", "Given the following `ExampleClass` class, create an instance of the class and reference each of its attributes using the dot operator." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "class ExampleClass:\n", " age = 24\n", " name = 'John Doe'\n", " location = 'Toronto, Ontario'" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "24\n", "John Doe\n", "Toronto, Ontario\n" ] } ], "source": [ "##solution goes here\n", "\n", "example_instance = ExampleClass()\n", "print(example_instance.age)\n", "print(example_instance.name)\n", "print(example_instance.location)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Problem 3\n", "\n", "Create a Python class called `BirthdayBoy` that takes in two variables:\n", "* A string called `name`\n", "* An integer called `age`\n", "\n", "Do not forget your `init` function!\n", "\n", "Within `BirthdayBoy`, create a method called `birthday` that increases the value of `age` by 1.\n", "\n", "Once this class has been created, create an instance of the class then call the `birthday` method on that class to increase its age. Check that the age has been appropriately increased by the `birthday` method by referencing it using the dot operator (for example, `instance.age`). " ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "25" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "class BirthdayBoy:\n", " def __init__(self, name, age):\n", " self.name = name\n", " self.age = age\n", " \n", " def birthday(self):\n", " self.age += 1\n", " \n", "me = BirthdayBoy('nick', 24)\n", "me.birthday()\n", "me.age" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Problem 4\n", "\n", "Create a Python class called `Salesperson` that has the following characteristics:\n", "* It has dynamic variables called `firstName` and `lastName` that provide the salesperson's first and last names. (Don't forget about your `init` function!)\n", "* It has a static variable (whose value starts at `0`) called `sales` that tracks the salesperson's sales over time. \n", "* It has a method called `makeSale` that takes in an integer, and adds that integer to `sales`.\n", "* It has a method called `salesReport` that prints a string saying `'My total sales are {sales}!'`\n", "\n", "Once this is done, create a `Salesperson` instance with `firstName = 'Nick'` and `lastName = 'McCullum`. Have the salesperson make a sale worth `500` and then run the `salesReport` method. \n", "\n", "This `Salesperson` example is an excellent use case for the `Class` structure in Python - you can have multiple \n", "instances (salespeople) with the same characteristics and can track them separately. " ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "class Salesperson:\n", " def __init__(self, firstName, lastName):\n", " self.firstName = firstName\n", " self.lastName = lastName\n", " \n", " sales = 0\n", " \n", " def makeSale(self, saleValue):\n", " self.sales += saleValue\n", " \n", " def salesReport(self):\n", " print(f'My total sales are {self.sales}!')\n", " " ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "My total sales are 600!\n" ] } ], "source": [ "me = Salesperson('Nick','McCullum')\n", "me.makeSale(600)\n", "me.salesReport()" ] } ], "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.7.6" } }, "nbformat": 4, "nbformat_minor": 4 }