2020-10-19 21:08:55

This commit is contained in:
wizardforcel
2020-10-19 21:08:55 +08:00
parent 7f63048035
commit ab0caba1f0
140 changed files with 3982 additions and 3982 deletions

View File

@@ -6,14 +6,14 @@
In [1]:
```
```py
2 + 2
```
Out[1]:
```
```py
4
```
@@ -21,14 +21,14 @@ Out[1]:
In [2]:
```
```py
2.0 + 2.5
```
Out[2]:
```
```py
4.5
```
@@ -36,14 +36,14 @@ Out[2]:
In [3]:
```
```py
2 + 2.5
```
Out[3]:
```
```py
4.5
```
@@ -53,7 +53,7 @@ Out[3]:
In [4]:
```
```py
a = 0.2
```
@@ -64,7 +64,7 @@ a = 0.2
In [5]:
```
```py
s = "hello world"
s
@@ -72,13 +72,13 @@ s
Out[5]:
```
```py
'hello world'
```
In [6]:
```
```py
s = 'hello world'
s
@@ -86,7 +86,7 @@ s
Out[6]:
```
```py
'hello world'
```
@@ -94,14 +94,14 @@ Out[6]:
In [7]:
```
```py
s = """hello
world"""
print s
```
```
```py
hello
world
@@ -109,14 +109,14 @@ world
In [8]:
```
```py
s = '''hello
world'''
print s
```
```
```py
hello
world
@@ -126,7 +126,7 @@ world
In [9]:
```
```py
s = "hello" + " world"
s
@@ -134,7 +134,7 @@ s
Out[9]:
```
```py
'hello world'
```
@@ -142,40 +142,40 @@ Out[9]:
In [10]:
```
```py
s[0]
```
Out[10]:
```
```py
'h'
```
In [11]:
```
```py
s[-1]
```
Out[11]:
```
```py
'd'
```
In [12]:
```
```py
s[0:5]
```
Out[12]:
```
```py
'hello'
```
@@ -183,7 +183,7 @@ Out[12]:
In [13]:
```
```py
s = "hello world"
s.split()
@@ -191,7 +191,7 @@ s.split()
Out[13]:
```
```py
['hello', 'world']
```
@@ -199,14 +199,14 @@ Out[13]:
In [14]:
```
```py
len(s)
```
Out[14]:
```
```py
11
```
@@ -216,7 +216,7 @@ Python用`[]`来生成列表
In [15]:
```
```py
a = [1, 2.0, 'hello', 5 + 1.0]
a
@@ -224,7 +224,7 @@ a
Out[15]:
```
```py
[1, 2.0, 'hello', 6.0]
```
@@ -232,14 +232,14 @@ Out[15]:
In [16]:
```
```py
a + a
```
Out[16]:
```
```py
[1, 2.0, 'hello', 6.0, 1, 2.0, 'hello', 6.0]
```
@@ -247,14 +247,14 @@ Out[16]:
In [17]:
```
```py
a[1]
```
Out[17]:
```
```py
2.0
```
@@ -262,14 +262,14 @@ Out[17]:
In [18]:
```
```py
len(a)
```
Out[18]:
```
```py
4
```
@@ -277,7 +277,7 @@ Out[18]:
In [19]:
```
```py
a.append("world")
a
@@ -285,7 +285,7 @@ a
Out[19]:
```
```py
[1, 2.0, 'hello', 6.0, 'world']
```
@@ -295,7 +295,7 @@ Python用{}来生成集合,集合中不含有相同元素。
In [20]:
```
```py
s = {2, 3, 4, 2}
s
@@ -303,7 +303,7 @@ s
Out[20]:
```
```py
{2, 3, 4}
```
@@ -311,14 +311,14 @@ Out[20]:
In [21]:
```
```py
len(s)
```
Out[21]:
```
```py
3
```
@@ -326,7 +326,7 @@ Out[21]:
In [22]:
```
```py
s.add(1)
s
@@ -334,7 +334,7 @@ s
Out[22]:
```
```py
{1, 2, 3, 4}
```
@@ -342,7 +342,7 @@ Out[22]:
In [23]:
```
```py
a = {1, 2, 3, 4}
b = {2, 3, 4, 5}
a & b
@@ -351,7 +351,7 @@ a & b
Out[23]:
```
```py
{2, 3, 4}
```
@@ -359,14 +359,14 @@ Out[23]:
In [24]:
```
```py
a | b
```
Out[24]:
```
```py
{1, 2, 3, 4, 5}
```
@@ -374,14 +374,14 @@ Out[24]:
In [25]:
```
```py
a - b
```
Out[25]:
```
```py
{1}
```
@@ -389,14 +389,14 @@ Out[25]:
In [26]:
```
```py
a ^ b
```
Out[26]:
```
```py
{1, 5}
```
@@ -406,7 +406,7 @@ Python用`{key:value}`来生成Dictionary。
In [27]:
```
```py
d = {'dogs':5, 'cats':4}
d
@@ -414,7 +414,7 @@ d
Out[27]:
```
```py
{'cats': 4, 'dogs': 5}
```
@@ -422,14 +422,14 @@ Out[27]:
In [28]:
```
```py
len(d)
```
Out[28]:
```
```py
2
```
@@ -437,14 +437,14 @@ Out[28]:
In [29]:
```
```py
d["dogs"]
```
Out[29]:
```
```py
5
```
@@ -452,7 +452,7 @@ Out[29]:
In [30]:
```
```py
d["dogs"] = 2
d
@@ -460,7 +460,7 @@ d
Out[30]:
```
```py
{'cats': 4, 'dogs': 2}
```
@@ -468,7 +468,7 @@ Out[30]:
In [31]:
```
```py
d["pigs"] = 7
d
@@ -476,7 +476,7 @@ d
Out[31]:
```
```py
{'cats': 4, 'dogs': 2, 'pigs': 7}
```
@@ -484,14 +484,14 @@ Out[31]:
In [32]:
```
```py
d.keys()
```
Out[32]:
```
```py
['cats', 'dogs', 'pigs']
```
@@ -499,14 +499,14 @@ Out[32]:
In [33]:
```
```py
d.values()
```
Out[33]:
```
```py
[4, 2, 7]
```
@@ -514,14 +514,14 @@ Out[33]:
In [34]:
```
```py
d.items()
```
Out[34]:
```
```py
[('cats', 4), ('dogs', 2), ('pigs', 7)]
```
@@ -531,7 +531,7 @@ Out[34]:
In [35]:
```
```py
from numpy import array
a = array([1, 2, 3, 4])
a
@@ -540,7 +540,7 @@ a
Out[35]:
```
```py
array([1, 2, 3, 4])
```
@@ -548,27 +548,27 @@ array([1, 2, 3, 4])
In [36]:
```
```py
a + 2
```
Out[36]:
```
```py
array([3, 4, 5, 6])
```
In [37]:
```
```py
a + a
```
Out[37]:
```
```py
array([2, 4, 6, 8])
```
@@ -578,7 +578,7 @@ Python提供了一个很像MATLAB的绘图接口。
In [38]:
```
```py
%matplotlib inline
from matplotlib.pyplot import plot
plot(a, a**2)
@@ -587,7 +587,7 @@ plot(a, a**2)
Out[38]:
```
```py
[<matplotlib.lines.Line2D at 0x9fb6fd0>]
```
@@ -685,7 +685,7 @@ SaoBg7kk1YDBXJJqwGAuSTVgMJekGvhf3kAwE/Ra4D0AAAAASUVORK5CYII=
In [39]:
```
```py
line = '1 2 3 4 5'
fields = line.split()
fields
@@ -694,13 +694,13 @@ fields
Out[39]:
```
```py
['1', '2', '3', '4', '5']
```
In [40]:
```
```py
total = 0
for field in fields:
total += int(field)
@@ -710,7 +710,7 @@ total
Out[40]:
```
```py
15
```
@@ -718,7 +718,7 @@ Python中有一种叫做列表推导式(List comprehension)的用法:
In [41]:
```
```py
numbers = [int(field) for field in fields]
numbers
@@ -726,20 +726,20 @@ numbers
Out[41]:
```
```py
[1, 2, 3, 4, 5]
```
In [42]:
```
```py
sum(numbers)
```
Out[42]:
```
```py
15
```
@@ -747,14 +747,14 @@ Out[42]:
In [43]:
```
```py
sum([int(field) for field in line.split()])
```
Out[43]:
```
```py
15
```
@@ -762,12 +762,12 @@ Out[43]:
In [44]:
```
```py
cd ~
```
```
```py
d:\Users\lijin
```
@@ -776,7 +776,7 @@ d:\Users\lijin
In [45]:
```
```py
f = open('data.txt', 'w')
f.write('1 2 3 4\n')
f.write('2 3 4 5\n')
@@ -788,7 +788,7 @@ f.close()
In [46]:
```
```py
f = open('data.txt')
data = []
for line in f:
@@ -800,19 +800,19 @@ data
Out[46]:
```
```py
[[1, 2, 3, 4], [2, 3, 4, 5]]
```
In [47]:
```
```py
for row in data:
print row
```
```
```py
[1, 2, 3, 4]
[2, 3, 4, 5]
@@ -822,7 +822,7 @@ for row in data:
In [48]:
```
```py
import os
os.remove('data.txt')
@@ -834,7 +834,7 @@ Python用关键词`def`来定义函数。
In [49]:
```
```py
def poly(x, a, b, c):
y = a * x ** 2 + b * x + c
return y
@@ -846,7 +846,7 @@ poly(x, 1, 2, 3)
Out[49]:
```
```py
6
```
@@ -854,7 +854,7 @@ Out[49]:
In [50]:
```
```py
x = array([1, 2, 3])
poly(x, 1, 2, 3)
@@ -862,7 +862,7 @@ poly(x, 1, 2, 3)
Out[50]:
```
```py
array([ 6, 11, 18])
```
@@ -870,7 +870,7 @@ array([ 6, 11, 18])
In [51]:
```
```py
from numpy import arange
def poly(x, a = 1, b = 2, c = 3):
@@ -885,33 +885,33 @@ array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
Out[51]:
```
```py
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
```
In [52]:
```
```py
poly(x)
```
Out[52]:
```
```py
array([ 3, 6, 11, 18, 27, 38, 51, 66, 83, 102])
```
In [53]:
```
```py
poly(x, b = 1)
```
Out[53]:
```
```py
array([ 3, 5, 9, 15, 23, 33, 45, 59, 75, 93])
```
@@ -921,7 +921,7 @@ Python中使用`import`关键词来导入模块。
In [54]:
```
```py
import os
```
@@ -930,14 +930,14 @@ import os
In [55]:
```
```py
os.getpid()
```
Out[55]:
```
```py
4400
```
@@ -945,14 +945,14 @@ Out[55]:
In [56]:
```
```py
os.sep
```
Out[56]:
```
```py
'\\'
```
@@ -962,7 +962,7 @@ Out[56]:
In [57]:
```
```py
class Person(object):
def __init__(self, first, last, age):
self.first = first
@@ -977,7 +977,7 @@ class Person(object):
In [58]:
```
```py
person = Person('Mertle', 'Sedgewick', 52)
```
@@ -986,14 +986,14 @@ person = Person('Mertle', 'Sedgewick', 52)
In [59]:
```
```py
person.first
```
Out[59]:
```
```py
'Mertle'
```
@@ -1001,14 +1001,14 @@ Out[59]:
In [60]:
```
```py
person.full_name()
```
Out[60]:
```
```py
'Mertle Sedgewick'
```
@@ -1016,7 +1016,7 @@ Out[60]:
In [61]:
```
```py
person.last = 'Smith'
```
@@ -1025,7 +1025,7 @@ person.last = 'Smith'
In [62]:
```
```py
person.critters = d
person.critters
@@ -1033,7 +1033,7 @@ person.critters
Out[62]:
```
```py
{'cats': 4, 'dogs': 2, 'pigs': 7}
```
@@ -1041,7 +1041,7 @@ Out[62]:
In [63]:
```
```py
url = 'http://ichart.finance.yahoo.com/table.csv?s=GE&d=10&e=5&f=2013&g=d&a=0&b=2&c=1962&ignore=.csv'
```
@@ -1050,7 +1050,7 @@ url = 'http://ichart.finance.yahoo.com/table.csv?s=GE&d=10&e=5&f=2013&g=d&a=0&b=
In [64]:
```
```py
import urllib2
ge_csv = urllib2.urlopen(url)
data = []
@@ -1062,7 +1062,7 @@ data[:4]
Out[64]:
```
```py
[['Date', 'Open', 'High', 'Low', 'Close', 'Volume', 'Adj Close\n'],
['2013-11-05', '26.32', '26.52', '26.26', '26.42', '24897500', '24.872115\n'],
['2013-11-04',
@@ -1085,7 +1085,7 @@ Out[64]:
In [65]:
```
```py
ge_csv = urllib2.urlopen(url)
import pandas
ge = pandas.read_csv(ge_csv, index_col=0, parse_dates=True)
@@ -1095,7 +1095,7 @@ ge.plot(y='Adj Close')
Out[65]:
```
```py
<matplotlib.axes._subplots.AxesSubplot at 0xc2e3198>
```