You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
6. Jupyter Lab (IPython, actually) has "magic" commands that start with ~%~ (line) or ~%%~ (cell)
65
+
6. (Optional) Jupyter Lab (IPython, actually) has "magic" commands that start with ~%~ (line) or ~%%~ (cell)
66
66
#+BEGIN_SRC python
67
67
# Print current items in memory
68
68
%dirs
@@ -77,6 +77,9 @@
77
77
%%time
78
78
#+END_SRC
79
79
80
+
- The magic command must be in the *first line* of the cell (no comments)
81
+
- Some commands are not available on Windows (e.g. ~%%bash~)
82
+
80
83
* Variables and Assignment
81
84
** Use variables to store values
82
85
Variables are names for values.
@@ -132,7 +135,8 @@ Order of operations matters!
132
135
first = 1
133
136
second = 5 * first
134
137
first = 2
135
-
print('first is', first, 'and second is', second)
138
+
print('first:', first)
139
+
print('second:', second)
136
140
#+END_SRC
137
141
138
142
* Data Types and Type Conversion
@@ -201,9 +205,13 @@ Most data is text and numbers, but there are many other types.
201
205
print(type(text))
202
206
#+END_SRC
203
207
204
-
4. This can work for more complex data types as well, e.g. Pandas data frames and Numpy arrays.
208
+
4. What's going on under the hood?
209
+
1. ~int~, ~float~, and ~str~ are types. More precisely, they are /classes/.
210
+
2. ~int()~, ~float()~, and ~str()~ are functions that create new /instances/ of their respective classes. The argument to the creation function (e.g., ~'2'~) is the raw material for creating the new instance.
211
+
212
+
5. This can work for more complex data types as well, e.g. Pandas data frames and Numpy arrays.
205
213
206
-
** Challenge: Explain what each operator does
214
+
** *Challenge*: Explain what each operator does
207
215
#+BEGIN_SRC python
208
216
# Floor
209
217
print('5 // 3:', 5 // 3)
@@ -236,21 +244,7 @@ print()
236
244
print('after')
237
245
#+END_SRC
238
246
239
-
** Every function returns something
240
-
1. Collect the results of a function in a new variable. This is one of the ways we build complex programs.
241
-
#+BEGIN_SRC python
242
-
result = len("hello")
243
-
print(result)
244
-
#+END_SRC
245
-
246
-
2. (Optional) Some function only have "side effects"; they return ~None~
247
-
#+BEGIN_SRC python
248
-
result = print("hello")
249
-
print(result)
250
-
print(type(result))
251
-
#+END_SRC
252
-
253
-
** Functions can have optional parameters
247
+
** Functions can have optional arguments
254
248
#+BEGIN_SRC python
255
249
# By default, we round to the nearest integer
256
250
round(3.712)
@@ -275,6 +269,26 @@ round(3.712, 1)
275
269
round(4.712823, ndigits=2)
276
270
#+END_SRC
277
271
272
+
** Every function returns something
273
+
1. Collect the results of a function in a new variable. This is one of the ways we build complex programs.
274
+
#+BEGIN_SRC python
275
+
# You can optionally specify the number of significant digits
276
+
rounded_num = round(4.712823, ndigits=2)
277
+
print(rounded_num)
278
+
#+END_SRC
279
+
280
+
#+BEGIN_SRC python
281
+
result = len("hello")
282
+
print(result)
283
+
#+END_SRC
284
+
285
+
2. (Optional) Some function only have "side effects"; they return ~None~
286
+
#+BEGIN_SRC python
287
+
result = print("hello")
288
+
print(result)
289
+
print(type(result))
290
+
#+END_SRC
291
+
278
292
** (Optional) Functions will typically generalize in sensible ways
279
293
1. ~max()~ and ~min()~ do the intuitively correct thing with numerical and text data
280
294
#+BEGIN_SRC python
@@ -307,7 +321,15 @@ round(3.712, 1)
307
321
my_string.upper().isupper()
308
322
#+END_SRC
309
323
310
-
3. You can view an object's attributes (i.e. methods and fields) using ~help()~ or ~dir()~. Some attributes are "private"; you're not supposed to use these directly.
324
+
3. (Optional) Strings are immutable. This will be covered later, but may come up here. If it comes up here, this is a good example:
325
+
#+BEGIN_SRC python
326
+
print(my_string.upper())
327
+
print(my_string)
328
+
upper_string = my_string.upper()
329
+
print(upper_string)
330
+
#+END_SRC
331
+
332
+
4. You can view an object's attributes (i.e. methods and fields) using ~help()~ or ~dir()~. Some attributes are "private"; you're not supposed to use these directly.
311
333
#+BEGIN_SRC python
312
334
# More verbose help
313
335
help(str)
@@ -318,7 +340,7 @@ round(3.712, 1)
318
340
dir(my_string)
319
341
#+END_SRC
320
342
321
-
4. The built-in string methods can be very useful for cleaning up data
343
+
5. The built-in string methods can be very useful for cleaning up data
322
344
#+BEGIN_SRC python
323
345
bad_string_1 = " Hello world! "
324
346
bad_string_2 = "|...goodbye cruel world|"
@@ -337,7 +359,7 @@ round(3.712, 1)
337
359
338
360
2. Python reports a runtime error when something goes wrong while a program is executing
339
361
340
-
** (Optional) Challenge: What happens when?
362
+
** *(Optional) Beginner Challenge*: What happens when?
341
363
Explain in simple terms the order of operations in the following program: when does the addition happen, when does the subtraction happen, when is each function called, etc. What is the final value of radiance?
342
364
343
365
#+BEGIN_SRC python
@@ -368,7 +390,7 @@ help(math) # user friendly
368
390
dir(math) # brief reminder, not user friendly
369
391
#+END_SRC
370
392
371
-
** Import shortcuts
393
+
** (Optional) Import shortcuts
372
394
1. Import specific items from a library module. You want to be careful with this. It's safer to keep the namespace.
** *(Optional) Advanced Challenge*: Convert a list to a dictionary
699
723
How can you convert our list of names and ages into a dictionary? Hint: You will need to populate the dictionary with a list of keys and a list of values.
0 commit comments