Skip to content

Commit 8a736ab

Browse files
committed
add chapters
1 parent e832e8e commit 8a736ab

File tree

135 files changed

+48014
-1151
lines changed

Some content is hidden

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

135 files changed

+48014
-1151
lines changed

Code/3358OS_01_Code/code1/vectorsum.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ def numpysum(n):
2525
return c
2626

2727
def pythonsum(n):
28-
a = range(n)
29-
b = range(n)
28+
a = list(range(n))
29+
b = list(range(n))
3030
c = []
3131

3232
for i in range(len(a)):
@@ -42,11 +42,11 @@ def pythonsum(n):
4242
start = datetime.now()
4343
c = pythonsum(size)
4444
delta = datetime.now() - start
45-
print "The last 2 elements of the sum", c[-2:]
46-
print "PythonSum elapsed time in microseconds", delta.microseconds
45+
print("The last 2 elements of the sum", c[-2:])
46+
print("PythonSum elapsed time in microseconds", delta.microseconds)
4747

4848
start = datetime.now()
4949
c = numpysum(size)
5050
delta = datetime.now() - start
51-
print "The last 2 elements of the sum", c[-2:]
52-
print "NumPySum elapsed time in microseconds", delta.microseconds
51+
print("The last 2 elements of the sum", c[-2:])
52+
print("NumPySum elapsed time in microseconds", delta.microseconds)

Code/3358OS_02_Code/3358OS_02_Code/code2/arrayattributes.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,37 @@
77
#
88
# python arrayattributes.py
99
a = np.arange(5)
10-
print "In: a = arange(5)"
10+
print("In: a = arange(5)")
1111

12-
print "In: a.dtype"
13-
print a.dtype
12+
print("In: a.dtype")
13+
print(a.dtype)
1414
#Out: dtype('int64')
15-
print
15+
print()
1616

17-
print "In: a.shape"
18-
print a.shape
17+
print("In: a.shape")
18+
print(a.shape)
1919
#Out: (5,)
20-
print
20+
print()
2121

22-
print "In: a"
23-
print a
22+
print("In: a")
23+
print(a)
2424
#Out[4]: array([0, 1, 2, 3, 4])
25-
print
25+
print()
2626

2727
m = np.array([np.arange(2), np.arange(2)])
2828

29-
print "In: m"
30-
print m
29+
print("In: m")
30+
print(m)
3131
#Out:
3232
#array([[0, 1],
3333
# [0, 1]])
34-
print
34+
print()
3535

36-
print "In: m.shape"
37-
print m.shape
36+
print("In: m.shape")
37+
print(m.shape)
3838
#Out: (2, 2)
3939

40-
print "In: m.dtype"
41-
print m.dtype
40+
print("In: m.dtype")
41+
print(m.dtype)
4242
#Out: dtype('int64')
43-
print
43+
print()

Code/3358OS_02_Code/3358OS_02_Code/code2/arrayattributes2.py

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,37 @@
66
#
77
# python arrayattributes2.py
88
b = np.arange(24).reshape(2, 12)
9-
print "In: b"
10-
print b
9+
print("In: b")
10+
print(b)
1111
#Out:
1212
#array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
1313
# [12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]])
1414

15-
print "In: b.ndim"
16-
print b.ndim
15+
print("In: b.ndim")
16+
print(b.ndim)
1717
#Out: 2
1818

19-
print "In: b.size"
20-
print b.size
19+
print("In: b.size")
20+
print(b.size)
2121
#Out: 24
2222

23-
print "In: b.itemsize"
24-
print b.itemsize
23+
print("In: b.itemsize")
24+
print(b.itemsize)
2525
#Out: 8
2626

27-
print "In: b.nbytes"
28-
print b.nbytes
27+
print("In: b.nbytes")
28+
print(b.nbytes)
2929
#Out: 192
3030

31-
print "In: b.size * b.itemsize"
32-
print b.size * b.itemsize
31+
print("In: b.size * b.itemsize")
32+
print(b.size * b.itemsize)
3333
#Out: 192
3434

35-
print "In: b.resize(6,4)"
36-
print b.resize(6,4)
35+
print("In: b.resize(6,4)")
36+
print(b.resize(6,4))
3737

38-
print "In: b"
39-
print b
38+
print("In: b")
39+
print(b)
4040
#Out:
4141
#array([[ 0, 1, 2, 3],
4242
# [ 4, 5, 6, 7],
@@ -45,92 +45,92 @@
4545
# [16, 17, 18, 19],
4646
# [20, 21, 22, 23]])
4747

48-
print "In: b.T"
49-
print b.T
48+
print("In: b.T")
49+
print(b.T)
5050
#Out:
5151
#array([[ 0, 4, 8, 12, 16, 20],
5252
# [ 1, 5, 9, 13, 17, 21],
5353
# [ 2, 6, 10, 14, 18, 22],
5454
# [ 3, 7, 11, 15, 19, 23]])
5555

56-
print "In: b.ndim"
57-
print b.ndim
56+
print("In: b.ndim")
57+
print(b.ndim)
5858
#Out: 1
5959

60-
print "In: b.T"
61-
print b.T
60+
print("In: b.T")
61+
print(b.T)
6262
#Out: array([0, 1, 2, 3, 4])
6363

6464

65-
print "In: b = array([1.j + 1, 2.j + 3])"
65+
print("In: b = array([1.j + 1, 2.j + 3])")
6666
b = np.array([1.j + 1, 2.j + 3])
6767

68-
print "In: b"
69-
print b
68+
print("In: b")
69+
print(b)
7070
#Out: array([ 1.+1.j, 3.+2.j])
7171

72-
print "In: b.real"
73-
print b.real
72+
print("In: b.real")
73+
print(b.real)
7474
#Out: array([ 1., 3.])
7575

76-
print "In: b.imag"
77-
print b.imag
76+
print("In: b.imag")
77+
print(b.imag)
7878
#Out: array([ 1., 2.])
7979

80-
print "In: b.dtype"
81-
print b.dtype
80+
print("In: b.dtype")
81+
print(b.dtype)
8282
#Out: dtype('complex128')
8383

84-
print "In: b.dtype.str"
85-
print b.dtype.str
84+
print("In: b.dtype.str")
85+
print(b.dtype.str)
8686
#Out: '<c16'
8787

8888

89-
print "In: b = arange(4).reshape(2,2)"
89+
print("In: b = arange(4).reshape(2,2)")
9090
b = np.arange(4).reshape(2,2)
9191

92-
print "In: b"
93-
print b
92+
print("In: b")
93+
print(b)
9494
#Out:
9595
#array([[0, 1],
9696
# [2, 3]])
9797

98-
print "In: f = b.flat"
98+
print("In: f = b.flat")
9999
f = b.flat
100100

101-
print "In: f"
102-
print f
101+
print("In: f")
102+
print(f)
103103
#Out: <numpy.flatiter object at 0x103013e00>
104104

105-
print "In: for it in f: print it"
105+
print("In: for it in f: print it")
106106
for it in f:
107-
print it
107+
print(it)
108108
#0
109109
#1
110110
#2
111111
#3
112112

113-
print "In: b.flat[2]"
114-
print b.flat[2]
113+
print("In: b.flat[2]")
114+
print(b.flat[2])
115115
#Out: 2
116116

117117

118-
print "In: b.flat[[1,3]]"
119-
print b.flat[[1,3]]
118+
print("In: b.flat[[1,3]]")
119+
print(b.flat[[1,3]])
120120
#Out: array([1, 3])
121121

122122

123-
print "In: b"
124-
print b
123+
print("In: b")
124+
print(b)
125125
#Out:
126126
#array([[7, 7],
127127
# [7, 7]])
128128

129-
print "In: b.flat[[1,3]] = 1"
129+
print("In: b.flat[[1,3]] = 1")
130130
b.flat[[1,3]] = 1
131131

132-
print "In: b"
133-
print b
132+
print("In: b")
133+
print(b)
134134
#Out:
135135
#array([[7, 1],
136136
# [7, 1]])

Code/3358OS_02_Code/3358OS_02_Code/code2/arrayconversion.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,37 @@
66
#
77
# python arrayconversion.py
88
b = np.array([ 1.+1.j, 3.+2.j])
9-
print "In: b"
10-
print b
9+
print("In: b")
10+
print(b)
1111
#Out: array([ 1.+1.j, 3.+2.j])
1212

13-
print "In: b.tolist()"
14-
print b.tolist()
13+
print("In: b.tolist()")
14+
print(b.tolist())
1515
#Out: [(1+1j), (3+2j)]
1616

17-
print "In: b.tostring()"
18-
print b.tostring()
17+
print("In: b.tostring()")
18+
print(b.tostring())
1919
#Out: '\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\x08@\x00\x00\x00\x00\x00\x00\x00@'
2020

21-
print "In: fromstring('\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\x08@\x00\x00\x00\x00\x00\x00\x00@', dtype=complex)"
22-
print np.fromstring('\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\x08@\x00\x00\x00\x00\x00\x00\x00@', dtype=complex)
21+
print("In: fromstring('\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\x08@\x00\x00\x00\x00\x00\x00\x00@', dtype=complex)")
22+
print(np.fromstring('\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\x08@\x00\x00\x00\x00\x00\x00\x00@', dtype=complex))
2323
#Out: array([ 1.+1.j, 3.+2.j]
2424

