Skip to content

Commit 7c20001

Browse files
committed
Cleaned up narrative and and challenges for week 1
1 parent cd582dc commit 7c20001

File tree

1 file changed

+73
-49
lines changed

1 file changed

+73
-49
lines changed

README.org

Lines changed: 73 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@
5757

5858
4. Many keyboard shortcuts are available; see https://gist.github.com/discdiver/9e00618756d120a8c9fa344ac1c375ac
5959

60-
5. Jupyter Lab undestands (some) terminal commands
60+
5. (Optional) Jupyter Lab undestands (some) terminal commands
6161
#+BEGIN_SRC bash
6262
ls
6363
#+END_SRC
6464

65-
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)
6666
#+BEGIN_SRC python
6767
# Print current items in memory
6868
%dirs
@@ -77,6 +77,9 @@
7777
%%time
7878
#+END_SRC
7979

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+
8083
* Variables and Assignment
8184
** Use variables to store values
8285
Variables are names for values.
@@ -132,7 +135,8 @@ Order of operations matters!
132135
first = 1
133136
second = 5 * first
134137
first = 2
135-
print('first is', first, 'and second is', second)
138+
print('first:', first)
139+
print('second:', second)
136140
#+END_SRC
137141

138142
* Data Types and Type Conversion
@@ -201,9 +205,13 @@ Most data is text and numbers, but there are many other types.
201205
print(type(text))
202206
#+END_SRC
203207

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.
205213

206-
** Challenge: Explain what each operator does
214+
** *Challenge*: Explain what each operator does
207215
#+BEGIN_SRC python
208216
# Floor
209217
print('5 // 3:', 5 // 3)
@@ -236,21 +244,7 @@ print()
236244
print('after')
237245
#+END_SRC
238246

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
254248
#+BEGIN_SRC python
255249
# By default, we round to the nearest integer
256250
round(3.712)
@@ -275,6 +269,26 @@ round(3.712, 1)
275269
round(4.712823, ndigits=2)
276270
#+END_SRC
277271

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+
278292
** (Optional) Functions will typically generalize in sensible ways
279293
1. ~max()~ and ~min()~ do the intuitively correct thing with numerical and text data
280294
#+BEGIN_SRC python
@@ -307,7 +321,15 @@ round(3.712, 1)
307321
my_string.upper().isupper()
308322
#+END_SRC
309323

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.
311333
#+BEGIN_SRC python
312334
# More verbose help
313335
help(str)
@@ -318,7 +340,7 @@ round(3.712, 1)
318340
dir(my_string)
319341
#+END_SRC
320342

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
322344
#+BEGIN_SRC python
323345
bad_string_1 = " Hello world! "
324346
bad_string_2 = "|...goodbye cruel world|"
@@ -337,7 +359,7 @@ round(3.712, 1)
337359

338360
2. Python reports a runtime error when something goes wrong while a program is executing
339361

340-
** (Optional) Challenge: What happens when?
362+
** *(Optional) Beginner Challenge*: What happens when?
341363
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?
342364

343365
#+BEGIN_SRC python
@@ -368,7 +390,7 @@ help(math) # user friendly
368390
dir(math) # brief reminder, not user friendly
369391
#+END_SRC
370392

371-
** Import shortcuts
393+
** (Optional) Import shortcuts
372394
1. Import specific items from a library module. You want to be careful with this. It's safer to keep the namespace.
373395
#+BEGIN_SRC python
374396
from math import cos, pi
@@ -406,8 +428,8 @@ print("Fifth item:" , pressure[4])
406428
** You can get a subset of the list by slicing it
407429
1. You slice a list from the start position up to, but not including, the stop position
408430
#+BEGIN_SRC python
409-
print("First 3 items:", pressure[0:3])
410-
print("3rd through 5th:", pressure[2:5])
431+
print(pressure[0:3])
432+
print(pressure[2:5])
411433
#+END_SRC
412434

413435
2. You can omit the start position if you're starting at the beginning...
@@ -416,7 +438,7 @@ print("Fifth item:" , pressure[4])
416438
print("First 5 items, but shorter:", pressure[:5])
417439
#+END_SRC
418440

419-
3. ...and you can omit the end position if you're going to the end
441+
3. ...and you /must/ omit the end position if you're going to the end (otherwise it's up to, but not including, the end!)
420442
#+BEGIN_SRC python
421443
# This is useful if you don't know how long the list is
422444
print("Everything but the first 3 items:", pressure[3:])
@@ -446,20 +468,20 @@ cf. https://stackoverflow.com/a/11364711
446468
#+END_SRC
447469

