Skip to content

Commit a7775ae

Browse files
committed
Fix: Use real codelens not from rs-substitutes
1 parent add629a commit a7775ae

72 files changed

Lines changed: 1430 additions & 824 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

pretext/ClassesBasics/ImprovingourConstructor.ptx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,19 @@
55
provide some additional capability for the user to pass information to the constructor. Since constructors are simply specially named functions, we can use parameters (as we've seen before) to provide the specific information.</p>
66
<p>We can make our class constructor more general by putting extra parameters into
77
the <c>__init__</c> method, as shown in this codelens example.</p>
8-
<exercise runestone="chp13_improveconstructor"/>
8+
<program xml:id="chp13_improveconstructor" interactive="codelens" language="python">
9+
<input>
10+
class Point:
11+
""" Point class for representing and manipulating x,y coordinates. """
12+
13+
def __init__(self, initX, initY):
14+
""" Create a new point at the given coordinates. """
15+
self.x = initX
16+
self.y = initY
17+
18+
p = Point(7, 6)
19+
</input>
20+
</program>
921
<p>Now when we create new points, we supply the x and y coordinates as parameters. When the point is created, the values of <c>initX</c> and <c>initY</c> are assigned to the state of the object.</p>
1022
<image source="ClassesBasics/Figures/objectpic5.png" width="50%" alt="Simple object has state and methods"/>
1123
</section>

pretext/ClassesBasics/UserDefinedClasses.ptx

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<?xml version="1.0"?>
22
<section xml:id="classes-basics_user-defined-classes">
33
<title>User Defined Classes</title>
4-
<p>We've already seen classes like <c>str</c>, <c>int</c>, <c>float</c> and <c>Turtle</c>. These were defined by Python and
4+
<p>We've already seen classes like <c>str</c>, <c>int</c>, <c>float</c> and <c>Turtle</c>. These were defined by Python and
55
made available for us to use. However, in many cases when we are solving problems we need to create data objects
66
that are related to the problem we are trying to solve. We need to create our own classes.</p>
77
<p>As an example, consider the concept of a mathematical point. In two dimensions, a point is two
88
numbers (coordinates) that are treated collectively as a single object.
99
Points are often written in parentheses with a comma
1010
separating the coordinates. For example, <c>(0, 0)</c> represents the origin, and
11-
<c>(x, y)</c> represents the point <c>x</c> units to the right and <c>y</c> units up
11+
<c>(x, y)</c> represents the point <c>x</c> units to the right and <c>y</c> units up
1212
from the origin. This <c>(x,y)</c> is the state of the point.</p>
1313
<p>Thinking about our diagram above, we could draw a <c>point</c> object as shown here.</p>
1414
<image source="ClassesBasics/Figures/objectpic2.png" width="50%" alt="A point has an x and a y"/>
@@ -31,7 +31,7 @@ class Point:
3131
""" Create a new point at the origin """
3232
self.x = 0
3333
self.y = 0
34-
</input>
34+
</input>
3535
</program>
3636
<p>Class definitions can appear anywhere in a program, but they are usually near
3737
the beginning (after the <c>import</c> statements). The syntax rules for a class
@@ -63,7 +63,7 @@ p = Point() # Instantiate an object of type Point
6363
q = Point() # and make a second point
6464

6565
print("Nothing seems to have happened with the points")
66-
</input>
66+
</input>
6767
</program>
6868
<p>During the initialization of the objects, we created two
6969
attributes called <title_reference>x</title_reference> and <title_reference>y</title_reference> for each, and gave them both the value 0.</p>
@@ -77,7 +77,22 @@ print("Nothing seems to have happened with the points")
7777
having an x and y coordinate with value 0. However, because we have not asked the point to do anything, we don't see any other result.</p>
7878
<image source="ClassesBasics/Figures/objectpic4.png" width="50%" alt="Simple object has state and methods"/>
7979
<p>You can see this for yourself, via codelens:</p>
80-
<exercise runestone="chp13_points"/>
80+
<program xml:id="chp13_points" interactive="codelens" language="python">
81+
<input>
82+
class Point:
83+
""" Point class for representing and manipulating x,y coordinates. """
84+
85+
def __init__(self):
86+
""" Create a new point at the origin """
87+
self.x = 0
88+
self.y = 0
89+
90+
p = Point() # Instantiate an object of type Point
91+
q = Point() # and make a second point
92+
93+
print("Nothing seems to have happened with the points")
94+
</input>
95+
</program>
8196
<p>The following program adds a few print statements. You can see that the output suggests that each one is a <c>Point object</c>.
8297
However, notice that the <c>is</c> operator returns <c>False</c> meaning that they are different objects (we will have more to say about this in a later chapter).</p>
8398
<program xml:id="chp13_classes2" interactive="activecode" language="python">
@@ -97,7 +112,7 @@ print(p)
97112
print(q)
98113

99114
print(p is q)
100-
</input>
115+
</input>
101116
</program>
102117
<p>This should look familiar &#x2014; we've used classes before to create
103118
more than one object:</p>
@@ -107,7 +122,7 @@ from turtle import Turtle
107122

108123
tess = Turtle() # Instantiate objects of type Turtle
109124
alex = Turtle()
110-
</input>
125+
</input>
111126
</program>
112127
<p>The variables <c>p</c> and <c>q</c> are assigned references to two new <c>Point</c> objects.
113128
A function like <c>Turtle</c> or <c>Point</c> that creates a new object instance
@@ -121,7 +136,8 @@ alex = Turtle()
121136
production line, its initialization method is executed to
122137
get the object properly set up with its factory default settings.</p>
123138
<p>The combined process of <q>make me a new object</q> and <q>get its settings initialized
124-
to the factory default settings</q> is called <term>instantiation</term>.</p>
139+
to the factory default settings</q> is called <term>instantiation</term>.
140+
</p>
125141
<p>
126142
<term>Check Your Understanding</term>
127143
</p>
@@ -142,7 +158,7 @@ x = BMW is Tesla
142158
y = type(BMW)==type(Tesla)
143159

144160
print(x, y)
145-
</input>
161+
</input>
146162
</program>
147163
</statement>
148164
<choices>
@@ -152,31 +168,31 @@ print(x, y)
152168
</statement>
153169
<feedback>
154170
Look closely at how the objects are instantiated.
155-
</feedback>
171+
</feedback>
156172
</choice>
157173
<choice>
158174
<statement>
159175
<p>True False</p>
160176
</statement>
161177
<feedback>
162178
Look closely at how the objects are instantiated and the types of the objects.
163-
</feedback>
179+
</feedback>
164180
</choice>
165181
<choice correct="yes">
166182
<statement>
167183
<p>False True</p>
168184
</statement>
169185
<feedback>
170186
Correct, the BMW object is not the Tesla object but they are of the same type.
171-
</feedback>
187+
</feedback>
172188
</choice>
173189
<choice>
174190
<statement>
175191
<p>False False</p>
176192
</statement>
177193
<feedback>
178194
Look closer at types of the objects.
179-
</feedback>
195+
</feedback>
180196
</choice>
181197
</choices>
182198
</exercise>

pretext/Dictionaries/Dictionaryoperations.ptx

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,36 @@
44
<p>The <c>del</c> statement removes a key-value pair from a dictionary. For example,
55
the following dictionary contains the names of various fruits and the number of
66
each fruit in stock. If someone buys all of the pears, we can remove the entry from the dictionary.</p>
7-
<exercise runestone="ch12_dict4"/>
7+
<program xml:id="ch12_dict4" interactive="codelens" language="python">
8+
<input>
9+
inventory = {'apples': 430, 'bananas': 312, 'oranges': 525, 'pears': 217}
10+
11+
del inventory['pears']
12+
</input>
13+
</program>
14+
815
<p>Dictionaries are also mutable. As we've seen before with lists, this means that the dictionary can
916
be modified by referencing an association on the left hand side of the assignment statement. In the previous
10-
example, instead of deleting the entry for <c>pears</c>, we could have set the inventory to <c>0</c>.</p>
11-
<exercise runestone="ch12_dict4a"/>
17+
example, instead of deleting the entry for <c>pears</c>, we could have set the inventory to <c>0</c>.
18+
</p>
19+
<program xml:id="ch12_dict4a" interactive="codelens" language="python">
20+
<input>
21+
inventory = {'apples': 430, 'bananas': 312, 'oranges': 525, 'pears': 217}
22+
23+
inventory['pears'] = 0
24+
</input>
25+
</program>
1226
<p>Similarily,
1327
a new shipment of 200 bananas arriving could be handled like this.</p>
14-
<exercise runestone="ch12_dict5"/>
28+
<program xml:id="ch12_dict5" interactive="codelens" language="python">
29+
<input>
30+
inventory = {'apples': 430, 'bananas': 312, 'oranges': 525, 'pears': 217}
31+
inventory['bananas'] = inventory['bananas'] + 200
32+
33+
34+
numItems = len(inventory)
35+
</input>
36+
</program>
1537
<p>Notice that there are now 512 bananas&#x2014;the dictionary has been modified. Note also that the <c>len</c> function also works on dictionaries. It returns the number
1638
of key-value pairs:</p>
1739
<p>
@@ -25,7 +47,7 @@
2547
mydict = {"cat":12, "dog":6, "elephant":23}
2648
mydict["mouse"] = mydict["cat"] + mydict["dog"]
2749
print(mydict["mouse"])
28-
</input>
50+
</input>
2951
</program>
3052
</statement>
3153
<choices>
@@ -35,31 +57,31 @@ print(mydict["mouse"])
3557
</statement>
3658
<feedback>
3759
12 is associated with the key cat.
38-
</feedback>
60+
</feedback>
3961
</choice>
4062
<choice>
4163
<statement>
4264
<p>0</p>
4365
</statement>
4466
<feedback>
4567
The key mouse will be associated with the sum of the two values.
46-
</feedback>
68+
</feedback>
4769
</choice>
4870
<choice correct="yes">
4971
<statement>
5072
<p>18</p>
5173
</statement>
5274
<feedback>
5375
Yes, add the value for cat and the value for dog (12 + 6) and create a new entry for mouse.
54-
</feedback>
76+
</feedback>
5577
</choice>
5678
<choice>
5779
<statement>
5880
<p>Error, there is no entry with mouse as the key.</p>
5981
</statement>
6082
<feedback>
6183
Since the new key is introduced on the left hand side of the assignment statement, a new key-value pair is added to the dictionary.
62-
</feedback>
84+
</feedback>
6385
</choice>
6486
</choices>
6587
</exercise>

pretext/Dictionaries/intro-Dictionaries.ptx

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,24 @@
55
lists, and tuples &#x2014; are sequential collections. This means that the items in the collection are
66
ordered from left to right and they use integers as indices to access
77
the values they contain.</p>
8-
<p><term>Dictionaries</term> are a different kind of collection. They are Python's
8+
<p>
9+
<term>Dictionaries</term> are a different kind of collection. They are Python's
910
built-in <term>mapping type</term>. A map is an unordered, associative collection. The association, or mapping,
1011
is from a <term>key</term>, which can be any immutable type,
1112
to a <term>value</term>, which can be any Python data object.</p>
1213
<p>As an example, we will create a dictionary to translate English words into
1314
Spanish. For this dictionary, the keys are strings and the values will also be strings.</p>
1415
<p>One way to create a dictionary is to start with the empty dictionary and add
15-
<term>key-value pairs</term>. The empty dictionary is denoted <c>{}</c></p>
16-
<exercise runestone="chp12_dict1"/>
16+
<term>key-value pairs</term>. The empty dictionary is denoted <c>{}</c>
17+
</p>
18+
<program xml:id="chp12_dict1" interactive="codelens" language="python">
19+
<input>
20+
eng2sp = {}
21+
eng2sp['one'] = 'uno'
22+
eng2sp['two'] = 'dos'
23+
eng2sp['three'] = 'tres'
24+
</input>
25+
</program>
1726
<p>The first assignment creates an empty dictionary named <c>eng2sp</c>. The other
1827
assignments add new key-value pairs to the dictionary. The left hand side gives the dictionary and the key being associated. The right hand side gives the value being associated with that key.
1928
We can print the current
@@ -26,20 +35,34 @@
2635
For our purposes we can think of this ordering as unpredictable.</p>
2736
<p>Another way to create a dictionary is to provide a list of key-value pairs
2837
using the same syntax as the previous output.</p>
29-
<exercise runestone="chp12_dict2"/>
38+
<program xml:id="chp12_dict2" interactive="codelens" language="python">
39+
<input>
40+
eng2sp = {'three': 'tres', 'one': 'uno', 'two': 'dos'}
41+
print(eng2sp)
42+
</input>
43+
</program>
44+
3045
<p>It doesn't matter what order we write the pairs. The values in a dictionary are
3146
accessed with keys, not with indices, so there is no need to care about
3247
ordering.</p>
3348
<p>Here is how we use a key to look up the corresponding value.</p>
34-
<exercise runestone="chp12_dict3"/>
35-
<p>The key <c>'two'</c> yields the value <c>'dos'</c>.</p>
49+
<program xml:id="chp12_dict3" interactive="codelens" language="python">
50+
<input>
51+
eng2sp = {'three': 'tres', 'one': 'uno', 'two': 'dos'}
52+
53+
value = eng2sp['two']
54+
print(value)
55+
</input>
56+
</program>
57+
<p>The key <c>'two'</c> yields the value <c>'dos'</c>.
58+
</p>
3659
<note>
3760
<p>This workspace is provided for your convenience. You can use this activecode window to try out anything you like.</p>
3861
<program xml:id="scratch_11_01" interactive="activecode" language="python">
3962
<input>
4063

4164

42-
</input>
65+
</input>
4366
</program>
4467
</note>
4568
<p>
@@ -56,15 +79,15 @@
5679
</statement>
5780
<feedback>
5881
Dictionaries associate keys with values but there is no assumed order for the entries.
59-
</feedback>
82+
</feedback>
6083
</choice>
6184
<choice correct="yes">
6285
<statement>
6386
<p>True</p>
6487
</statement>
6588
<feedback>
6689
Yes, dictionaries are associative collections meaning that they store key-value pairs.
67-
</feedback>
90+
</feedback>
6891
</choice>
6992
</choices>
7093
</exercise>
@@ -75,7 +98,7 @@
7598
<input>
7699
mydict = {"cat":12, "dog":6, "elephant":23}
77100
print(mydict["dog"])
78-
</input>
101+
</input>
79102
</program>
80103
</statement>
81104
<choices>
@@ -85,31 +108,31 @@ print(mydict["dog"])
85108
</statement>
86109
<feedback>
87110
12 is associated with the key cat.
88-
</feedback>
111+
</feedback>
89112
</choice>
90113
<choice correct="yes">
91114
<statement>
92115
<p>6</p>
93116
</statement>
94117
<feedback>
95118
Yes, 6 is associated with the key dog.
96-
</feedback>
119+
</feedback>
97120
</choice>
98121
<choice>
99122
<statement>
100123
<p>23</p>
101124
</statement>
102125
<feedback>
103126
23 is associated with the key elephant.
104-
</feedback>
127+
</feedback>
105128
</choice>
106129
<choice>
107130
<statement>
108131
<p>Error, you cannot use the index operator with a dictionary.</p>
109132
</statement>
110133
<feedback>
111134
The [ ] operator, when used with a dictionary, will look up a value based on its key.
112-
</feedback>
135+
</feedback>
113136
</choice>
114137
</choices>
115138
</exercise>

0 commit comments

Comments
 (0)