{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Practice Problems\n", "### Lecture 10\n", "Answer each number in a separate cell\n", "\n", "Rename this notebook with your last name, first initial and the lecture \n", " \n", " ex. Cych_B_10\n", " \n", "Turn-in this notebook on Canvas." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. Classes\n", " - Create a class called Rectangle with width and height (w,h) as attributes\n", " - include a docstring\n", " - define two methods: area and perimeter\n", " - area calculates the area of the rectangle\n", " - perimeter calculates the perimeter of the rectangle\n", " - include a docstring with each method\n", " - Create a new instance of the Rectangle class with width 1 and length 4.\n", " - Create a second instance of the Rectangle class with width 4 and length 4\n", " - Call the method **area( )** on both your first and second Rectangle object. Print the area\n", " - Call the method **perimeter( )** on your second Rectangle object and save the outcome in a new variable. Print the value of the new variable\n", "\n", "2. Classes in modules\n", " - Rename the Rectangle class **Rect** and save it to a module called **Shapes.py**. Now we can import and re-use it whenever we like. \n", " - Do this by:\n", " - copy the class definition code block into your text file window\n", " - save it to this directory using the %%writefile magic command\n", " - import the Rect class into your Practice Problems notebook\n", " - Create a new instance of the Rect class.\n", " - Call the methods **area( )** and **perimeter( )** and print out the output\n", " \n", "3. Copies\n", " - using your **Shapes** module, make an instance of Rectangle called **rec** with a length and width of 2 and 4\n", " - make a copy of **rec** called **copy**\n", " - make a deepcopy of **copy** called **deep**\n", " - change the length of **copy** to 3\n", " - print out rec.length, copy.length, deep.length " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Reminder**: \n", "If you have changed a module and you need to import the changes into the jupyter notebook, then \n", "execute the command: \n", "\n", "**from importlib import reload**\n", "\n", "Then you can use this command to refresh your module in the notebook:\n", "\n", "**reload(yourModuleName)**" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.6.7" } }, "nbformat": 4, "nbformat_minor": 2 }