|
1 | 1 | { |
2 | | - "nbformat": 4, |
3 | | - "nbformat_minor": 0, |
4 | | - "metadata": { |
5 | | - "colab": { |
6 | | - "name": "Learn the Python Math Module.ipynb", |
7 | | - "provenance": [], |
8 | | - "collapsed_sections": [], |
9 | | - "authorship_tag": "ABX9TyOwgJhxR8T7UydRYxOPlUbq", |
10 | | - "include_colab_link": true |
11 | | - }, |
12 | | - "kernelspec": { |
13 | | - "name": "python3", |
14 | | - "display_name": "Python 3" |
15 | | - } |
16 | | - }, |
17 | 2 | "cells": [ |
18 | 3 | { |
19 | 4 | "cell_type": "markdown", |
20 | 5 | "metadata": { |
21 | | - "id": "view-in-github", |
22 | | - "colab_type": "text" |
| 6 | + "colab_type": "text", |
| 7 | + "id": "view-in-github" |
23 | 8 | }, |
24 | 9 | "source": [ |
25 | 10 | "<a href=\"https://colab.research.google.com/github/Tanu-N-Prabhu/Python/blob/master/Learn_the_Python_Math_Module.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" |
|
28 | 13 | { |
29 | 14 | "cell_type": "markdown", |
30 | 15 | "metadata": { |
31 | | - "id": "iOM74FRHcq3g", |
32 | | - "colab_type": "text" |
| 16 | + "colab_type": "text", |
| 17 | + "id": "iOM74FRHcq3g" |
33 | 18 | }, |
34 | 19 | "source": [ |
35 | 20 | "# Learn the Python Math Module" |
|
38 | 23 | { |
39 | 24 | "cell_type": "markdown", |
40 | 25 | "metadata": { |
41 | | - "id": "g-hJaHmZc2UC", |
42 | | - "colab_type": "text" |
| 26 | + "colab_type": "text", |
| 27 | + "id": "g-hJaHmZc2UC" |
43 | 28 | }, |
44 | 29 | "source": [ |
45 | 30 | "In this tutorial, I will show you how to use to the builtin Python `math` module." |
|
48 | 33 | { |
49 | 34 | "cell_type": "markdown", |
50 | 35 | "metadata": { |
51 | | - "id": "mf1bX3MQc76M", |
52 | | - "colab_type": "text" |
| 36 | + "colab_type": "text", |
| 37 | + "id": "mf1bX3MQc76M" |
53 | 38 | }, |
54 | 39 | "source": [ |
55 | 40 | "" |
|
58 | 43 | { |
59 | 44 | "cell_type": "markdown", |
60 | 45 | "metadata": { |
61 | | - "id": "rPl_xuiadFJ1", |
62 | | - "colab_type": "text" |
| 46 | + "colab_type": "text", |
| 47 | + "id": "rPl_xuiadFJ1" |
63 | 48 | }, |
64 | 49 | "source": [ |
65 | 50 | "# What is the math module?\n" |
|
68 | 53 | { |
69 | 54 | "cell_type": "markdown", |
70 | 55 | "metadata": { |
71 | | - "id": "FCvwxdR2dIvR", |
72 | | - "colab_type": "text" |
| 56 | + "colab_type": "text", |
| 57 | + "id": "FCvwxdR2dIvR" |
73 | 58 | }, |
74 | 59 | "source": [ |
75 | 60 | "It is the C standard defined math module. The math module provides access to mathematical functions.\n", |
|
80 | 65 | { |
81 | 66 | "cell_type": "markdown", |
82 | 67 | "metadata": { |
83 | | - "id": "zJe5CnI1dOyY", |
84 | | - "colab_type": "text" |
| 68 | + "colab_type": "text", |
| 69 | + "id": "zJe5CnI1dOyY" |
85 | 70 | }, |
86 | 71 | "source": [ |
87 | 72 | "\n", |
|
103 | 88 | { |
104 | 89 | "cell_type": "markdown", |
105 | 90 | "metadata": { |
106 | | - "id": "ZvvM2dNSd1wA", |
107 | | - "colab_type": "text" |
| 91 | + "colab_type": "text", |
| 92 | + "id": "ZvvM2dNSd1wA" |
108 | 93 | }, |
109 | 94 | "source": [ |
110 | 95 | "# Why do we need to use the math module?\n" |
|
113 | 98 | { |
114 | 99 | "cell_type": "markdown", |
115 | 100 | "metadata": { |
116 | | - "id": "SrUQXTeld5Ta", |
117 | | - "colab_type": "text" |
| 101 | + "colab_type": "text", |
| 102 | + "id": "SrUQXTeld5Ta" |
118 | 103 | }, |
119 | 104 | "source": [ |
120 | 105 | "Well, the answer is simple: Whenever we need to perform mathematical operations then the math module comes in handy. Let me give you a simple example. It’s quiz time!\n" |
|
123 | 108 | { |
124 | 109 | "cell_type": "markdown", |
125 | 110 | "metadata": { |
126 | | - "id": "ch7vaPiNd8Xc", |
127 | | - "colab_type": "text" |
| 111 | + "colab_type": "text", |
| 112 | + "id": "ch7vaPiNd8Xc" |
128 | 113 | }, |
129 | 114 | "source": [ |
130 | 115 | "## Calculate the area of a regular polygon with 4 sides and length of a side is 20\n", |
|
142 | 127 | "* n = number of sides\n", |
143 | 128 | "* l = length of a side\n", |
144 | 129 | "\n", |
145 | | - "Now you have things clarified, give me the answer (the program to solve it without using the math module) in about 5–7 minutes. You can post the answer in the comment section below.\n", |
| 130 | + "Now you have things clarified, give me the answer (the program to solve it without using the math module) in about 5-7 minutes. You can post the answer in the comment section below.\n", |
146 | 131 | "\n", |
147 | 132 | "\n", |
148 | 133 | "\n", |
|
161 | 146 | }, |
162 | 147 | { |
163 | 148 | "cell_type": "code", |
| 149 | + "execution_count": 1, |
164 | 150 | "metadata": { |
165 | | - "id": "pp_A8CNEfNU_", |
166 | | - "colab_type": "code", |
167 | 151 | "colab": { |
168 | 152 | "base_uri": "https://localhost:8080/", |
169 | 153 | "height": 34 |
170 | 154 | }, |
| 155 | + "colab_type": "code", |
| 156 | + "id": "pp_A8CNEfNU_", |
171 | 157 | "outputId": "f351de5f-0d2b-4de8-9a96-c4f885aa72c1" |
172 | 158 | }, |
173 | | - "source": [ |
174 | | - "import math\n", |
175 | | - "area = (4 * math.pow(20, 2))/(4 * math.tan(math.pi/4))\n", |
176 | | - "print(area)" |
177 | | - ], |
178 | | - "execution_count": 1, |
179 | 159 | "outputs": [ |
180 | 160 | { |
| 161 | + "name": "stdout", |
181 | 162 | "output_type": "stream", |
182 | 163 | "text": [ |
183 | 164 | "400.00000000000006\n" |
184 | | - ], |
185 | | - "name": "stdout" |
| 165 | + ] |
186 | 166 | } |
| 167 | + ], |
| 168 | + "source": [ |
| 169 | + "import math\n", |
| 170 | + "area = (4 * math.pow(20, 2))/(4 * math.tan(math.pi/4))\n", |
| 171 | + "print(area)" |
187 | 172 | ] |
188 | 173 | }, |
189 | 174 | { |
190 | 175 | "cell_type": "markdown", |
191 | 176 | "metadata": { |
192 | | - "id": "i-wzJoIxfR2D", |
193 | | - "colab_type": "text" |
| 177 | + "colab_type": "text", |
| 178 | + "id": "i-wzJoIxfR2D" |
194 | 179 | }, |
195 | 180 | "source": [ |
196 | 181 | "\n", |
|
202 | 187 | "The `math.pow()` → It is a built-in power function that is used to calculate the exponent of the given number used in the math module.\n", |
203 | 188 | "\n", |
204 | 189 | "\n", |
205 | | - "```\n", |
| 190 | + "```python\n", |
206 | 191 | "pow(...) \n", |
207 | 192 | " pow(x, y) \n", |
208 | 193 | " Return x**y (x to the power of y).\n", |
|
215 | 200 | }, |
216 | 201 | { |
217 | 202 | "cell_type": "code", |
| 203 | + "execution_count": 2, |
218 | 204 | "metadata": { |
219 | | - "id": "NtQUWBn0gdLE", |
220 | | - "colab_type": "code", |
221 | 205 | "colab": { |
222 | 206 | "base_uri": "https://localhost:8080/", |
223 | 207 | "height": 34 |
224 | 208 | }, |
| 209 | + "colab_type": "code", |
| 210 | + "id": "NtQUWBn0gdLE", |
225 | 211 | "outputId": "5af667dd-0ae7-4860-adc2-5d38c7ea0bb2" |
226 | 212 | }, |
227 | | - "source": [ |
228 | | - "import math\n", |
229 | | - "print(math.pow(150, 4))" |
230 | | - ], |
231 | | - "execution_count": 2, |
232 | 213 | "outputs": [ |
233 | 214 | { |
| 215 | + "name": "stdout", |
234 | 216 | "output_type": "stream", |
235 | 217 | "text": [ |
236 | 218 | "506250000.0\n" |
237 | | - ], |
238 | | - "name": "stdout" |
| 219 | + ] |
239 | 220 | } |
| 221 | + ], |
| 222 | + "source": [ |
| 223 | + "import math\n", |
| 224 | + "print(math.pow(150, 4))" |
240 | 225 | ] |
241 | 226 | }, |
242 | 227 | { |
243 | 228 | "cell_type": "markdown", |
244 | 229 | "metadata": { |
245 | | - "id": "T_B0RZruggTa", |
246 | | - "colab_type": "text" |
| 230 | + "colab_type": "text", |
| 231 | + "id": "T_B0RZruggTa" |
247 | 232 | }, |
248 | 233 | "source": [ |
249 | 234 | "The `math.tan()` → It is used to calculate the tangent of a given number in radians.\n" |
|
252 | 237 | { |
253 | 238 | "cell_type": "markdown", |
254 | 239 | "metadata": { |
255 | | - "id": "eKxIPY-TgjwY", |
256 | | - "colab_type": "text" |
| 240 | + "colab_type": "text", |
| 241 | + "id": "eKxIPY-TgjwY" |
257 | 242 | }, |
258 | 243 | "source": [ |
259 | 244 | "\n", |
260 | 245 | "\n", |
261 | | - "```\n", |
| 246 | + "```python\n", |
262 | 247 | "tan(...) \n", |
263 | 248 | " tan(x) \n", |
264 | 249 | " Return the tangent of x (measured in radians).\n", |
|
269 | 254 | { |
270 | 255 | "cell_type": "markdown", |
271 | 256 | "metadata": { |
272 | | - "id": "gMTxZmnIgnGm", |
273 | | - "colab_type": "text" |
| 257 | + "colab_type": "text", |
| 258 | + "id": "gMTxZmnIgnGm" |
274 | 259 | }, |
275 | 260 | "source": [ |
276 | 261 | "## Calculate the tangent of 60\n" |
277 | 262 | ] |
278 | 263 | }, |
279 | 264 | { |
280 | 265 | "cell_type": "code", |
| 266 | + "execution_count": 3, |
281 | 267 | "metadata": { |
282 | | - "id": "l3XohnCSgq9X", |
283 | | - "colab_type": "code", |
284 | 268 | "colab": { |
285 | 269 | "base_uri": "https://localhost:8080/", |
286 | 270 | "height": 34 |
287 | 271 | }, |
| 272 | + "colab_type": "code", |
| 273 | + "id": "l3XohnCSgq9X", |
288 | 274 | "outputId": "4f8516e8-02c5-486e-a0cd-ecf0503d08cd" |
289 | 275 | }, |
290 | | - "source": [ |
291 | | - "import math\n", |
292 | | - "print(math.tan(60))" |
293 | | - ], |
294 | | - "execution_count": 3, |
295 | 276 | "outputs": [ |
296 | 277 | { |
| 278 | + "name": "stdout", |
297 | 279 | "output_type": "stream", |
298 | 280 | "text": [ |
299 | 281 | "0.320040389379563\n" |
300 | | - ], |
301 | | - "name": "stdout" |
| 282 | + ] |
302 | 283 | } |
| 284 | + ], |
| 285 | + "source": [ |
| 286 | + "import math\n", |
| 287 | + "print(math.tan(60))" |
303 | 288 | ] |
304 | 289 | }, |
305 | 290 | { |
306 | 291 | "cell_type": "markdown", |
307 | 292 | "metadata": { |
308 | | - "id": "enyfb98Jgs8t", |
309 | | - "colab_type": "text" |
| 293 | + "colab_type": "text", |
| 294 | + "id": "enyfb98Jgs8t" |
310 | 295 | }, |
311 | 296 | "source": [ |
312 | 297 | "The `math.pi` → As the name suggests is used as the constant value of Pi (3.14159265359). It returns the floating value of pi, rather than memorizing and then writing the value of pi, just use the math module.\n", |
|
318 | 303 | }, |
319 | 304 | { |
320 | 305 | "cell_type": "code", |
| 306 | + "execution_count": 4, |
321 | 307 | "metadata": { |
322 | | - "id": "vm4tPQM4gxj7", |
323 | | - "colab_type": "code", |
324 | 308 | "colab": { |
325 | 309 | "base_uri": "https://localhost:8080/", |
326 | 310 | "height": 34 |
327 | 311 | }, |
| 312 | + "colab_type": "code", |
| 313 | + "id": "vm4tPQM4gxj7", |
328 | 314 | "outputId": "498934be-c416-4612-d883-dd9e299a8609" |
329 | 315 | }, |
330 | | - "source": [ |
331 | | - "import math\n", |
332 | | - "print(math.pi)" |
333 | | - ], |
334 | | - "execution_count": 4, |
335 | 316 | "outputs": [ |
336 | 317 | { |
| 318 | + "name": "stdout", |
337 | 319 | "output_type": "stream", |
338 | 320 | "text": [ |
339 | 321 | "3.141592653589793\n" |
340 | | - ], |
341 | | - "name": "stdout" |
| 322 | + ] |
342 | 323 | } |
| 324 | + ], |
| 325 | + "source": [ |
| 326 | + "import math\n", |
| 327 | + "print(math.pi)" |
343 | 328 | ] |
344 | 329 | }, |
345 | 330 | { |
346 | 331 | "cell_type": "markdown", |
347 | 332 | "metadata": { |
348 | | - "id": "YlAJgpM1g0zn", |
349 | | - "colab_type": "text" |
| 333 | + "colab_type": "text", |
| 334 | + "id": "YlAJgpM1g0zn" |
350 | 335 | }, |
351 | 336 | "source": [ |
352 | 337 | "Back to the explanation.\n", |
|
387 | 372 | "Anyway, I hope that you guys learned something new today. Learning is the stepping stone of success. So keep learning. Stay tuned for more updates.\n", |
388 | 373 | "\n", |
389 | 374 | "\n", |
390 | | - "\n", |
391 | | - "\n", |
392 | | - "\n", |
393 | | - "\n", |
394 | | - "\n", |
395 | 375 | "---\n", |
396 | 376 | "\n", |
397 | 377 | "\n" |
398 | 378 | ] |
399 | 379 | } |
400 | | - ] |
| 380 | + ], |
| 381 | + "metadata": { |
| 382 | + "colab": { |
| 383 | + "authorship_tag": "ABX9TyOwgJhxR8T7UydRYxOPlUbq", |
| 384 | + "collapsed_sections": [], |
| 385 | + "include_colab_link": true, |
| 386 | + "name": "Learn the Python Math Module.ipynb", |
| 387 | + "provenance": [] |
| 388 | + }, |
| 389 | + "kernelspec": { |
| 390 | + "display_name": "Python 3", |
| 391 | + "name": "python3" |
| 392 | + } |
| 393 | + }, |
| 394 | + "nbformat": 4, |
| 395 | + "nbformat_minor": 0 |
401 | 396 | } |
0 commit comments