Created
October 10, 2019 13:03
-
-
Save ashutoshkrris/d9ebd046b8e52e9ef3f0ce2c35278101 to your computer and use it in GitHub Desktop.
Created on Cognitive Class Labs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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": null, | |
"metadata": { | |
"collapsed": true, | |
"jupyter": { | |
"outputs_hidden": true | |
} | |
}, | |
"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>__init__</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": null, | |
"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": null, | |
"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": 1, | |
"metadata": { | |
"collapsed": false, | |
"jupyter": { | |
"outputs_hidden": false | |
} | |
}, | |
"outputs": [ | |
{ | |
"ename": "NameError", | |
"evalue": "name 'RedCircle' is not defined", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-1-ab9110aaff1c>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Find out the methods can be used on the object RedCircle\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mdir\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mRedCircle\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[0;31mNameError\u001b[0m: name 'RedCircle' is not defined" | |
] | |
} | |
], | |
"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": null, | |
"metadata": { | |
"collapsed": false, | |
"jupyter": { | |
"outputs_hidden": false | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
"# Print the object attribute radius\n", | |
"\n", | |
"RedCircle.radius" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false, | |
"jupyter": { | |
"outputs_hidden": false | |
} | |
}, | |
"outputs": [], | |
"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": null, | |
"metadata": { | |
"collapsed": false, | |
"jupyter": { | |
"outputs_hidden": false | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
"# Set the object attribute radius\n", | |
"\n", | |
"RedCircle.radius = 1\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": null, | |
"metadata": { | |
"collapsed": false, | |
"jupyter": { | |
"outputs_hidden": false | |
} | |
}, | |
"outputs": [], | |
"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": null, | |
"metadata": { | |
"collapsed": false, | |
"jupyter": { | |
"outputs_hidden": false | |
} | |
}, | |
"outputs": [], | |
"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": null, | |
"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": null, | |
"metadata": { | |
"collapsed": false, | |
"jupyter": { | |
"outputs_hidden": false | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
"# Print the object attribute radius\n", | |
"\n", | |
"BlueCircle.radius" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false, | |
"jupyter": { | |
"outputs_hidden": false | |
} | |
}, | |
"outputs": [], | |
"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": null, | |
"metadata": { | |
"collapsed": false, | |
"jupyter": { | |
"outputs_hidden": false | |
} | |
}, | |
"outputs": [], | |
"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": null, | |
"metadata": { | |
"collapsed": true, | |
"jupyter": { | |
"outputs_hidden": true | |
} | |
}, | |
"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": null, | |
"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": null, | |
"metadata": { | |
"collapsed": false, | |
"jupyter": { | |
"outputs_hidden": false | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
"# Print the object attribute height\n", | |
"\n", | |
"SkinnyBlueRectangle.height " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false, | |
"jupyter": { | |
"outputs_hidden": false | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
"# Print the object attribute width\n", | |
"\n", | |
"SkinnyBlueRectangle.width" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false, | |
"jupyter": { | |
"outputs_hidden": false | |
} | |
}, | |
"outputs": [], | |
"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": 2, | |
"metadata": { | |
"collapsed": false, | |
"jupyter": { | |
"outputs_hidden": false | |
} | |
}, | |
"outputs": [ | |
{ | |
"ename": "NameError", | |
"evalue": "name 'FatYellowRectangle' is not defined", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-2-92c87739a5f8>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Use the drawRectangle method to draw the shape\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mFatYellowRectangle\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdrawRectangle\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[0;31mNameError\u001b[0m: name 'FatYellowRectangle' is not defined" | |
] | |
} | |
], | |
"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 © 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