25-
print "In: fromstring('20:42:52',sep=':', dtype=int)"
26-
print np.fromstring('20:42:52',sep=':', dtype=int)
25+
print("In: fromstring('20:42:52',sep=':', dtype=int)")
26+
print(np.fromstring('20:42:52',sep=':', dtype=int))
2727
#Out: array([20, 42, 52])
2828

29-
print "In: b"
30-
print b
29+
print("In: b")
30+
print(b)
3131
#Out: array([ 1.+1.j, 3.+2.j])
3232

33-
print "In: b.astype(int)"
34-
print b.astype(int)
33+
print("In: b.astype(int)")
34+
print(b.astype(int))
3535
#/usr/local/bin/ipython:1: ComplexWarning: Casting complex values to real discards the imaginary part
3636
# #!/usr/bin/python
3737
#Out: array([1, 3])
3838

39-
print "In: b.astype('complex')"
40-
print b.astype('complex')
39+
print("In: b.astype('complex')")
40+
print(b.astype('complex'))
4141
#Out: array([ 1.+1.j, 3.+2.j])
4242

Code/3358OS_02_Code/3358OS_02_Code/code2/broadcasting.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
import scipy.io.wavfile
22
import matplotlib.pyplot as plt
3-
import urllib2
3+
import urllib.request, urllib.error, urllib.parse
44
import numpy as np
55

6-
response = urllib2.urlopen('http://www.thesoundarchive.com/austinpowers/smashingbaby.wav')
7-
print response.info()
6+
response = urllib.request.urlopen('http://www.thesoundarchive.com/austinpowers/smashingbaby.wav')
7+
print(response.info())
88
WAV_FILE = 'smashingbaby.wav'
99
filehandle = open(WAV_FILE, 'w')
1010
filehandle.write(response.read())
1111
filehandle.close()
1212
sample_rate, data = scipy.io.wavfile.read(WAV_FILE)
13-
print "Data type", data.dtype, "Shape", data.shape
13+
print("Data type", data.dtype, "Shape", data.shape)
1414

1515
plt.subplot(2, 1, 1)
1616
plt.title("Original")
1717
plt.plot(data)
1818

1919
newdata = data * 0.2
2020
newdata = newdata.astype(np.uint8)
21-
print "Data type", newdata.dtype, "Shape", newdata.shape
21+
print("Data type", newdata.dtype, "Shape", newdata.shape)
2222

2323
scipy.io.wavfile.write("quiet.wav",
2424
sample_rate, newdata)

Code/3358OS_02_Code/3358OS_02_Code/code2/charcodes.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
#
77
# python charcodes.py
88

9-
print "In: arange(7, dtype='f')"
10-
print np.arange(7, dtype='f')
9+
print("In: arange(7, dtype='f')")
10+
print(np.arange(7, dtype='f'))
1111
#Out: array([ 0., 1., 2., 3., 4., 5., 6.], dtype=float32)
1212

13-
print "In: arange(7, dtype='D')"
14-
print np.arange(7, dtype='D')
13+
print("In: arange(7, dtype='D')")
14+
print(np.arange(7, dtype='D'))
1515
#Out: array([ 0.+0.j, 1.+0.j, 2.+0.j, 3.+0.j, 4.+0.j, 5.+0.j, 6.+0.j])
1616

1717

Code/3358OS_02_Code/3358OS_02_Code/code2/copy_view.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import matplotlib.pyplot as plt
33

44
lena = scipy.misc.lena()
5-
print lena.shape
5+
print(lena.shape)
66
acopy = lena.copy()
77
aview = lena.view()
88

Code/3358OS_02_Code/3358OS_02_Code/code2/dtypeattributes.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
# python dtypeattributes.py
99

1010

11-
print "In: a = array([[1,2],[3,4]])"
11+
print("In: a = array([[1,2],[3,4]])")
1212
a = np.array([[1,2],[3,4]])
1313

14-
print "In: a.dtype.byteorder"
15-
print a.dtype.byteorder
14+
print("In: a.dtype.byteorder")
15+
print(a.dtype.byteorder)
1616
#Out: '='
1717

18-
print "In: a.dtype.itemsize"
19-
print a.dtype.itemsize
18+
print("In: a.dtype.itemsize")
19+
print(a.dtype.itemsize)
2020
#Out: 8
2121

2222

0 commit comments

Comments
 (0)