Skip to content

Instantly share code, notes, and snippets.

@danielle-b
Created July 30, 2019 04:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielle-b/7d5439b5879b22cceb1d54331d1c9540 to your computer and use it in GitHub Desktop.
Save danielle-b/7d5439b5879b22cceb1d54331d1c9540 to your computer and use it in GitHub Desktop.
Created on Cognitive Class Labs
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a href=\"https://cognitiveclass.ai/\">\n",
" <img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Ad/CCLog.png\" width=\"200\" align=\"center\">\n",
"</a>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h1>Classes and Objects in Python</h1>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>\n",
" <strong>Welcome!</strong> \n",
" Objects in programming are like objects in real life. Like life, there are different classes of objects. In this notebook, we will create two classes called Circle and Rectangle. By the end of this notebook, you will have a better idea about :\n",
" <ul>\n",
" <li>what a class is</li>\n",
" <li>what an attribute is</li>\n",
" <li>what a method is</li>\n",
" </ul>\n",
"\n",
" Don’t worry if you don’t get it the first time, as much of the terminology is confusing. Don’t forget to do the practice tests in the notebook.\n",
"</p>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n",
" <a href=\"https://cocl.us/topNotebooksPython101Coursera\">\n",
" <img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Ad/TopAd.png\" width=\"750\" align=\"center\">\n",
" </a>\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2>Table of Contents</h2>\n",
"<div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n",
" <ul>\n",
" <li>\n",
" <a href=\"#intro\">Introduction to Classes and Objects</a>\n",
" <ul>\n",
" <li><a href=\"create\">Creating a class</a></li>\n",
" <li><a href=\"instance\">Instances of a Class: Objects and Attributes</a></li>\n",
" <li><a href=\"method\">Methods</a></li>\n",
" </ul>\n",
" </li>\n",
" <li><a href=\"creating\">Creating a class</a></li>\n",
" <li><a href=\"circle\">Creating an instance of a class Circle</a></li>\n",
" <li><a href=\"rect\">The Rectangle Class</a></li>\n",
" </ul>\n",
" <p>\n",
" Estimated time needed: <strong>40 min</strong>\n",
" </p>\n",
"</div>\n",
"\n",
"<hr>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"intro\">Introduction to Classes and Objects</h2>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Creating a Class</h3>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The first part of creating a class is giving it a name: In this notebook, we will create two classes, Circle and Rectangle. We need to determine all the data that make up that class, and we call that an attribute. Think about this step as creating a blue print that we will use to create objects. In figure 1 we see two classes, circle and rectangle. Each has their attributes, they are variables. The class circle has the attribute radius and color, while the rectangle has the attribute height and width. Let’s use the visual examples of these shapes before we get to the code, as this will help you get accustomed to the vocabulary."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Chapter%203/Images/ClassesClass.png\" width=\"500\" />"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<i>Figure 1: Classes circle and rectangle, and each has their own attributes. The class circle has the attribute radius and colour, the rectangle has the attribute height and width.</i>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3 id=\"instance\">Instances of a Class: Objects and Attributes</h3>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"An instance of an object is the realisation of a class, and in Figure 2 we see three instances of the class circle. We give each object a name: red circle, yellow circle and green circle. Each object has different attributes, so let's focus on the attribute of colour for each object."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Chapter%203/Images/ClassesObj.png\" width=\"500\" />"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<i>Figure 2: Three instances of the class circle or three objects of type circle.</i>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" The colour attribute for the red circle is the colour red, for the green circle object the colour attribute is green, and for the yellow circle the colour attribute is yellow. \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3 id=\"method\">Methods</h3>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Methods give you a way to change or interact with the object; they are functions that interact with objects. For example, let’s say we would like to increase the radius by a specified amount of a circle. We can create a method called **add_radius(r)** that increases the radius by **r**. This is shown in figure 3, where after applying the method to the \"orange circle object\", the radius of the object increases accordingly. The “dot” notation means to apply the method to the object, which is essentially applying a function to the information in the object."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Chapter%203/Images/ClassesMethod.png\" width=\"500\" /> "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<i>Figure 3: Applying the method “add_radius” to the object orange circle object.</i>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"creating\">Creating a Class</h2>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we are going to create a class circle, but first, we are going to import a library to draw the objects: "
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# Import the library\n",
"\n",
"import matplotlib.pyplot as plt\n",
"%matplotlib inline "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" The first step in creating your own class is to use the <code>class</code> keyword, then the name of the class as shown in Figure 4. In this course the class parent will always be object: "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Chapter%203/Images/ClassesDefine.png\" width=\"400\" />"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<i>Figure 4: Three instances of the class circle or three objects of type circle.</i>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The next step is a special method called a constructor <code>&#95;&#95;init&#95;&#95;</code>, which is used to initialize the object. The input are data attributes. The term <code>self</code> contains all the attributes in the set. For example the <code>self.color</code> gives the value of the attribute color and <code>self.radius</code> will give you the radius of the object. We also have the method <code>add_radius()</code> with the parameter <code>r</code>, the method adds the value of <code>r</code> to the attribute radius. To access the radius we use the syntax <code>self.radius</code>. The labeled syntax is summarized in Figure 5:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Chapter%203/Images/ClassesCircle.png\" width=\"600\" />"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<i>Figure 5: Labeled syntax of the object circle.</i>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The actual object is shown below. We include the method <code>drawCircle</code> to display the image of a circle. We set the default radius to 3 and the default colour to blue:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"# Create a class Circle\n",
"\n",
"class Circle(object):\n",
" \n",
" # Constructor\n",
" def __init__(self, radius=3, color='blue'):\n",
" self.radius = radius\n",
" self.color = color \n",
" \n",
" # Method\n",
" def add_radius(self, r):\n",
" self.radius = self.radius + r\n",
" return(self.radius)\n",
" \n",
" # Method\n",
" def drawCircle(self):\n",
" plt.gca().add_patch(plt.Circle((0, 0), radius=self.radius, fc=self.color))\n",
" plt.axis('scaled')\n",
" plt.show() "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"circle\">Creating an instance of a class Circle</h2>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let’s create the object <code>RedCircle</code> of type Circle to do the following:"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"# Create an object RedCircle\n",
"\n",
"RedCircle = Circle(10, 'red')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can use the <code>dir</code> command to get a list of the object's methods. Many of them are default Python methods."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"['__class__',\n",
" '__delattr__',\n",
" '__dict__',\n",
" '__dir__',\n",
" '__doc__',\n",
" '__eq__',\n",
" '__format__',\n",
" '__ge__',\n",
" '__getattribute__',\n",
" '__gt__',\n",
" '__hash__',\n",
" '__init__',\n",
" '__init_subclass__',\n",
" '__le__',\n",
" '__lt__',\n",
" '__module__',\n",
" '__ne__',\n",
" '__new__',\n",
" '__reduce__',\n",
" '__reduce_ex__',\n",
" '__repr__',\n",
" '__setattr__',\n",
" '__sizeof__',\n",
" '__str__',\n",
" '__subclasshook__',\n",
" '__weakref__',\n",
" 'add_radius',\n",
" 'color',\n",
" 'drawCircle',\n",
" 'radius']"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Find out the methods can be used on the object RedCircle\n",
"\n",
"dir(RedCircle)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can look at the data attributes of the object: "
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Print the object attribute radius\n",
"\n",
"RedCircle.radius"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"'red'"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Print the object attribute color\n",
"\n",
"RedCircle.color"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" We can change the object's data attributes: "
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Set the object attribute radius\n",
"\n",
"RedCircle.radius = 5\n",
"RedCircle.radius"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" We can draw the object by using the method <code>drawCircle()</code>:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAP0AAAD4CAYAAAAn+OBPAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAV30lEQVR4nO3deXBW1f3H8fc3ISAVRCtR2cbgKArlh2OJS0Vrf4gKCrEjMyri2lHUWkWr9edeGVvpKFZGrbuOa0sdLZZFakHL0g46JBrUFFdKMRhqKigUbSFyfn8cUgIkEHjuc89dPq+ZZ2ICnvuB4fOcc+9zF3POISL5URI6gIjES6UXyRmVXiRnVHqRnFHpRXKmQ4iNdu/e3VVUVITYtEgu1NTU/NM5V97arwUpfUVFBdXV1SE2LZILZvb3tn5Ny3uRnFHpRXJGpRfJGZVeJGdUepGcUelFckalF8kZlV4kZ4KcnCMJsGYNNDTAJ59s+bWhAdatg6amLV8lJdCh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xswEhM22lCbjaOdcfOAq4LGH5AMYDS0KHaCl3pQfuBq4FEncE0zn3R+dc06ZvXwNC38juCOBD59xS59x6YApwauBM/+Wca3DOvbHpv9fiy9UrbKrNzKw3cArwaOgsLeWq9GZWBaxwzi0OnaUdfgDMCpyhF/Bxi+/rSVCpWjKzCuAw4PWwSbYwGT/BbAwdpKXM3Q3XzOYArT30/UbgBuDEeBNtaXv5nHO/3/R7bsQvXZ+NM1srrJWfJW6FZGZdgBeAK51za0LnATCzkcCnzrkaM/te6DwtZa70zrlhrf3czP4H6AssNjPwS+c3zOwI59zK0Pmamdl5wEjgeBf+JIp6oE+L73sDnwTK0iozK8MX/lnn3O9C52lhCFBlZicDuwF7mNkzzrmzA+fK78k5ZrYMqHTOJebqLDMbDvwSOM4515iAPB3wBxSPB1YAi4CznHN1QYNtYv7d+0lglXPuytB52rJppr/GOTcydBbI2T59CtwHdAVmm1mtmT0YMsymg4o/Al7GHyR7LimF32QIcA4wdNPfV+2mmVW2I7czvUheaaYXyRmVXiRnVHqRnFHpRXJGpRfJGZVeJGdUepGc+X/343xhcawMPwAAAABJRU5ErkJggg==\n",
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"# Call the method drawCircle\n",
"\n",
"RedCircle.drawCircle()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can increase the radius of the circle by applying the method <code>add_radius()</code>. Let increases the radius by 2 and then by 5: "
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Radius of object: 10\n",
"Radius of object of after applying the method add_radius(2): 12\n",
"Radius of object of after applying the method add_radius(5): 17\n"
]
}
],
"source": [
"# Use method to change the object attribute radius\n",
"\n",
"print('Radius of object:',RedCircle.radius)\n",
"RedCircle.add_radius(2)\n",
"print('Radius of object of after applying the method add_radius(2):',RedCircle.radius)\n",
"RedCircle.add_radius(5)\n",
"print('Radius of object of after applying the method add_radius(5):',RedCircle.radius)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" Let’s create a blue circle. As the default colour is blue, all we have to do is specify what the radius is:"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"# Create a blue circle with a given radius\n",
"\n",
"BlueCircle = Circle(radius=100)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" As before we can access the attributes of the instance of the class by using the dot notation:"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"100"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Print the object attribute radius\n",
"\n",
"BlueCircle.radius"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"'blue'"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Print the object attribute color\n",
"\n",
"BlueCircle.color"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" We can draw the object by using the method <code>drawCircle()</code>:"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAQoAAAD4CAYAAAAU5qhvAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAbmklEQVR4nO3de3xU5Z3H8c9PAjEoBbkH0QWpqGAxlkhtbe22iwqUBlGx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4Z+CSwJlvdHX0vJbZFoaprVXV9hqf+PrCvqr6JG/dijIiUEgzsq+43lhrYNwkyvibPNbVkDLBJVd9Q1d3AQlz9STUJuCf4+h5i+J5R1WeBbU0Wt1R3h95LiQ2KVhwKvJP2ODWA76G0Y2Bfjy4TkdVBczLVXGzpNcVRkmptSoEnRKQ6GNgZYIC6kdkI7vt7q659Wqq7Q3+fWE9S7HNg36i09ppwc578EFfXD4E5wIXEpPY2SlKtTZ2kqnUi0h9YLiLrfBcUgQ79fWIdFJqHA/u29TWJyHzg18HDll5THCWp1o9Q1brgfouIPIprkjeISKmq1ge7r1u8Ftl2LdXdob9PPu56JHZg3+APmjIZNxMbtPCacl1fG70MHCkiQ0WkG27GuErPNWUlIgeJSI/U18CpuN9/JTAt+LZpxOw904qW6u7Ye8l3j20nenon49JxF9AALEt7bhauN3c9aUc2gHLcH78GuI3gzNS43ID7gFeB1cEftDTba4rjDZgAbAjqneW7njbWfATuaMAq4LVU3UAf4ElgY3Df23etGWp/AKhn/wRb/9Ja3R15L9kp3MaYrPJx18MYEzILCmNMVhYUxpisLCiMMVlZUBhjsrKgMMZkZUFhjMnq/wHBJHawQti6dgAAAABJRU5ErkJggg==\n",
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"# Call the method drawCircle\n",
"\n",
"BlueCircle.drawCircle()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Compare the x and y axis of the figure to the figure for <code>RedCircle</code>; they are different."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"rect\">The Rectangle Class</h2>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's create a class rectangle with the attributes of height, width and color. We will only add the method to draw the rectangle object:"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"# Create a new Rectangle class for creating a rectangle object\n",
"\n",
"class Rectangle(object):\n",
" \n",
" # Constructor\n",
" def __init__(self, width=2, height=3, color='r'):\n",
" self.height = height \n",
" self.width = width\n",
" self.color = color\n",
" \n",
" # Method\n",
" def drawRectangle(self):\n",
" plt.gca().add_patch(plt.Rectangle((0, 0), self.width, self.height ,fc=self.color))\n",
" plt.axis('scaled')\n",
" plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let’s create the object <code>SkinnyBlueRectangle</code> of type Rectangle. Its width will be 2 and height will be 3, and the color will be blue:"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"# Create a new object rectangle\n",
"\n",
"SkinnyBlueRectangle = Rectangle(2, 10, 'blue')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" As before we can access the attributes of the instance of the class by using the dot notation:"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"10"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Print the object attribute height\n",
"\n",
"SkinnyBlueRectangle.height "
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Print the object attribute width\n",
"\n",
"SkinnyBlueRectangle.width"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"'blue'"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Print the object attribute color\n",
"\n",
"SkinnyBlueRectangle.color"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" We can draw the object:"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
},
"scrolled": true
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAE4AAAD4CAYAAABCOdB1AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAGuklEQVR4nO3dX4hcdxnG8e9jYqitFSuJUpOu20IpBG8aFv/jhbVQo1gvvLCgVBH2Sq0iSMWL3nohohcihFotWOpFLVikqKVaRJDibozaGLV/tO220WwRVLyJxdeLmUiY3U1mn9/p2XO6zweWnZ0Z9rx8mdmZ2fmdM6oqYvtesdMDjFXCmRLOlHCmhDPt7XNj+/fvr8XFxT43aVtdXX2hqg5sdXmv4RYXF1lZWelzkzZJT1/o8txVTQlnSjhTwpkSznTRcJLuknRG0mPnnfc6SQ9Jenz6/YqXdszhmecW9x3gppnzbgcerqprgYenP+8qFw1XVT8H/j5z9s3A3dPTdwMf6niuwXOfAL+hqk4DVNVpSa/f6oqSloFlgIWFhel55lZ7MO+/J1/yB4eqOlZVS1W1dODAlq9gRscN9zdJVwJMv5/pbqRxcMM9ANw6PX0r8INuxhmPeZ6O3Av8ErhO0pqkTwJfBm6U9Dhw4/TnXeWiDw5VdcsWF93Q8SyjklcOpoQzJZwp4UwJZ0o4U8KZEs6UcKaEMyWcKeFMCWdKOFPCmRLOlHCmhDMlnCnhTAlnSjhTwpkSzpRwpoQzJZwp4UwJZ0o4U8KZmsJJ+pykk5Iek3SvpEu6Gmzo7HCSDgKfAZaq6s3AHuAjXQ02dK131b3AqyTtBS4Fnm8faRzscFX1HPAV4BngNPCPqvrJ7PUkLUtakbSyvr7uTzowLXfVK5jsYXM18EbgMkkfnb1e9nPY6L3An6tqvar+A9wPvKObsYavJdwzwNsk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VXWWyfHOb+5mrOFr2bPmIPDseT+vAW+dvZKkZWAZYGFhAYCqhq0ORMstbrP1vxuSZD+HjdaAq877+RDwfNs449ES7lfAtZKulrSPyUcTPNDNWMPXslz/RUmfAn4M7AHuqqqTnU02cE0fS1BVDwIPdjTLqOSVgynhTAlnSjiTqsen8ZLWgaeB/cALvW14e87N9qaq2vIZe6/h/r9RaaWqlnrf8BzmnS13VVPCmXYq3LEd2u485pptR/7GvRzkrmpKOFOv4Yb85s62D7JVVb18MfnX05PANcA+4DfA4b62P8d8VwJHpqcvB/50ofn6vMUN+s2d7R5kq89wm725M8ijf13oIFvn9Blurjd3dtrFDrJ1Tp/hBv/mznYOstVnuEG/ubPtg2z1/Mh1lMmj1ZPAl3b6kXRmtncx+dPxW+DE9OvoVtfPSy5TXjmYEs6UcKaEMyWcKeFMCWf6HwezQ1Yzc3TpAAAAAElFTkSuQmCC\n",
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"# Use the drawRectangle method to draw the shape\n",
"\n",
"SkinnyBlueRectangle.drawRectangle()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let’s create the object <code>FatYellowRectangle</code> of type Rectangle :"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"jupyter": {
"outputs_hidden": true
}
},
"outputs": [],
"source": [
"# Create a new object rectangle\n",
"\n",
"FatYellowRectangle = Rectangle(20, 5, 'yellow')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" We can access the attributes of the instance of the class by using the dot notation:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"# Print the object attribute height\n",
"\n",
"FatYellowRectangle.height "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"# Print the object attribute width\n",
"\n",
"FatYellowRectangle.width"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"# Print the object attribute color\n",
"\n",
"FatYellowRectangle.color"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" We can draw the object:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"# Use the drawRectangle method to draw the shape\n",
"\n",
"FatYellowRectangle.drawRectangle()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>\n",
"<h2>The last exercise!</h2>\n",
"<p>Congratulations, you have completed your first lesson and hands-on lab in Python. However, there is one more thing you need to do. The Data Science community encourages sharing work. The best way to share and showcase your work is to share it on GitHub. By sharing your notebook on GitHub you are not only building your reputation with fellow data scientists, but you can also show it off when applying for a job. Even though this was your first piece of work, it is never too early to start building good habits. So, please read and follow <a href=\"https://cognitiveclass.ai/blog/data-scientists-stand-out-by-sharing-your-notebooks/\" target=\"_blank\">this article</a> to learn how to share your work.\n",
"<hr>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n",
"<h2>Get IBM Watson Studio free of charge!</h2>\n",
" <p><a href=\"https://cocl.us/bottemNotebooksPython101Coursera\"><img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Ad/BottomAd.png\" width=\"750\" align=\"center\"></a></p>\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>About the Authors:</h3> \n",
"<p><a href=\"https://www.linkedin.com/in/joseph-s-50398b136/\" target=\"_blank\">Joseph Santarcangelo</a> is a Data Scientist at IBM, and holds a PhD in Electrical Engineering. His research focused on using Machine Learning, Signal Processing, and Computer Vision to determine how videos impact human cognition. Joseph has been working for IBM since he completed his PhD.</p>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Other contributors: <a href=\"www.linkedin.com/in/jiahui-mavis-zhou-a4537814a\">Mavis Zhou</a>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>Copyright &copy; 2018 IBM Developer Skills Network. This notebook and its source code are released under the terms of the <a href=\"https://cognitiveclass.ai/mit-license/\">MIT License</a>.</p>"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python",
"language": "python",
"name": "conda-env-python-py"
},
"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": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment