Skip to content

Instantly share code, notes, and snippets.

@Ajay-aj
Created June 15, 2021 18:42
Show Gist options
  • Save Ajay-aj/9ceb5f50b716087add44f0e985e40506 to your computer and use it in GitHub Desktop.
Save Ajay-aj/9ceb5f50b716087add44f0e985e40506 to your computer and use it in GitHub Desktop.
Created on Skills Network Labs
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<center>\n",
" <img src=\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork/IDSNlogo.png\" width=\"300\" alt=\"cognitiveclass.ai logo\" />\n",
"</center>\n",
"\n",
"# Classes and Objects in Python\n",
"\n",
"Estimated time needed: **40** minutes\n",
"\n",
"## Objectives\n",
"\n",
"After completing this lab you will be able to:\n",
"\n",
"* Work with classes and objects\n",
"* Identify and define attributes and methods\n"
]
},
{
"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",
"\n",
"</div>\n",
"\n",
"<hr>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"intro\">Introduction to Classes and Objects</h2>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Creating a Class</h3>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The first step in 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, which we call <em>attributes</em>. 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, which are variables. The class Circle has the attribute radius and color, while the Rectangle class 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.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork/labs/Module%203/images/ClassesClass.png\" width=\"500\" />\n"
]
},
{
"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 class Rectangle has the attributes height and width.</i>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3 id=\"instance\">Instances of a Class: Objects and Attributes</h3>\n"
]
},
{
"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 color attribute for each object.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork/labs/Module%203/images/ClassesObj.png\" width=\"500\" />\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<i>Figure 2: Three instances of the class Circle, or three objects of type Circle.</i>\n"
]
},
{
"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>\n"
]
},
{
"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 of a circle by a specified amount. 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.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork/labs/Module%203/images/ClassesMethod.png\" width=\"500\" /> \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<i>Figure 3: Applying the method “add_radius” to the object orange circle object.</i>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"creating\">Creating a Class</h2>\n"
]
},
{
"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:\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"tags": []
},
"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:\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork/labs/Module%203/images/ClassesDefine.png\" width=\"400\" />\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<i>Figure 4: Creating a class Circle.</i>\n"
]
},
{
"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 inputs 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:\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork/labs/Module%203/images/ClassesCircle.png\" width=\"600\" />\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<i>Figure 5: Labeled syntax of the object circle.</i>\n"
]
},
{
"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:\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"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>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"circle\">Creating an instance of a class Circle</h2>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let’s create the object <code>RedCircle</code> of type Circle to do the following:\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"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.\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"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": 4,
"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:\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Print the object attribute radius\n",
"\n",
"RedCircle.radius"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'red'"
]
},
"execution_count": 6,
"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:\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"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>:\n"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs":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mNlKM9tlZnlPKZnZSDN71czeiMYmTQ0z29/Mnjaz16PH/fIst9bMlpvZEjNLxd17HX2u5t0azV9mZkNDxNmRAvbjVDPbEn32S8xsUog422Nm08xsk5mtyDO/878L51xmJvyo6EfjxxptyLNMLbAaOALoCiwFBoaOvVV8NwLXRc+vA/41z3JrgZ6h4y3mcwXOAebgR3M7AXgpdNyd3I9TgcdCx9rBfpwMDAVW5Jnf6d9FploazrlVzrlXO1js44GOnXMfAs0DHafFefhBlokePx8ulKIU8rmeB8xw3ovAvtGIbWmS9r+PgjjnFgCb21mk07+LTCWNAuUa6PjgQLHksttgy0C+wZYd8JSZNUaDLodWyOea9s8eCo/xRDNbamZzzOwzyYQWq07/LpIYjTxWaRnouBTt7UMRmxnhnNsYjeD+tJm9Ev13CaWQzzX4Z1+AQmJ8GX9vxvtmdg7wCNC/3IHFrNO/i4pLGq68Ax0nor19MLOCBlt2zm2MHjeZ2Ux8szpk0ijkcw3+2Regwxidc1tbPX/CzG43s57OuUq6ma3Tv4tqPDxpb6DjNOhwsGUz29vMejQ/B84GcvaSJ6iQz3UWcHHUc38CsKX5UCxFOtwPMzvIogphZjYc/z16N/FIS9P530XoXt6Ye4zPx2fQPwNvA3Oj1/sCT7TpOX4N30v+ndBxt9mHA4Bngdejx/3b7gO+Z39pNK1Myz7k+lyBicDE6LkBP4nmLyfPGa7QUwH7cVX0uS8FXgROCh1zjn14AGgCdkTfib+L63ehy8hFpCjVeHgiIiVQ0hCRoihpiEhRlDREpChKGiJSFCUNESmKkoaIFOX/AedMnxceoiKeAAAAAElFTkSuQmCC\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's increases the radius by 2 and then by 5:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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:\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"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:\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"100"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Print the object attribute radius\n",
"\n",
"BlueCircle.radius"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'blue'"
]
},
"execution_count": 12,
"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>:\n"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAQoAAAD4CAYAAAAU5qhvAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8QVMy6AAAACXBIWXMAAAsTAAALEwEAmpwYAAAbm0lEQVR4nO3de3xU5Z3H8c9PAjEoBbkH0QWpqGAxlkhtbe22iwqUBlGxuLairkVrrXVt18qlrrbW1brI1lq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TYFoWqrlXV9Rme+vvAvqr6Jm7cizEiUkowsK+631hqYN8kyPiaPNfUkjHAJlV9Q1V3Awtx9SfVJOCe4Ot7iOF7RlWfBbY1WdxS3R16LyU2KFpxKPBO2uPUAL6H0o6BfT26TERWB83JVHOxpdcUR0mqtSkFnhCR6mBgZ4AB6kZmI7jv76269mmp7g79fWI9SbHPgX2j0tprws158kNcXT8E5gAXEpPa2yhJtTZ1kqrWiUh/YLmIrPNdUAQ69PeJdVBoHg7s29bXJCLzgV8HD1t6TXGUpFo/QlXrgvstIvIorkneICKlqlof7L5u8Vpk27VUd4f+Pvm465HYgX2DP2jKZNxMbNDCa8p1fW30MnCkiAwVkW64GeMqPdeUlYgcJCI9Ul8Dp+J+/5XAtODbphGz90wrWqq7Y+8l3z22nejpnYxLx11AA7As7blZuN7c9aQd2QDKcX/8GuA2gjNT43ID7gNeBVYHf9DSbK8pjjdgArAhqHeW73raWPMRuKMBq4DXUnUDfYAngY3BfW/ftWao/QGgnv0TbP1La3V35L1kp3AbY7LKx10PY0zILCiMMVlZUBhjsrKgMMZkZUFhjMnKgsIYk5UFhTEmq/8HwSR2sAeO5aYAAAAASUVORK5CYII=\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.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"rect\">The Rectangle Class</h2>\n"
]
},
{
"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:\n"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"tags": []
},
"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()\n",
" "
]
},
{
"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:\n"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"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:\n"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Print the object attribute height\n",
"\n",
"SkinnyBlueRectangle.height "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Print the object attribute width\n",
"\n",
"SkinnyBlueRectangle.width"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'blue'"
]
},
"execution_count": 17,
"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:\n"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAE4AAAD4CAYAAABCOdB1AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuNCwgaH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mXYq3LEd2u485pptR/7GvRzkrmpKOFOv4Yb85s62D7JVVb18MfnX05PANcA+4DfA4b62P8d8VwJHpqcvB/50ofn6vMUN+s2d7R5kq89wm725M8ijf13oIFvn9Blurjd3dtrFDrJ1Tp/hBv/mznYOstVnuEG/ubPtg2z1/Mh1lMmj1ZPAl3b6kXRmtncx+dPxW+DE9OvoVtfPSy5TXjmYEs6UcKaEMyWcKeFMCWf6HwezQ1aLwPHmAAAAAElFTkSuQmCC\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:\n"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"tags": []
},
"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:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Print the object attribute height\n",
"\n",
"FatYellowRectangle.height "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Print the object attribute width\n",
"\n",
"FatYellowRectangle.width"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Print the object attribute color\n",
"\n",
"FatYellowRectangle.color"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can draw the object:\n"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAWoAAABzCAYAAACxdkgEAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMu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5LORMTxmu2t9KsvG1o5xroI6ibTpjaaWjUV21dKekjS7og417d5RsXT+3dL+rakn7dU1qaI2KjiP+18wfZNfds765mLybq2SPpZxeau+tVUl327U9J5Sftrhgza56P2XUlvl/QeSfMqLjP06/KxebuWPptO3q8B2VB7s4p1Q/Wsi6BuMm1qo6lVU7C9SsWO2B8RD/dvj4hzEfGPcvkxSatsj6WuKyJeKL+flfSIiqdTvTrrmYoHxkxEnOnf0FW/SmcWL/+U389WjOmkb7a3S7pN0qeivJDZr8E+H6mIOBMR/4mIVyR9v+b+uurXSkmfkPRg3ZjU/arJhlaOsS6Cusm0qQckfbZ8J8ONkl5afHqRUnkN7D5JcxFxb82YN5fjZPsGFT38W+K6Xmd79eKyihejjvYN66RnpdoznS761eOApO3l8nZJv6gY0/o0vrZvlfQVSVsi4p81Y5rs81HX1fuaxsdr7q+raY8/JOm5iDhdtTF1v5bIhnaOsRSvkDZ4BXWzildNT0q6s1x3h6Q7ymWr+Ie6JyU9K2mypbrer+IpyRFJs+XX5r7adko6puKV24OS3tdCXdeV93e4vO+cevZaFcH7+p51rfdLxR+KeUn/VnEG83lJ10j6laTj5fery7FvkfTYUsdj4rpOqLhmuXiMfa+/rrp9nriuH5fHzhEVQbImh36V63+4eEz1jG2zX3XZ0MoxxicTASBzfDIRADJHUANA5ghqAMgcQQ0AmSOoASBzBDUAZI6gBoDMEdQAkLn/AoCPDaQTCdfoAAAAAElFTkSuQmCC\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",
"FatYellowRectangle.drawRectangle()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"rect\">Exercises</h2>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h4> Text Analysis </h4>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You have been recruited by your friend, a linguistics enthusiast, to create a utility tool that can perform analysis on a given piece of text. Complete the class\n",
"'analysedText' with the following methods -\n",
"\n",
"<ul>\n",
" <li> Constructor - Takes argument 'text',makes it lower case and removes all punctuation. Assume only the following punctuation is used - period (.), exclamation mark (!), comma (,) and question mark (?). Store the argument in \"fmtText\" \n",
" <li> freqAll - returns a dictionary of all unique words in the text along with the number of their occurences.\n",
" <li> freqOf - returns the frequency of the word passed in argument.\n",
"</ul>\n",
" The skeleton code has been given to you. Docstrings can be ignored for the purpose of the exercise. <br>\n",
" <i> Hint: Some useful functions are <code>replace()</code>, <code>lower()</code>, <code>split()</code>, <code>count()</code> </i><br>\n"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [],
"source": [
"class analysedText(object):\n",
" \n",
" def __init__ (self, text):\n",
" text = text.lower();\n",
" text = text.replace('.', '')\n",
" text = text.replace('!', '')\n",
" text = text.replace(',', '')\n",
" text = text.replace('?', '')\n",
" self.fmtText = text\n",
" self.list = self.fmtText.split()\n",
" print(self.fmtText)\n",
" print(self.list)\n",
" \n",
" def freqAll(self):\n",
" return_dict = {}\n",
" for words in self.list:\n",
" if (words in return_dict.keys()):\n",
" return_dict[words] = return_dict[words] + 1;\n",
" else:\n",
" return_dict[words] = 1;\n",
" return return_dict;\n",
" \n",
" def freqOf(self,word):\n",
" returnVal = self.fmtText.count(word);\n",
" print(returnVal)\n",
" return self.fmtText.count(word);\n",
" \n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Execute the block below to check your progress.\n"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Constructor: \n",
"lorem ipsum dolor diam amet consetetur lorem magna sed diam nonumy eirmod tempor diam et labore et diam magna et diam amet\n",
"['lorem', 'ipsum', 'dolor', 'diam', 'amet', 'consetetur', 'lorem', 'magna', 'sed', 'diam', 'nonumy', 'eirmod', 'tempor', 'diam', 'et', 'labore', 'et', 'diam', 'magna', 'et', 'diam', 'amet']\n",
"Test Passed\n",
"freqAll: \n",
"Test Passed\n",
"freqOf: \n",
"1\n",
"1\n",
"2\n",
"5\n",
"1\n",
"1\n",
"1\n",
"1\n",
"2\n",
"7\n",
"Test Failed\n"
]
}
],
"source": [
"import sys\n",
"\n",
"sampleMap = {'eirmod': 1,'sed': 1, 'amet': 2, 'diam': 5, 'consetetur': 1, 'labore': 1, 'tempor': 1, 'dolor': 1, 'magna': 2, 'et': 3, 'nonumy': 1, 'ipsum': 1, 'lorem': 2}\n",
"\n",
"def testMsg(passed):\n",
" if passed:\n",
" return 'Test Passed'\n",
" else :\n",
" return 'Test Failed'\n",
"\n",
"print(\"Constructor: \")\n",
"try:\n",
" samplePassage = analysedText(\"Lorem ipsum dolor! diam amet, consetetur Lorem magna. sed diam nonumy eirmod tempor. diam et labore? et diam magna. et diam amet.\")\n",
" print(testMsg(samplePassage.fmtText == \"lorem ipsum dolor diam amet consetetur lorem magna sed diam nonumy eirmod tempor diam et labore et diam magna et diam amet\"))\n",
"except:\n",
" print(\"Error detected. Recheck your function \" )\n",
"print(\"freqAll: \")\n",
"try:\n",
" wordMap = samplePassage.freqAll()\n",
" print(testMsg(wordMap==sampleMap))\n",
"except:\n",
" print(\"Error detected. Recheck your function \" )\n",
"print(\"freqOf: \")\n",
"try:\n",
" passed = True\n",
" for word in sampleMap:\n",
" if samplePassage.freqOf(word) != sampleMap[word]:\n",
" passed = False\n",
" break\n",
" print(testMsg(passed))\n",
" \n",
"except:\n",
" print(\"Error detected. Recheck your function \" )\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<details><summary>Click here for the solution</summary>\n",
"\n",
"```python\n",
"class analysedText(object):\n",
" \n",
" def __init__ (self, text):\n",
" # remove punctuation\n",
" formattedText = text.replace('.','').replace('!','').replace('?','').replace(',','')\n",
" \n",
" # make text lowercase\n",
" formattedText = formattedText.lower()\n",
" \n",
" self.fmtText = formattedText\n",
" \n",
" def freqAll(self): \n",
" # split text into words\n",
" wordList = self.fmtText.split(' ')\n",
" \n",
" # Create dictionary\n",
" freqMap = {}\n",
" for word in set(wordList): # use set to remove duplicates in list\n",
" freqMap[word] = wordList.count(word)\n",
" \n",
" return freqMap\n",
" \n",
" def freqOf(self,word):\n",
" # get frequency map\n",
" freqDict = self.freqAll()\n",
" \n",
" if word in freqDict:\n",
" return freqDict[word]\n",
" else:\n",
" return 0\n",
" \n",
"```\n",
"\n",
"</details>\n"
]
},
{
"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/?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_content=000026UJ&utm_term=10006555&utm_id=NA-SkillsNetwork-Channel-SkillsNetworkCoursesIBMDeveloperSkillsNetworkPY0101ENSkillsNetwork19487395-2021-01-01\" target=\"_blank\">this article</a> to learn how to share your work.\n",
"<hr>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Author\n",
"\n",
"<a href=\"https://www.linkedin.com/in/joseph-s-50398b136/?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_content=000026UJ&utm_term=10006555&utm_id=NA-SkillsNetwork-Channel-SkillsNetworkCoursesIBMDeveloperSkillsNetworkPY0101ENSkillsNetwork19487395-2021-01-01\" target=\"_blank\">Joseph Santarcangelo</a>\n",
"\n",
"## Other contributors\n",
"\n",
"<a href=\"www.linkedin.com/in/jiahui-mavis-zhou-a4537814a\">Mavis Zhou</a>\n",
"\n",
"## Change Log\n",
"\n",
"| Date (YYYY-MM-DD) | Version | Changed By | Change Description |\n",
"|---|---|---|---|\n",
"| 2020-08-26 | 2.0 | Lavanya | Moved lab to course repo in GitLab |\n",
"| | | | |\n",
"| | | | |\n",
"\n",
"<hr/>\n",
"\n",
"## <h3 align=\"center\"> © IBM Corporation 2020. All rights reserved. <h3/>\n"
]
}
],
"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.13"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment