Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3e43831

Browse files
committed
Merge branch 'master' of github.com:sagemath/more-sagemath-tutorials
2 parents 73f68eb + c870339 commit 3e43831

38 files changed

+416
-336
lines changed

‎2009年01月01日-SienaTutorials/Worksheet02-Lists.rst‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,18 @@ the output of the following commands will be.::
7070
sage: a = [1,2,3]
7171
sage: b = a
7272
sage: b[0] = 7
73-
sage: print a, b
74-
73+
sage: print(a, b)
74+
...
75+
7576
This result makes sense when you understand that ``a`` and ``b`` are both labels
7677
attached to the same list. Compare that result with the following.::
7778

7879
sage: a = 2
7980
sage: b = a
8081
sage: b = b + 1
81-
sage: print a, b
82-
82+
sage: print(a, b)
83+
...
84+
8385
In this case, we changed the object that ``b`` is attached to (to the object
8486
``2`` plus the object ``1``, which is the object ``3``), while ``a`` continues
8587
to be attached to the object ``2``. This concept will be useful to keep in
@@ -131,15 +133,17 @@ mind, as we discuss some methods which can be used to modify lists.
131133

132134
sage: L = [3, 1, 2]
133135
sage: M = L.sort()
134-
sage: print L, M
136+
sage: print(L, M)
137+
...
135138

136139
#. Now try the following.
137140

138141
::
139142
140143
sage: L = [3, 1, 2]
141144
sage: M = sorted(L)
142-
sage: print L, M
145+
sage: print(L, M)
146+
...
143147

144148
The range command
145149
-----------------
@@ -185,7 +189,7 @@ List Comprehensions
185189

186190
We already know how to create the list :math:`[1, 2, \ldots, 10]`::
187191

188-
sage: range(1,11)
192+
sage: list(range(1,11))
189193
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
190194

191195
Using a *list comprehension,* we can now create the list :math:`[1^2, 2^2,
@@ -195,7 +199,7 @@ Using a *list comprehension,* we can now create the list :math:`[1^2, 2^2,
195199

196200
sage: [i^2 for i in range(1,11)]
197201
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
198-
202+
199203
**Exercises:**
200204

201205
#. Create two lists:
@@ -263,8 +267,9 @@ A list can be *filtered* using a list comprehension. For example, to create a
263267
list of the squares of the prime numbers between 1 and 100, we use a list
264268
comprehension as follows::
265269

266-
sage: [p^2 for p in [1,2,..,100] if is_prime(p)]
267-
270+
sage: [p^2 for p in [1,2,..,100] if is_prime(p)]
271+
[4, 9, ...]
272+
268273
**Exercise:** Use a *list comprehension* to list all the natural numbers below
269274
20 that are multiples of 3 or 5. *Hints:*
270275

‎2009年01月01日-SienaTutorials/Worksheet03-FirstStepsTowardsProgramming.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ between 1 and 1000 that are multiples of 3 and 5::
6868
sage: for i in range(1,1001):
6969
....: if i % 3 == 0 and i % 5 == 0:
7070
....: nums.append(i)
71-
sage: printnums
71+
sage: print(nums)
7272
[15, 30, 45, 60, 75, 90, 105, 120, 135, 150, 165, 180, 195, 210, 225, 240, 255, 270, 285, 300, 315, 330, 345, 360, 375, 390, 405, 420, 435, 450, 465, 480, 495, 510, 525, 540, 555, 570, 585, 600, 615, 630, 645, 660, 675, 690, 705, 720, 735, 750, 765, 780, 795, 810, 825, 840, 855, 870, 885, 900, 915, 930, 945, 960, 975, 990]
7373

7474
**Exercises:**

‎2009年01月01日-SienaTutorials/Worksheet07-StringsAndTheBWT.rst‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ quotes (') or double quotes ("):
2323
::
2424

2525
sage: t = 'So is this!'
26-
sage: print t
26+
sage: print(t)
2727
So is this!
2828

2929
.. end of output
@@ -38,7 +38,7 @@ You can also input a string using three quotes (""" or '''). This is useful if y
3838
sage: that includes 'single quotes'
3939
....: and "double quotes".
4040
sage: """
41-
sage: print s
41+
sage: print(s)
4242
This is a multi-line
4343
string
4444
that includes 'single quotes'
@@ -221,7 +221,7 @@ then the last column is *bnnsaaa* , so the BWT of *bananas* is *bnnsaaa*.
221221
(*Hint:* String formatting can be done using the ``%`` operator. Here is
222222
an example::
223223

224-
sage: print'The sum of %s and %s is %s.' % (3,2,3+2)
224+
sage: print('The sum of %s and %s is %s.' % (3,2,3+2))
225225
The sum of 3 and 2 is 5.
226226

227227
If you are familiar with *C* then you will notice that string formating

‎2009年01月01日-SienaTutorials/Worksheet09-CombinatoricsIteratorsGenerators.rst‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,28 +50,28 @@ The Python command ``iter`` returns an iterator from an object (the object its
5050
5151
::
5252

53-
sage: it.next()
53+
sage: next(it)
5454
1
5555

5656
.. end of output
5757
5858
::
5959

60-
sage: it.next()
60+
sage: next(it)
6161
2
6262

6363
.. end of output
6464
6565
::
6666

67-
sage: it.next()
67+
sage: next(it)
6868
3
6969

7070
.. end of output
7171
7272
::
7373

74-
sage: it.next()
74+
sage: next(it)
7575
Traceback (most recent call last):
7676
...
7777
StopIteration
@@ -101,42 +101,42 @@ A *generator* is a function that is used to define an iterator. Instead of ``
101101
102102
::
103103

104-
sage: f.next()
104+
sage: next(f)
105105
1
106106

107107
.. end of output
108108
109109
::
110110

111-
sage: f.next()
111+
sage: next(f)
112112
1
113113

114114
.. end of output
115115
116116
::
117117

118-
sage: f.next()
118+
sage: next(f)
119119
2
120120

121121
.. end of output
122122
123123
::
124124

125-
sage: f.next()
125+
sage: next(f)
126126
3
127127

128128
.. end of output
129129
130130
::
131131

132-
sage: f.next()
132+
sage: next(f)
133133
5
134134

135135
.. end of output
136136
137137
::
138138

139-
sage: f.next()
139+
sage: next(f)
140140
8
141141

142142
.. end of output
@@ -246,7 +246,7 @@ There are many objects in Sage that model sets of combinatorial objects.
246246
::
247247

248248
sage: for p in Partitions(4):
249-
....: print p
249+
....: print(p)
250250
[4]
251251
[3, 1]
252252
[2, 2]
@@ -258,7 +258,7 @@ There are many objects in Sage that model sets of combinatorial objects.
258258
::
259259

260260
sage: for c in Compositions(4):
261-
....: print c
261+
....: print(c)
262262
[1, 1, 1, 1]
263263
[1, 1, 2]
264264
[1, 2, 1]
@@ -288,7 +288,7 @@ There are many objects in Sage that model sets of combinatorial objects.
288288
::
289289

290290
sage: for dw in DyckWords(4):
291-
....: print dw
291+
....: print(dw)
292292
()()()()
293293
()()(())
294294
()(())()
@@ -326,7 +326,7 @@ There are many objects in Sage that model sets of combinatorial objects.
326326

327327
sage: it = iter(W)
328328
sage: for a in range(16):
329-
....: print it.next()
329+
....: print(next(it))
330330
word:
331331
word: a
332332
word: b
@@ -367,7 +367,7 @@ There are many objects in Sage that model sets of combinatorial objects.
367367

368368
sage: it = iter(P)
369369
sage: for a in range(10):
370-
....: print it.next()
370+
....: print(next(it))
371371
Finite poset containing 0 elements
372372
Finite poset containing 1 elements
373373
Finite poset containing 2 elements

‎2009年01月01日-SienaTutorials/Worksheet10-IntroductionToCython.rst‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ Consider the following Python function that outputs a list of the first ``m``
3232
To time a function in Python, use the ``time`` command::
3333

3434
sage: time p = first_primes_python(5000)
35-
Time: CPU 6.20 s, Wall: 6.64 s
35+
Time: CPU 6.20 s, Wall: 6.64 s
3636

3737
::
3838

3939
sage: p[:100]
40-
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541]
40+
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541]
4141

4242

4343
First steps with Cython
@@ -66,12 +66,12 @@ The Sage notebook will take the contents of this cell, convert it to Cython, com
6666
Note the speed up we obtained by just adding ``%cython``::
6767

6868
sage: time p = first_primes_cython_v1(5000)
69-
Time: CPU 0.88 s, Wall: 0.91 s
69+
Time: CPU 0.88 s, Wall: 0.91 s
7070

7171
::
7272

7373
sage: time p = first_primes_cython_v1(10000)
74-
Time: CPU 3.23 s, Wall: 3.45 s
74+
Time: CPU 3.23 s, Wall: 3.45 s
7575

7676

7777
More Cython
@@ -123,22 +123,22 @@ a Python list at the end.
123123
::
124124

125125
sage: time p = first_primes_v3(10000)
126-
Time: CPU 0.22 s, Wall: 0.23 s
126+
Time: CPU 0.22 s, Wall: 0.23 s
127127

128128
We did not screw up anything, this function actually does produce primes::
129129

130130
sage: first_primes_v3(17)
131-
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59]
131+
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59]
132132

133133
And it agrees with the Sage version of the function::
134134

135135
sage: first_primes_v3(10000) == primes_first_n(10000)
136-
True
136+
True
137137

138138
But the Sage version is much, much better::
139139

140140
sage: time p = primes_first_n(10000)
141-
Time: CPU 0.00 s, Wall: 0.00 s
141+
Time: CPU 0.00 s, Wall: 0.00 s
142142

143143
::
144144

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /