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
Copy file name to clipboardExpand all lines: _sources/AdvancedFunctions/ChapterAssessment.rst
+14-25Lines changed: 14 additions & 25 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -136,27 +136,17 @@ Chapter Assessment
136
136
:autograde: unittest
137
137
:practice: T
138
138
139
-
We have provided the function ``checkingIfIn`` such that if the first input parameter is in the third, dictionary, input parameter, then the function returns that value, and otherwise, it returns ``False``. Follow the instructions in the active code window for specific variable assignmemts.
139
+
We have provided a function below and the skeleton of three invocations of the function. Fill in the parameters of the invocations to produce the specified outputs
Copy file name to clipboardExpand all lines: _sources/AdvancedFunctions/KeywordParameters.rst
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -247,7 +247,7 @@ For example,
247
247
:autograde: unittest
248
248
:practice: T
249
249
250
-
**6.** Currently the function is supposed to take 1 required parameter, and 2 optional parameters, however the code doesn't work. Fix the code so that it passes the test. This should only require changing one line of code.
250
+
**6.** Currently the function is supposed to take 1 required parameter, and 2 optional parameters, however the code doesn't work because a parameter name without a default value follows a parameter name with a default value. Fix the code so that it passes the test. This should only require changing one line of code.
Copy file name to clipboardExpand all lines: _sources/AdvancedFunctions/OptionalParameters.rst
+11-11Lines changed: 11 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -29,7 +29,15 @@ call in on a string, ``int("100")``, the return value will be the integer 100.
29
29
30
30
That's the most common way programmers want to convert strings to integers. Sometimes, however, they
31
31
are working with numbers in some other "base" rather than base 10. For example, in base 8, the rightmost
32
-
digit is ones, the next digit to the left is 8s, and the one to the left of that is the 64s place (8**2).
32
+
digit says how many ones, the next digit to the left says how many 8s, and the one to the left of that says how many 64s (64 is 8 squared).
33
+
34
+
.. note:: New Math
35
+
36
+
Some math educators believe that elementary school students will get a much deeper understanding
37
+
of the place-value system, and set a foundation for learning algebra later, if they learn to do
38
+
arithmetic not only in base-10 but also in base-8 and other bases. This was part of a movement
39
+
called `New Math <https://en.wikipedia.org/wiki/New_Math>`_, though it's not so new now. It was popular in the 1960s and 1970s in the USA. One of the authors of this textbook (Resnick) had some version of it in elementary school and credits it with ruining his mind, in a good way. Tom
40
+
Lehrer wrote a really funny song about it in 1965, and it's now set with visuals in several YouTube renditions. Try this very nice `lip-synched version <http://www.youtube.com/watch?v=DfCJgC2zezw>`_.
33
41
34
42
The int function provides an optional parameter for the base. When it is not specified, the number is
35
43
converted to an integer assuming the original number was in base 10. We say that 10 is the default value.
@@ -42,15 +50,7 @@ supplying a different value.
42
50
print(int("100", 10)) # same thing, 10 is the default value for the base
43
51
print(int("100", 8)) # now the base is 8, so the result is 1*64 = 64
44
52
45
-
.. note:: Tom Lehrer's New Math
46
53
47
-
Some math educators believe that elementary school students will get a much deeper understanding
48
-
of the place-value system, and set a foundation for learning algebra later, if they learn to do
49
-
arithmetic not only in base-10 but also in base-8 and other bases. This was part of a movement
50
-
called "The New Math", though it's not so new now (I had it when I was in elementary school!) Tom
51
-
Lehrer made a really funny song about it, and it's set with visuals in several YouTube renditions
52
-
now. Try this very nice `lip-synched version <http://www.youtube.com/watch?v=DfCJgC2zezw>`_.
53
-
54
54
When defining a function, you can specify a default value for a parameter. That parameter then becomes an
55
55
optional parameter when the function is called. The way to specify a default value is with an assignment
56
56
statement inside the parameter list. Consider the following code, for example.
@@ -76,7 +76,7 @@ specify a value for z without specifying a value for y.
76
76
77
77
.. note::
78
78
79
-
This is a second, related but slightly different use of = than we have seen previously. In a stand-alone assignment statement, not part of a function definition, ``x=3`` assigns 3 to the variable x. As part of specifying the parameters in a function definition, ``x=3`` says that 3 is the *default* value for x, used *only when* no value is provided during the function invocation.
79
+
This is a second, related but slightly different use of = than we have seen previously. In a stand-alone assignment statement, not part of a function definition, ``y=3`` assigns 3 to the variable y. As part of specifying the parameters in a function definition, ``y=3`` says that 3 is the *default* value for y, used *only when* no value is provided during the function invocation.
80
80
81
81
There are two tricky things that can confuse you with default values. The first is that the default
82
82
value is determined at the time that the function is defined, not at the time that it is invoked. So
@@ -96,7 +96,7 @@ value 7 when f is invoked without specifying a value for z.
96
96
97
97
The second tricky thing is that if the default value is set to a mutable object, such as a list or a dictionary,
98
98
that object will be shared in all invocations of the function. This can get very confusing, so I suggest that you
99
-
never set a default value that is a mutable object. For example, follow the exceution of this one carefully.
99
+
never set a default value that is a mutable object. For example, follow the execution of this one carefully.
Copy file name to clipboardExpand all lines: _sources/Dictionaries/AccumulatingtheBestKey.rst
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -72,7 +72,7 @@ but you'll learn more if you try to write it yourself first.
72
72
:autograde: unittest
73
73
:practice: T
74
74
75
-
**1.** Create a dictionary called ``d`` that keeps track of all the characters in the string ``placement`` and notes how many times each character was seen. Then, find the key with the lowest value in this dictionary and assign that key to ``min_value``.
75
+
**1.** Create a dictionary called ``d`` that keeps track of all the characters in the string ``placement`` and notes how many times each character was seen. Then, find the key with the lowest value in this dictionary and assign that key to ``key_with_min_value``.
76
76
~~~~
77
77
placement = "Beaches are cool places to visit in spring however the Mackinaw Bridge is near. Most people visit Mackinaw later since the island is a cool place to explore."
78
78
@@ -86,7 +86,7 @@ but you'll learn more if you try to write it yourself first.
86
86
self.assertEqual(sorted(d.keys()), sorted(['B', 'e', 'a', 'c', 'h', 's', ' ', 'r', 'o', 'l', 'p', 't', 'v', 'i', 'n', 'g', 'w', 'M', 'k', 'd', '.', 'x']), "Testing the keys were created correctly")
87
87
self.assertEqual(sorted(d.values()), sorted([2, 17, 12, 8, 4, 10, 27, 7, 10, 8, 6, 8, 3, 13, 7, 2, 3, 3, 2, 2, 2, 1]), "Testing the values were created correctly")
88
88
def testTwo(self):
89
-
self.assertEqual(min_value, "x", "Testing that min_value has been correctly assigned")
89
+
self.assertEqual(key_with_min_value, "x", "Testing that key_with_min_value has been correctly assigned")
90
90
91
91
myTests().main()
92
92
@@ -95,7 +95,7 @@ but you'll learn more if you try to write it yourself first.
95
95
:autograde: unittest
96
96
:practice: T
97
97
98
-
**5.** Create a dictionary called ``lett_d`` that keeps track of all of the characters in the string ``product`` and notes how many times each character was seen. Then, find the key with the highest value in this dictionary and assign that key to ``max_value``.
98
+
**5.** Create a dictionary called ``lett_d`` that keeps track of all of the characters in the string ``product`` and notes how many times each character was seen. Then, find the key with the highest value in this dictionary and assign that key to ``key_with_max_value``.
99
99
~~~~
100
100
product = "iphone and android phones"
101
101
@@ -108,6 +108,6 @@ but you'll learn more if you try to write it yourself first.
108
108
def testOne(self):
109
109
self.assertEqual(sorted(lett_d.items()), sorted([('h', 2), ('a', 2), (' ', 3), ('n', 4), ('d', 3), ('o', 3), ('i', 2), ('p', 2), ('e', 2), ('r', 1), ('s', 1)]), "Testing that lett_d has been created correctly.")
110
110
def testTwo(self):
111
-
self.assertEqual(max_value, "n", "Testing that max_value has been correctly assigned")
111
+
self.assertEqual(key_with_max_value, "n", "Testing that key_with_max_value has been correctly assigned")
Copy file name to clipboardExpand all lines: _sources/Dictionaries/ChapterAssessment.rst
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -173,7 +173,7 @@ Assessment - Dictionaries
173
173
:practice: T
174
174
:topics: Dictionaries/intro-Dictionaries
175
175
176
-
Provided is a dictionary called ``US_medals`` which has the first 70 metals that the United States has won in 2016, and in which category they have won it in. Using dictionary mechanics, assign the value of the key ``"Fencing"`` to a variable ``fencing_value``. Remember, do not hard code this.
176
+
Provided is a dictionary called ``US_medals`` which has the first 70 medals that the United States has won in 2016, and in which category they have won it in. Using dictionary mechanics, assign the value of the key ``"Fencing"`` to a variable ``fencing_value``. Remember, do not hard code this.
Copy file name to clipboardExpand all lines: _sources/Dictionaries/Dictionarymethods.rst
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -245,7 +245,7 @@ a key, ``get`` returns 0 (instead of None).
245
245
:autograde: unittest
246
246
:practice: T
247
247
248
-
**5.** We have a dictionary of the specific events that Italy has won medals in and the number of medals they have won for each event. Assign to the variable ``events`` a list of the keys from the dictionary ``medal_events``. Do not hard code this.
248
+
**5.** We have a dictionary of the specific events that Italy has won medals in and the number of medals they have won for each event. Assign to the variable ``events`` a list of the keys from the dictionary ``medal_events``. Use a dictionary method and cast to a list; do not hard code or accumulate a list via iteration.
Copy file name to clipboardExpand all lines: _sources/Dictionaries/intro-Dictionaries.rst
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -130,7 +130,7 @@ The key ``'two'`` yields the value ``'dos'``. The key ``one`` yields the value `
130
130
:autograde: unittest
131
131
:practice: T
132
132
133
-
**4.** You are keeping track of olympic medals for Italy in the 2016 Rio Summer Olympics! At the moment, Italy has 7 gold medals, 8 silver metals, and 6 bronze medals. Create a dictionary called ``olympics`` where the keys are the types of medals, and the values are the number of that type of medals that Italy has won so far.
133
+
**4.** You are keeping track of olympic medals for Italy in the 2016 Rio Summer Olympics! At the moment, Italy has 7 gold medals, 8 silver medals, and 6 bronze medals. Create a dictionary called ``olympics`` where the keys are the types of medals, and the values are the number of that type of medals that Italy has won so far.
Copy file name to clipboardExpand all lines: _sources/Files/Iteratingoverlinesinafile.rst
+9-9Lines changed: 9 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -33,21 +33,21 @@ file as a string of characters. The general pattern for processing each line of
33
33
statement2
34
34
...
35
35
36
-
To process all of our olypmics data, we will use a *for* loop to iterate over the lines of the file. Using
36
+
To process all of our olympics data, we will use a *for* loop to iterate over the lines of the file. Using
37
37
the ``split`` method, we can break each line into a list containing all the fields of interest about the
38
38
athlete. We can then take the values corresponding to name, team and event to
39
39
construct a simple sentence.
40
40
41
41
.. activecode:: ac9_5_1
42
42
:nocodelens:
43
43
44
-
olypmicsfile = open("olypmics.txt", "r")
44
+
olympicsfile = open("olympics.txt", "r")
45
45
46
-
for aline in olypmicsfile.readlines():
46
+
for aline in olympicsfile.readlines():
47
47
values = aline.split(",")
48
48
print(values[0], "is from", values[3], "and is on the roster for", values[4])
49
49
50
-
olypmicsfile.close()
50
+
olympicsfile.close()
51
51
52
52
To make the code a little simpler, and to allow for more efficient processing, Python provides a built-in way to
53
53
iterate through the contents of a file one line at a time, without first reading them all into a list. Some students find this confusing initially, so we don't recommend doing it this way, until you get a
@@ -57,17 +57,17 @@ to read it. And when you start dealing with big files, you may notice the effici
57
57
.. activecode:: ac9_5_2
58
58
:nocodelens:
59
59
60
-
olypmicsfile = open("olypmics.txt", "r")
60
+
olympicsfile = open("olympics.txt", "r")
61
61
62
-
for aline in olypmicsfile:
62
+
for aline in olympicsfile:
63
63
values = aline.split(",")
64
64
print(values[0], "is from", values[3], "and is on the roster for", values[4])
65
65
66
-
olypmicsfile.close()
66
+
olympicsfile.close()
67
67
68
68
.. raw:: html
69
69
70
-
<prehiddenid="olypmics.txt">
70
+
<prehiddenid="olympics.txt">
71
71
Name,Sex,Age,Team,Event,Medal
72
72
A Dijiang,M,24,China,Basketball,NA
73
73
A Lamusi,M,23,China,Judo,NA
@@ -127,6 +127,6 @@ to read it. And when you start dealing with big files, you may notice the effici
127
127
128
128
def testOne(self):
129
129
self.assertEqual(num_lines, 7, "Testing that num_lines was assigned to the correct value.")
130
-
self.assertNotIn('len', self.getEditorText(), "Testing your code (Don't worry about actual and expected values).")
130
+
self.assertNotIn('len', self.getEditorText(), "Checking that you didn't use the len function.")
Copy file name to clipboardExpand all lines: _sources/Files/ReadingCSVFiles.rst
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -46,7 +46,7 @@ Once we have parsed the lines into their separate values, we can use those value
46
46
47
47
Note that the trick of splitting the text for each row based on the presence of commas only works because commas are not used in any of the field values. Suppose that some of our events were more specific, and used commas. For example, "Swimming, 100M Freestyle". How will a program processing a .csv file know when a comma is separating columns, and when it is just part of the text string giving a value within a column?
48
48
49
-
The CSV format is actually a little more general than we have described and has a couple of solutions for that problem. One alternative format uses a different column separator, such as | or a tab (\t). Sometimes, when a tab is used, the format is called tsv, for tab-separated values). If you get a file using a different separator, you can just call the ``.split('|')`` or ``.split('\\t')``.
49
+
The CSV format is actually a little more general than we have described and has a couple of solutions for that problem. One alternative format uses a different column separator, such as | or a tab (\\t). Sometimes, when a tab is used, the format is called tsv, for tab-separated values). If you get a file using a different separator, you can just call the ``.split('|')`` or ``.split('\t')``.
50
50
51
51
The other advanced CSV format uses commas to separate but encloses all values in double quotes.
0 commit comments