|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Question 44\n", |
| 8 | + "\n", |
| 9 | + "### **Question:**\n", |
| 10 | + "\n", |
| 11 | + "> **_Write a program which can map() to make a list whose elements are square of numbers between 1 and 20 (both included)._**\n", |
| 12 | + "\n", |
| 13 | + "---\n", |
| 14 | + "\n", |
| 15 | + "### Hints:\n", |
| 16 | + "\n", |
| 17 | + "> **_Use map() to generate a list. Use lambda to define anonymous functions._**\n", |
| 18 | + "\n", |
| 19 | + "---\n", |
| 20 | + "\n", |
| 21 | + "**Main Author's Solution: Python 2**" |
| 22 | + ] |
| 23 | + }, |
| 24 | + { |
| 25 | + "cell_type": "code", |
| 26 | + "execution_count": null, |
| 27 | + "metadata": {}, |
| 28 | + "outputs": [], |
| 29 | + "source": [ |
| 30 | + "squaredNumbers = map(lambda x: x**2, range(1,21))\n", |
| 31 | + "print squaredNumbers" |
| 32 | + ] |
| 33 | + }, |
| 34 | + { |
| 35 | + "cell_type": "markdown", |
| 36 | + "metadata": {}, |
| 37 | + "source": [ |
| 38 | + "---\n", |
| 39 | + "\n", |
| 40 | + "**My Solution: Python 3**" |
| 41 | + ] |
| 42 | + }, |
| 43 | + { |
| 44 | + "cell_type": "code", |
| 45 | + "execution_count": null, |
| 46 | + "metadata": {}, |
| 47 | + "outputs": [], |
| 48 | + "source": [ |
| 49 | + "def sqr(x):\n", |
| 50 | + " return x*x\n", |
| 51 | + "\n", |
| 52 | + "squaredNumbers = list(map(sqr, range(1,21)))\n", |
| 53 | + "print (squaredNumbers)" |
| 54 | + ] |
| 55 | + }, |
| 56 | + { |
| 57 | + "cell_type": "markdown", |
| 58 | + "metadata": {}, |
| 59 | + "source": [ |
| 60 | + "---\n", |
| 61 | + "\n", |
| 62 | + "# Question 45\n", |
| 63 | + "\n", |
| 64 | + "### **Question:**\n", |
| 65 | + "\n", |
| 66 | + "> **_Define a class named American which has a static method called printNationality._**\n", |
| 67 | + "\n", |
| 68 | + "---\n", |
| 69 | + "\n", |
| 70 | + "### Hints:\n", |
| 71 | + "\n", |
| 72 | + "> **_Use @staticmethod decorator to define class static method.There are also two more methods.To know more, go to this [link](https://realpython.com/blog/python/instance-class-and-static-methods-demystified/)._**\n", |
| 73 | + "\n", |
| 74 | + "---\n", |
| 75 | + "\n", |
| 76 | + "**Main Author's Solution: Python 2**" |
| 77 | + ] |
| 78 | + }, |
| 79 | + { |
| 80 | + "cell_type": "code", |
| 81 | + "execution_count": null, |
| 82 | + "metadata": {}, |
| 83 | + "outputs": [], |
| 84 | + "source": [ |
| 85 | + "class American(object):\n", |
| 86 | + " @staticmethod\n", |
| 87 | + " def printNationality():\n", |
| 88 | + " print \"America\"\n", |
| 89 | + "\n", |
| 90 | + "anAmerican = American()\n", |
| 91 | + "anAmerican.printNationality()\n", |
| 92 | + "American.printNationality()" |
| 93 | + ] |
| 94 | + }, |
| 95 | + { |
| 96 | + "cell_type": "markdown", |
| 97 | + "metadata": {}, |
| 98 | + "source": [ |
| 99 | + "---\n", |
| 100 | + "\n", |
| 101 | + "**My Solution: Python 3**" |
| 102 | + ] |
| 103 | + }, |
| 104 | + { |
| 105 | + "cell_type": "code", |
| 106 | + "execution_count": null, |
| 107 | + "metadata": {}, |
| 108 | + "outputs": [], |
| 109 | + "source": [ |
| 110 | + "class American():\n", |
| 111 | + " @staticmethod\n", |
| 112 | + " def printNationality():\n", |
| 113 | + " print(\"I am American\")\n", |
| 114 | + "\n", |
| 115 | + "american = American()\n", |
| 116 | + "american.printNationality() # this will not run if @staticmethod does not decorates the function.\n", |
| 117 | + " # Because the class has no instance.\n", |
| 118 | + "\n", |
| 119 | + "American.printNationality() # this will run even though the @staticmethod\n", |
| 120 | + " # does not decorate printNationality()" |
| 121 | + ] |
| 122 | + }, |
| 123 | + { |
| 124 | + "cell_type": "markdown", |
| 125 | + "metadata": {}, |
| 126 | + "source": [ |
| 127 | + "---\n", |
| 128 | + "\n", |
| 129 | + "# Question 46\n", |
| 130 | + "\n", |
| 131 | + "### **Question:**\n", |
| 132 | + "\n", |
| 133 | + "> **_Define a class named American and its subclass NewYorker._**\n", |
| 134 | + "\n", |
| 135 | + "---\n", |
| 136 | + "\n", |
| 137 | + "### Hints:\n", |
| 138 | + "\n", |
| 139 | + "> **Use class Subclass(ParentClass) to define a subclass.\\***\n", |
| 140 | + "\n", |
| 141 | + "---\n", |
| 142 | + "\n", |
| 143 | + "**Main Author's Solution: Python 2**" |
| 144 | + ] |
| 145 | + }, |
| 146 | + { |
| 147 | + "cell_type": "code", |
| 148 | + "execution_count": null, |
| 149 | + "metadata": {}, |
| 150 | + "outputs": [], |
| 151 | + "source": [ |
| 152 | + "class American(object):\n", |
| 153 | + " pass\n", |
| 154 | + "\n", |
| 155 | + "class NewYorker(American):\n", |
| 156 | + " pass\n", |
| 157 | + "\n", |
| 158 | + "anAmerican = American()\n", |
| 159 | + "aNewYorker = NewYorker()\n", |
| 160 | + "print anAmerican\n", |
| 161 | + "print aNewYorker" |
| 162 | + ] |
| 163 | + }, |
| 164 | + { |
| 165 | + "cell_type": "markdown", |
| 166 | + "metadata": {}, |
| 167 | + "source": [ |
| 168 | + "---\n", |
| 169 | + "\n", |
| 170 | + "**My Solution: Python 3**" |
| 171 | + ] |
| 172 | + }, |
| 173 | + { |
| 174 | + "cell_type": "code", |
| 175 | + "execution_count": null, |
| 176 | + "metadata": {}, |
| 177 | + "outputs": [], |
| 178 | + "source": [ |
| 179 | + "class American():\n", |
| 180 | + " pass\n", |
| 181 | + "\n", |
| 182 | + "class NewYorker(American):\n", |
| 183 | + " pass\n", |
| 184 | + "\n", |
| 185 | + "american = American()\n", |
| 186 | + "newyorker = NewYorker()\n", |
| 187 | + "\n", |
| 188 | + "print(american)\n", |
| 189 | + "print(newyorker)" |
| 190 | + ] |
| 191 | + }, |
| 192 | + { |
| 193 | + "cell_type": "markdown", |
| 194 | + "metadata": {}, |
| 195 | + "source": [ |
| 196 | + "---\n", |
| 197 | + "\n", |
| 198 | + "[**_go to previous day_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_11.md \"Day 11\")\n", |
| 199 | + "\n", |
| 200 | + "[**_go to next day_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_13.md \"Day 13\")\n", |
| 201 | + "\n", |
| 202 | + "[**_Discussion_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/issues/3)" |
| 203 | + ] |
| 204 | + } |
| 205 | + ], |
| 206 | + "metadata": {}, |
| 207 | + "nbformat": 4, |
| 208 | + "nbformat_minor": 4 |
| 209 | +} |
0 commit comments