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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
|
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h1>1. Print, Calculator, Variable Types, and Operators</h1>\n",
"<h2>09/29/23</h2>\n",
"\n",
"<h2>1.0 Introduction to Jupyter Notebooks</h2>\n",
"First, this is a Jupyter Notebook, which enables a combination of markdown cells (like this one) to provide description alongside actual code that can be run. If you want to change a markdown cell, simply double-click on the text, make any changes you want to make, and click \"Run\" (the little play button) <b>above</b> to execute. You can add HTML-style tags to markdown cells to add details such as formatting and links.\n",
"\n",
"There are also code cells, which can actually be executed, just like in a normal programming environment. Click on the below code cell and try clicking \"Run\" (or type Shift+Enter). You can also modify the text that's being printed to the screen and try running it again."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2>1.1 Basic Variables</h2>\n",
"Python has <b>dynamic typing</b>, which means that variable types are set automatically based on the input. This is handy if you're coming from an environment like Fortran that requires you to specify each and every time what kind of variable you want to create!\n",
"\n",
"Here are some of the most important variable types (more on these in a bit):\n",
"<ul>\n",
" <li>Integer (short and long)</li>\n",
" <li>Floating point (float)</li>\n",
" <li>Strings</li>\n",
" <li>Booleans</li>\n",
" <li>NoneType</li>\n",
" <li>Lists and tuples</li> (Tuesday's class)\n",
" <li>Dictionaries</li> (Tuesday's class)\n",
"</ul>\n",
"\n",
"Arithmetic operators:\n",
"<ul>\n",
" <li>+ is addition</li>\n",
" <li>- is subtraction</li>\n",
" <li>/ is division</li>\n",
" <li>* is multiplication</li>\n",
" <li>** is exponentiation</li>\n",
"</ul>\n",
"\n",
"Comparison operators:\n",
"<ul>\n",
" <li>> is greater than</li>\n",
" <li>< is less than</li>\n",
" <li>>= is greater than or equal to</li>\n",
" <li><= is less than or equal to</li>\n",
" <li>!= is not equal to</li>\n",
" <li>== is equal to</li>\n",
"</ul>\n",
"\n",
"<b>Important note:</b> Python is case-sensitive! \"A\" is not the same thing as \"a\"."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Code that is preceded with a hashtag will not be read by the computer.\n",
"# These comments are intended for human readers of this code!\n",
"# Commenting your code is essential to ensure that you and others\n",
"# will be able to interpret it down the road!\n",
"\n",
"# Now, let's try playing with some numbers...\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note that all of the above are examples of Python's <b>dynamic typing</b>: assigning a variable a floating point value (i.e., including a decimal point) results in that variable being a float, whereas assigning a variable an integer value (i.e., no decimal point) results in that variable being an integer.\n",
"\n",
"You can also convert from one to the other using the int() and float() commands!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Convert a from a float to an integer.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Convert c from an integer to a float.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note that the conversion from float to int <b>loses information</b> along the way: Python merely truncates everything after the decimal point.\n",
"\n",
"On the other hand, the conversion from int to float is <b>essentially lossless</b>: the same information is maintained."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Now let's try some mathematical operators.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"When applying mathematical operations, Python tries to keep as much information as possible! This means that an integer and a float interacting together will result in a float as an answer. Likewise, division of integers will give you a float by default (this is new in version 3 of Python).\n",
"\n",
"<b>Why is a*b not exactly -7.56?</b>\n",
"\n",
"Floats are not exact numbers! There will always be some degree of rounding errors associated with floating point numbers (because we don't have infinite storage to keep an infinite number of digits after the decimal). As a result, it's good practice when comparing floats not to look for equality, but instead to look for \"close enough\" - there are functions to do this that we will see later!\n",
"\n",
"You can also represent scientific notation in Python!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Let's represent 6 x 10^23 in Python!\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2>1.2 Strings</h2>\n",
"\n",
"Strings are the text variable type in Python: they're surrounded either by single or double quotes (it generally doesn't matter which, as long as you're consistent at the start and finish)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you want to combine two strings, you can do so easily with the \"+\" command."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# To add a space, simply throw another string in there.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# How about combining a string and a float?\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The <b>TypeError</b> you get in the above example explains the problem: a float cannot be combined in this way with a string. But what if we want to make this change? That's where the str() function comes in handy."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Let's try that again!\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Sometimes you want a new line within the same string. This can be accomplished with the \"escape character\", which in Python is a backslash: \\\\. \"\\n\" will start a new line!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Creating a multi-line string using the newline character.\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you don't want to deal with newlines being created automatically, you can create a raw string!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# This will give you a nonsense result because \\n is being treated as a new line.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Instead, tell Python you're going to make a raw string by adding the letter 'r'.\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2>1.3 Boolean Variables</h2>\n",
"\n",
"A Boolean variable can only have two values: <b>True</b> and <b>False</b> (and remember, capitalization counts!) These are the results of the logical tests mentioned earlier!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can also combine conditions using <b>and</b> and <b>or</b>.\n",
"\n",
"<b>and</b>: Both statements must be <b>True</b> in order for the entire statement to be <b>True</b>.\n",
"\n",
"<b>or</b>: Either statement (or both) can be <b>True</b> in order for the entire statement to be <b>True</b>."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2>1.4 NoneType</h2>\n",
"\n",
"A NoneType variable can only have one value: <b>None</b> (and capitalization counts!). \n",
"\n",
"Why would we want this? Consider the scenario where you want to make sure there's no value already in a given variable (so, for example, if you're working with thousands of lines of code and the same variable types keep coming up). Setting that variable to None (via something like <b>a = None</b>) means you're guaranteed not to be messing around with any previous values by accident."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h1>TAKE-HOME POINTS</h1>\n",
"\n",
"<ul>\n",
" <li>Python has a variety of data types, many of which we saw today!</li>\n",
" <li>An <b>integer</b> variable is a number with no decimal point.</li>\n",
" <li>A <b>floating point</b> variable is a number with a decimal point</li>\n",
" <li>A <b>string</b> variable is text.</li>\n",
" <li>A <b>Boolean</b> variable can take on the values <b>True</b> and <b>False</b>.</li>\n",
" <li>A <b>NoneType</b> variable can take on the value <b>None</b>.</li>\n",
" <li>Python uses dynamic typing, which means you don't need to define a variable's type when you first use that variable.</li>\n",
" <li>The type() command lets you see what type a given variable is. int(), float(), and str() can be used to convert to different variable types.</li>\n",
" <li>Combining a float and an integer results in a float.</li>\n",
" <li>Combining a string and an integer or float results in a TypeError.</li>\n",
" <li>Floats are often prone to rounding errors.</li>\n",
" <li><b>and</b> and <b>or</b> can be used to combine Boolean variables.</li>\n",
"</ul>"
]
},
{
"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.10.10"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
|