448470
** Some other properties of indexes
449-
1. Indexing beyond the end of the collection is an error
471+
1. You can count backwards from the end with negative integers
450472
#+BEGIN_SRC python
451-
pressure[20]
473+
print("Last item:", pressure[-1])
452474
#+END_SRC
453475

454-
2. You can count backwards from the end with negative integers
476+
2. Indexing beyond the end of the collection is an error
455477
#+BEGIN_SRC python
456-
print("Last item:", pressure[-1])
478+
pressure[20]
457479
#+END_SRC
458480

459481
** Lists are mutable
460482
1. You can replace a value at a specific index location
461483
#+BEGIN_SRC python
462-
pressuressure[0] = 0.999
484+
pressure[0] = 0.999
463485
print(pressure)
464486
#+END_SRC
465487

@@ -583,29 +605,31 @@ len('carbon')
583605
#+END_SRC
584606

585607
** Building strings with ~.join()~
586-
#+BEGIN_SRC python
587-
date_list = ["3", "17", "2007"]
588-
date = "/".join(date_list)
589-
print(date)
590-
#+END_SRC
608+
1. Use ~.join()~ to concatenate strings
609+
#+BEGIN_SRC python
610+
date_list = ["3", "17", "2007"]
611+
date = "/".join(date_list)
612+
print(date)
613+
#+END_SRC
614+
615+
2. This is going to be useful for building CSV files
616+
#+BEGIN_SRC python
617+
date_list = ["3", "17", "2007"]
618+
date = ",".join(date_list)
619+
print(date)
620+
#+END_SRC
591621

592-
** (Optional) Challenge: From Strings to Lists and Back
622+
** *(Optional) Beginner Challenge*: From Strings to Lists and Back
593623
1. Given this Python code...
594624
#+BEGIN_SRC python
595625
print('string to list:', list('tin'))
596626
print('list to string:', ''.join(['g', 'o', 'l', 'd']))
597627
#+END_SRC
598628

599-
2. ...and this output:
600-
#+BEGIN_EXAMPLE
601-
string to list: ['t', 'i', 'n']
602-
list to string: gold
603-
#+END_EXAMPLE
604-
605-
3. What does ~list('some string')~ do?
606-
4. What does ~'-'.join(['x', 'y', 'z'])~ generate?
629+
2. What does ~list('some string')~ do?
630+
3. What does ~'-'.join(['x', 'y', 'z'])~ generate?
607631

608-
** (Optional) Challenge: Locating the right module
632+
** *Challenge*: Locating the right module
609633
You want to select a random character from a string:
610634
#+BEGIN_SRC python
611635
bases = 'ACTTGCTTGAC'
@@ -695,7 +719,7 @@ location = {'latitude': [37.28306, 'N'],
695719
print(location['longitude'][0])
696720
#+END_SRC
697721

698-
** Challenge: Convert a list to a dictionary
722+
** *(Optional) Advanced Challenge*: Convert a list to a dictionary
699723
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.
700724

701725
#+BEGIN_SRC python
@@ -911,7 +935,7 @@ data.columns = data.columns.str.strip("gdpPercap_")
911935
print(data.columns)
912936
#+END_SRC
913937

914-
** Use list slicing notation to get subsets of the data frame
938+
** TODO Move singleUse list slicing notation to get subsets of the data frame
915939
1. Select multiple columns or rows using ~.loc~ and a named slice. This generalizes the concept of a slice to include labeled indexes:
916940
#+BEGIN_SRC python
917941
# This returns a DataFrame

0 commit comments

Comments
 (0)