Skip to content

Instantly share code, notes, and snippets.

@Ammaraf
Created January 2, 2020 04:27
Show Gist options
  • Save Ammaraf/a6ea20a946ec4dd46cc523da9bbc72df to your computer and use it in GitHub Desktop.
Save Ammaraf/a6ea20a946ec4dd46cc523da9bbc72df 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": [
"<div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n",
" <a href=\"https://cocl.us/NotebooksPython101\">\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": [
"<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": [
"<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": 1,
"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": 18,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"# Create an object RedCircle\n",
"\n",
"RedCircle = Circle(10, 'red')\n"
]
},
{
"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": 7,
"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": 7,
"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": [
"10"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Print the object attribute radius\n",
"\n",
"RedCircle.radius"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"'red'"
]
},
"execution_count": 10,
"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": 15,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Set the object attribute radius\n",
"\n",
"RedCircle.radius = 1\n",
"RedCircle.radius\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" We can draw the object by using the method <code>drawCircle()</code>:"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAQ0AAAD4CAYAAAD2OrMWAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAbaElEQVR4nO3deZRU9Zn/8ffTzRIjxA1EUKT1JxrQ4waixsTouIGj4DIxoBMdZxLUaDSOMxk1iZFxIp5Ef3rUuMuoSYzxJMHgikt+As5RA21AJIgiIdLQihEFRCfQ8vz++FaHpqleirq3vvdWfV7n1Knt9q2nGurT37vU9zF3R0Sku+piFyAi+aLQEJGSKDREpCQKDREpiUJDRErSI3YBW6Nfv37e0NAQuwyRqtXY2PgXd+9f7LlchkZDQwNz5syJXYZI1TKzP3f0nDZPRKQkCg0RKYlCQ0RKotAQkZIoNESkJImEhplNMbOVZvZam8d2NLNnzOzNwvUOHfzsaDNbZGaLzezyJOoRkfQkNdK4Dxjd7rHLgefcfSjwXO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P2+pUws7ePJgNDDWzPcysF2FH9LTINZXMzLY1s76tt4Hjyc+/QXtlT+ady5FGZ8zsVOAWoD9hsuO57n6Cuy8ws9bJjltoM9lxDvzIzA4kDO2XAufFLad73L3FzC4CpgP1wJTCpNN5MwCYWugk2AN40N2filtS18zsF4QOhv0KE4D/gDB598Nm9i/A28BXSl6vTiMXkVLU0uaJiCRAoSEiJVFoiEhJFBoiUhKFhoiURKEhIiVRaIhISf4/F3FNqz2l6UgAAAAASUVORK5CYII=\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": 20,
"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": 25,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"# Create a blue circle with a given radius\n",
"\n",
"BlueCircle = Circle(radius=100, color='black')"
]
},
{
"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": 22,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"100"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Print the object attribute radius\n",
"\n",
"BlueCircle.radius"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"'black'"
]
},
"execution_count": 26,
"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": 27,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAQoAAAD4CAYAAAAU5qhvAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAfGElEQVR4nO3dfXRU1b3/8feXJISAFOQ5SL0gggIKoQmIYnW1NxVBDCKiWK0oRbHUWn/0lgqiC1rKqq1IpVZt+IlUi40X/SnhFkRQUa+okFhAUJ6CT0AErA9UhYSE7++PmeAIk0wezpl9Zub7WmuvmTlncs5nkplv9jkzs7eoKsYYU5dmrgMYY4LPCoUxJiYrFMaYmKxQGGNiskJhjIkp3XWA+urQoYN2797ddQxjklZpaenHqtox2rqEKRTdu3enpKTEdQxjkpaIvF/bOjv0MMbEZIXCGBOTFQpjTExWKIwxMVmhMMbE5EmhEJGFIrJfRDZHLGsnIqtEZEf48uSIddNEZKeIbBORYV5kMMb4x6sexSLg4uOW3Q48r6q9gOfDtxGRvsA4oF/4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4m1CXfJyLZqloePnzd7zRk/dWWu1F/n2Q89EjYgX3Df9AaownNxAa1PKZ456un9UAvEekhIs0JzRhX7DhTTCLSSkRa11wHLiL0+y8GxofvNp6APWfqUFvuxj2XXJ+xbcKZ3tGEqmMFsA9YGbHuDkJnc7cR8c4GkEfoj18G3E/4k6lBacBjwFvApvAfNDvWYwpiA0YA28N573Cdp56ZTyP0bsBGYEtNbqA98DywI3zZznXWKNn/DpTz9QRbP64rd2OeS/YRbmNMTMl46GGM8ZgVCmNMTFYojDExWaEwxsRkhcIYE5MVCmNMTFYojDEx/X/qgcnxFcrZjgAAAABJRU5ErkJggg==\n",
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"# Call the method drawCircle\n",
"\n",
"BlackCircle.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": 29,
"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": 30,
"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": 31,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"10"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Print the object attribute height\n",
"\n",
"SkinnyBlueRectangle.height "
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Print the object attribute width\n",
"\n",
"SkinnyBlueRectangle.width"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"'blue'"
]
},
"execution_count": 33,
"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": null,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
},
"scrolled": true
},
"outputs": [],
"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/NotebooksPython101bottom\"><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