summaryrefslogtreecommitdiff
path: root/python/atms-310/notebooks/Week 02 W.ipynb
blob: 89360469b01e40e00c64bd471414e15cbe504f48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h1>3. Functions and Logic</h1>\n",
    "<h2>10/04/23</h2>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2>3.0 Last Time...</h2>\n",
    "\n",
    "<ul>\n",
    "    <li>A list is a <b>mutable</b>, <b>ordered</b> sequence of elements.</li>\n",
    "    <li>Python counts starting with 0, not 1.</li>\n",
    "    <li>You can also count backwards from the end of a list using negative numbers.</li>\n",
    "    <li>A list can be sliced, where the slice is defined by a range that has an inclusive lower bound and an exclusive upper bound.</li>\n",
    "    <li>Values in a list can be changed by assignment or by particular methods.</li>\n",
    "    <li>A tuple is an <b>immutable</b>, <b>ordered</b> sequence of elements.</li>\n",
    "    <li>A dictionary is a <b>mutable</b>, <b>unordered</b> sequence of elements consisting of key:value pairs.</li>\n",
    "</ul>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2>3.1 Functions</h2>\n",
    "\n",
    "A <b>function</b> is a self-contained block of code that can be \"called\" in any other code. It can take user-specified input and return output (which is generally a single output value: you can put multiple outputs into a list).\n",
    "\n",
    "To create a function, we start with a <b>def</b> statement that defines the name of the function and then follows with a list of input arguments in parentheses, followed by a colon.\n",
    "\n",
    "All the code that you want to be executed within a function should be indented (by default, this is typically indented by four spaces). The amount of whitespace before a command in Python matters! Everything indented together will be run together; there's no need to specify the end of the function."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [],
   "source": [
    "# You can start by defining a function that calculates the surface area\n",
    "# of a cylinder. \n",
    "# This can go in its own file so that you can refer to it in the future\n",
    "# from any code you may want to run.\n",
    "def area(radius, height):\n",
    "    a = 2*3.14*radius**2 + 2*3.14*radius*height\n",
    "    return a\n",
    "\n",
    "\n",
    "# Basically, this function is called 'area' and takes as input two variables\n",
    "# called 'radius' and 'height' from the user. \n",
    "# It calculates the surface area of a cylinder, then outputs or 'returns' \n",
    "# that area to the original code.\n",
    "\n",
    "# Running this block of code now will output nothing; you've just defined\n",
    "# the function."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "94.2\n"
     ]
    }
   ],
   "source": [
    "# Now, try calling the function using a radius of 3 and a height of 2.\n",
    "print(area(3,2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "94.2\n"
     ]
    }
   ],
   "source": [
    "# An even shorter version of that function would be:\n",
    "def area(radius, height):\n",
    "    return 2*3.14*radius**2 + 2*3.14*radius*height\n",
    "\n",
    "print(area(3,2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The arguments in this example ('radius' and 'height') are what's known as <b>positional arguments</b>, meaning that it knows the first argument corresponds to the radius and the second argument corresponds to the height.\n",
    "\n",
    "Alternatively, you can use what are known as <b>keyword arguments</b>."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "94.2\n"
     ]
    }
   ],
   "source": [
    "# Can write the arguments out of order if we're using keywords!\n",
    "print(area(height=2, radius=3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "62.8\n"
     ]
    }
   ],
   "source": [
    "# Note that this doesn't work with positional arguments:\n",
    "print(area(2,3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can facilitate long lists of arguments by treating positional arguments like lists and treating keyword arguments like dictionaries. To do this, in the function call, you just need to place an asterisk (\\*) before the list with positional arguments and two asterisks (\\*\\*) before the list with keyword arguments."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "94.2\n"
     ]
    }
   ],
   "source": [
    "# Call the function using a list of positional arguments.\n",
    "positional = [3,2] # plugging values from the list \"positional\" into area function\n",
    "print(area(*positional))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "94.2\n"
     ]
    }
   ],
   "source": [
    "# Call the function using a dictionary of keyword arguments.\n",
    "\n",
    "keyword = {'height':2, 'radius':3}\n",
    "print(area(**keyword))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "94.2\n"
     ]
    }
   ],
   "source": [
    "# Call the function using a combo of positional and keyword arguments.\n",
    "positional = [3]\n",
    "keyword = {'height':2}\n",
    "print(area(*positional,**keyword))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This seems like a silly way of going about it, but if you're passing dozens or hundreds of variables to a particular function, it definitely makes life easier!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2>3.2 Logical Constructs</h2>\n",
    "\n",
    "Logic in Python is handled through <b>if statements</b>. The general idea behind an if statement is that we check whether something is true: if it is, we then execute certain code before continuing.\n",
    "\n",
    "If statements are formatted by writing <b>if</b> and then some condition that must be met, followed by a colon. The block of code we wish to execute if the statement is true must all be indented together."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ass\n",
      "booty\n"
     ]
    }
   ],
   "source": [
    "# Write a code that checks whether a given variable is equal to 3.\n",
    "a = 3\n",
    "if a == 3:\n",
    "    print('ass')\n",
    "print('booty')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [],
   "source": [
    "# If the condition is not true, the code ignores what's indented.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can also specify what will be done if your conditional statement is not true by using <b>else:</b>."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hell no\n",
      "buttocks\n"
     ]
    }
   ],
   "source": [
    "# Now, if the condition is not true, the code continues with what follows\n",
    "# the else statement.\n",
    "a = 2\n",
    "if a == 3:\n",
    "    print('yes!')\n",
    "else:\n",
    "    print('hell no')\n",
    "\n",
    "print('buttocks')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Want to deal with more than one scenario? Use <b>elif</b>, which is short for <b>else if:</b>. In other words, if the previous condition is not true, but this new condition is true, do this instead."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "its 3\n",
      "end\n"
     ]
    }
   ],
   "source": [
    "a = 3\n",
    "if a == 3:\n",
    "    print('its 3')\n",
    "elif a == 2:\n",
    "    print('its 2')\n",
    "else: print('it aint 2 or 3')\n",
    "\n",
    "print('end')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Pay close attention to the indentation in the above code!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<b>An example!</b>\n",
    "\n",
    "Write an if/elif/else statement in the box below to check whether a particular variable is positive, negative, or equal to zero. Each possibility should print a string statement: \"I am positive.\", \"I am negative.\", or \"I am zero.\"\n",
    "\n",
    "Try testing it with different values to make sure it works!\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "its negative\n",
      "end\n"
     ]
    }
   ],
   "source": [
    "d = input('enter a number')\n",
    "f = int(d)\n",
    "if f == 0:\n",
    "    print('its zero')\n",
    "elif f < 0:\n",
    "    print('its negative')\n",
    "elif f > 0:\n",
    "    print('its positive')\n",
    "print('end')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2>TAKE-HOME POINTS</h2>\n",
    "\n",
    "<ul>\n",
    "    <li>A <b>function</b> is a block of code that can be called.</li>\n",
    "    <li>Functions can be called using either positional or keyword arguments.</li>\n",
    "    <li>Positional arguments can be described via lists and keyword arguments can be described via dictionaries.</li>\n",
    "    <li>Python uses if statements for logical constructs.</li>\n",
    "    <li>The code within an <b>if</b> statement is run if the current statement is true.</li>\n",
    "    <li>The code within an <b>elif</b> statement applies if prior statements are not true <b>and</b> the current statement is true.</li>\n",
    "    <li>The code within an <b>else</b> statement applies if all prior statements are not true.</li>\n",
    "</ul>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "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.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}