mirror of
https://github.com/apachecn/ailearning.git
synced 2026-02-10 05:45:40 +08:00
1014 lines
31 KiB
Markdown
1014 lines
31 KiB
Markdown
# Python 入门演示
|
||
|
||
## 简单的数学运算
|
||
|
||
整数相加,得到整数:
|
||
|
||
In [1]:
|
||
|
||
```py
|
||
2 + 2
|
||
|
||
```
|
||
|
||
Out[1]:
|
||
|
||
```py
|
||
4
|
||
```
|
||
|
||
浮点数相加,得到浮点数:
|
||
|
||
In [2]:
|
||
|
||
```py
|
||
2.0 + 2.5
|
||
|
||
```
|
||
|
||
Out[2]:
|
||
|
||
```py
|
||
4.5
|
||
```
|
||
|
||
整数和浮点数相加,得到浮点数:
|
||
|
||
In [3]:
|
||
|
||
```py
|
||
2 + 2.5
|
||
|
||
```
|
||
|
||
Out[3]:
|
||
|
||
```py
|
||
4.5
|
||
```
|
||
|
||
## 变量赋值
|
||
|
||
**Python**使用`<变量名>=<表达式>`的方式对变量进行赋值
|
||
|
||
In [4]:
|
||
|
||
```py
|
||
a = 0.2
|
||
|
||
```
|
||
|
||
## 字符串 String
|
||
|
||
字符串的生成,单引号与双引号是等价的:
|
||
|
||
In [5]:
|
||
|
||
```py
|
||
s = "hello world"
|
||
s
|
||
|
||
```
|
||
|
||
Out[5]:
|
||
|
||
```py
|
||
'hello world'
|
||
```
|
||
|
||
In [6]:
|
||
|
||
```py
|
||
s = 'hello world'
|
||
s
|
||
|
||
```
|
||
|
||
Out[6]:
|
||
|
||
```py
|
||
'hello world'
|
||
```
|
||
|
||
三引号用来输入包含多行文字的字符串:
|
||
|
||
In [7]:
|
||
|
||
```py
|
||
s = """hello
|
||
world"""
|
||
print s
|
||
|
||
```
|
||
|
||
```py
|
||
hello
|
||
world
|
||
|
||
```
|
||
|
||
In [8]:
|
||
|
||
```py
|
||
s = '''hello
|
||
world'''
|
||
print s
|
||
|
||
```
|
||
|
||
```py
|
||
hello
|
||
world
|
||
|
||
```
|
||
|
||
字符串的加法:
|
||
|
||
In [9]:
|
||
|
||
```py
|
||
s = "hello" + " world"
|
||
s
|
||
|
||
```
|
||
|
||
Out[9]:
|
||
|
||
```py
|
||
'hello world'
|
||
```
|
||
|
||
字符串索引:
|
||
|
||
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'
|
||
```
|
||
|
||
字符串的分割:
|
||
|
||
In [13]:
|
||
|
||
```py
|
||
s = "hello world"
|
||
s.split()
|
||
|
||
```
|
||
|
||
Out[13]:
|
||
|
||
```py
|
||
['hello', 'world']
|
||
```
|
||
|
||
查看字符串的长度:
|
||
|
||
In [14]:
|
||
|
||
```py
|
||
len(s)
|
||
|
||
```
|
||
|
||
Out[14]:
|
||
|
||
```py
|
||
11
|
||
```
|
||
|
||
## 列表 List
|
||
|
||
Python用`[]`来生成列表
|
||
|
||
In [15]:
|
||
|
||
```py
|
||
a = [1, 2.0, 'hello', 5 + 1.0]
|
||
a
|
||
|
||
```
|
||
|
||
Out[15]:
|
||
|
||
```py
|
||
[1, 2.0, 'hello', 6.0]
|
||
```
|
||
|
||
列表加法:
|
||
|
||
In [16]:
|
||
|
||
```py
|
||
a + a
|
||
|
||
```
|
||
|
||
Out[16]:
|
||
|
||
```py
|
||
[1, 2.0, 'hello', 6.0, 1, 2.0, 'hello', 6.0]
|
||
```
|
||
|
||
列表索引:
|
||
|
||
In [17]:
|
||
|
||
```py
|
||
a[1]
|
||
|
||
```
|
||
|
||
Out[17]:
|
||
|
||
```py
|
||
2.0
|
||
```
|
||
|
||
列表长度:
|
||
|
||
In [18]:
|
||
|
||
```py
|
||
len(a)
|
||
|
||
```
|
||
|
||
Out[18]:
|
||
|
||
```py
|
||
4
|
||
```
|
||
|
||
向列表中添加元素:
|
||
|
||
In [19]:
|
||
|
||
```py
|
||
a.append("world")
|
||
a
|
||
|
||
```
|
||
|
||
Out[19]:
|
||
|
||
```py
|
||
[1, 2.0, 'hello', 6.0, 'world']
|
||
```
|
||
|
||
## 集合 Set
|
||
|
||
Python用{}来生成集合,集合中不含有相同元素。
|
||
|
||
In [20]:
|
||
|
||
```py
|
||
s = {2, 3, 4, 2}
|
||
s
|
||
|
||
```
|
||
|
||
Out[20]:
|
||
|
||
```py
|
||
{2, 3, 4}
|
||
```
|
||
|
||
集合的长度:
|
||
|
||
In [21]:
|
||
|
||
```py
|
||
len(s)
|
||
|
||
```
|
||
|
||
Out[21]:
|
||
|
||
```py
|
||
3
|
||
```
|
||
|
||
向集合中添加元素:
|
||
|
||
In [22]:
|
||
|
||
```py
|
||
s.add(1)
|
||
s
|
||
|
||
```
|
||
|
||
Out[22]:
|
||
|
||
```py
|
||
{1, 2, 3, 4}
|
||
```
|
||
|
||
集合的交:
|
||
|
||
In [23]:
|
||
|
||
```py
|
||
a = {1, 2, 3, 4}
|
||
b = {2, 3, 4, 5}
|
||
a & b
|
||
|
||
```
|
||
|
||
Out[23]:
|
||
|
||
```py
|
||
{2, 3, 4}
|
||
```
|
||
|
||
并:
|
||
|
||
In [24]:
|
||
|
||
```py
|
||
a | b
|
||
|
||
```
|
||
|
||
Out[24]:
|
||
|
||
```py
|
||
{1, 2, 3, 4, 5}
|
||
```
|
||
|
||
差:
|
||
|
||
In [25]:
|
||
|
||
```py
|
||
a - b
|
||
|
||
```
|
||
|
||
Out[25]:
|
||
|
||
```py
|
||
{1}
|
||
```
|
||
|
||
对称差:
|
||
|
||
In [26]:
|
||
|
||
```py
|
||
a ^ b
|
||
|
||
```
|
||
|
||
Out[26]:
|
||
|
||
```py
|
||
{1, 5}
|
||
```
|
||
|
||
## 字典 Dictionary
|
||
|
||
Python用`{key:value}`来生成Dictionary。
|
||
|
||
In [27]:
|
||
|
||
```py
|
||
d = {'dogs':5, 'cats':4}
|
||
d
|
||
|
||
```
|
||
|
||
Out[27]:
|
||
|
||
```py
|
||
{'cats': 4, 'dogs': 5}
|
||
```
|
||
|
||
字典的大小
|
||
|
||
In [28]:
|
||
|
||
```py
|
||
len(d)
|
||
|
||
```
|
||
|
||
Out[28]:
|
||
|
||
```py
|
||
2
|
||
```
|
||
|
||
查看字典某个键对应的值:
|
||
|
||
In [29]:
|
||
|
||
```py
|
||
d["dogs"]
|
||
|
||
```
|
||
|
||
Out[29]:
|
||
|
||
```py
|
||
5
|
||
```
|
||
|
||
修改键值:
|
||
|
||
In [30]:
|
||
|
||
```py
|
||
d["dogs"] = 2
|
||
d
|
||
|
||
```
|
||
|
||
Out[30]:
|
||
|
||
```py
|
||
{'cats': 4, 'dogs': 2}
|
||
```
|
||
|
||
插入键值:
|
||
|
||
In [31]:
|
||
|
||
```py
|
||
d["pigs"] = 7
|
||
d
|
||
|
||
```
|
||
|
||
Out[31]:
|
||
|
||
```py
|
||
{'cats': 4, 'dogs': 2, 'pigs': 7}
|
||
```
|
||
|
||
所有的键:
|
||
|
||
In [32]:
|
||
|
||
```py
|
||
d.keys()
|
||
|
||
```
|
||
|
||
Out[32]:
|
||
|
||
```py
|
||
['cats', 'dogs', 'pigs']
|
||
```
|
||
|
||
所有的值:
|
||
|
||
In [33]:
|
||
|
||
```py
|
||
d.values()
|
||
|
||
```
|
||
|
||
Out[33]:
|
||
|
||
```py
|
||
[4, 2, 7]
|
||
```
|
||
|
||
所有的键值对:
|
||
|
||
In [34]:
|
||
|
||
```py
|
||
d.items()
|
||
|
||
```
|
||
|
||
Out[34]:
|
||
|
||
```py
|
||
[('cats', 4), ('dogs', 2), ('pigs', 7)]
|
||
```
|
||
|
||
## 数组 Numpy Arrays
|
||
|
||
需要先导入需要的包,Numpy数组可以进行很多列表不能进行的运算。
|
||
|
||
In [35]:
|
||
|
||
```py
|
||
from numpy import array
|
||
a = array([1, 2, 3, 4])
|
||
a
|
||
|
||
```
|
||
|
||
Out[35]:
|
||
|
||
```py
|
||
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])
|
||
```
|
||
|
||
## 画图 Plot
|
||
|
||
Python提供了一个很像MATLAB的绘图接口。
|
||
|
||
In [38]:
|
||
|
||
```py
|
||
%matplotlib inline
|
||
from matplotlib.pyplot import plot
|
||
plot(a, a**2)
|
||
|
||
```
|
||
|
||
Out[38]:
|
||
|
||
```py
|
||
[<matplotlib.lines.Line2D at 0x9fb6fd0>]
|
||
```
|
||
|
||

|
||
|
||
## 循环 Loop
|
||
|
||
In [39]:
|
||
|
||
```py
|
||
line = '1 2 3 4 5'
|
||
fields = line.split()
|
||
fields
|
||
|
||
```
|
||
|
||
Out[39]:
|
||
|
||
```py
|
||
['1', '2', '3', '4', '5']
|
||
```
|
||
|
||
In [40]:
|
||
|
||
```py
|
||
total = 0
|
||
for field in fields:
|
||
total += int(field)
|
||
total
|
||
|
||
```
|
||
|
||
Out[40]:
|
||
|
||
```py
|
||
15
|
||
```
|
||
|
||
Python中有一种叫做列表推导式(List comprehension)的用法:
|
||
|
||
In [41]:
|
||
|
||
```py
|
||
numbers = [int(field) for field in fields]
|
||
numbers
|
||
|
||
```
|
||
|
||
Out[41]:
|
||
|
||
```py
|
||
[1, 2, 3, 4, 5]
|
||
```
|
||
|
||
In [42]:
|
||
|
||
```py
|
||
sum(numbers)
|
||
|
||
```
|
||
|
||
Out[42]:
|
||
|
||
```py
|
||
15
|
||
```
|
||
|
||
写在一行:
|
||
|
||
In [43]:
|
||
|
||
```py
|
||
sum([int(field) for field in line.split()])
|
||
|
||
```
|
||
|
||
Out[43]:
|
||
|
||
```py
|
||
15
|
||
```
|
||
|
||
## 文件操作 File IO
|
||
|
||
In [44]:
|
||
|
||
```py
|
||
cd ~
|
||
|
||
```
|
||
|
||
```py
|
||
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')
|
||
f.close()
|
||
|
||
```
|
||
|
||
读文件:
|
||
|
||
In [46]:
|
||
|
||
```py
|
||
f = open('data.txt')
|
||
data = []
|
||
for line in f:
|
||
data.append([int(field) for field in line.split()])
|
||
f.close()
|
||
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]
|
||
|
||
```
|
||
|
||
删除文件:
|
||
|
||
In [48]:
|
||
|
||
```py
|
||
import os
|
||
os.remove('data.txt')
|
||
|
||
```
|
||
|
||
## 函数 Function
|
||
|
||
Python用关键词`def`来定义函数。
|
||
|
||
In [49]:
|
||
|
||
```py
|
||
def poly(x, a, b, c):
|
||
y = a * x ** 2 + b * x + c
|
||
return y
|
||
|
||
x = 1
|
||
poly(x, 1, 2, 3)
|
||
|
||
```
|
||
|
||
Out[49]:
|
||
|
||
```py
|
||
6
|
||
```
|
||
|
||
用Numpy数组做参数x:
|
||
|
||
In [50]:
|
||
|
||
```py
|
||
x = array([1, 2, 3])
|
||
poly(x, 1, 2, 3)
|
||
|
||
```
|
||
|
||
Out[50]:
|
||
|
||
```py
|
||
array([ 6, 11, 18])
|
||
```
|
||
|
||
可以在定义时指定参数的默认值:
|
||
|
||
In [51]:
|
||
|
||
```py
|
||
from numpy import arange
|
||
|
||
def poly(x, a = 1, b = 2, c = 3):
|
||
y = a*x**2 + b*x + c
|
||
return y
|
||
|
||
x = arange(10)
|
||
x
|
||
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])
|
||
```
|
||
|
||
## 模块 Module
|
||
|
||
Python中使用`import`关键词来导入模块。
|
||
|
||
In [54]:
|
||
|
||
```py
|
||
import os
|
||
|
||
```
|
||
|
||
当前进程号:
|
||
|
||
In [55]:
|
||
|
||
```py
|
||
os.getpid()
|
||
|
||
```
|
||
|
||
Out[55]:
|
||
|
||
```py
|
||
4400
|
||
```
|
||
|
||
系统分隔符:
|
||
|
||
In [56]:
|
||
|
||
```py
|
||
os.sep
|
||
|
||
```
|
||
|
||
Out[56]:
|
||
|
||
```py
|
||
'\\'
|
||
```
|
||
|
||
## - 类 Class
|
||
|
||
用`class`来定义一个类。 `Person(object)`表示继承自`object`类; `__init__`函数用来初始化对象; `self`表示对象自身,类似于`C` `Java`里面`this`。
|
||
|
||
In [57]:
|
||
|
||
```py
|
||
class Person(object):
|
||
def __init__(self, first, last, age):
|
||
self.first = first
|
||
self.last = last
|
||
self.age = age
|
||
def full_name(self):
|
||
return self.first + ' ' + self.last
|
||
|
||
```
|
||
|
||
构建新对象:
|
||
|
||
In [58]:
|
||
|
||
```py
|
||
person = Person('Mertle', 'Sedgewick', 52)
|
||
|
||
```
|
||
|
||
调用对象的属性:
|
||
|
||
In [59]:
|
||
|
||
```py
|
||
person.first
|
||
|
||
```
|
||
|
||
Out[59]:
|
||
|
||
```py
|
||
'Mertle'
|
||
```
|
||
|
||
调用对象的方法:
|
||
|
||
In [60]:
|
||
|
||
```py
|
||
person.full_name()
|
||
|
||
```
|
||
|
||
Out[60]:
|
||
|
||
```py
|
||
'Mertle Sedgewick'
|
||
```
|
||
|
||
修改对象的属性:
|
||
|
||
In [61]:
|
||
|
||
```py
|
||
person.last = 'Smith'
|
||
|
||
```
|
||
|
||
添加新属性,d是之前定义的字典:
|
||
|
||
In [62]:
|
||
|
||
```py
|
||
person.critters = d
|
||
person.critters
|
||
|
||
```
|
||
|
||
Out[62]:
|
||
|
||
```py
|
||
{'cats': 4, 'dogs': 2, 'pigs': 7}
|
||
```
|
||
|
||
## 网络数据 Data from Web
|
||
|
||
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'
|
||
|
||
```
|
||
|
||
处理后就相当于一个可读文件:
|
||
|
||
In [64]:
|
||
|
||
```py
|
||
import urllib2
|
||
ge_csv = urllib2.urlopen(url)
|
||
data = []
|
||
for line in ge_csv:
|
||
data.append(line.split(','))
|
||
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',
|
||
'26.59',
|
||
'26.59',
|
||
'26.309999',
|
||
'26.43',
|
||
'28166100',
|
||
'24.88153\n'],
|
||
['2013-11-01',
|
||
'26.049999',
|
||
'26.639999',
|
||
'26.030001',
|
||
'26.540001',
|
||
'55634500',
|
||
'24.985086\n']]
|
||
```
|
||
|
||
使用`pandas`处理数据:
|
||
|
||
In [65]:
|
||
|
||
```py
|
||
ge_csv = urllib2.urlopen(url)
|
||
import pandas
|
||
ge = pandas.read_csv(ge_csv, index_col=0, parse_dates=True)
|
||
ge.plot(y='Adj Close')
|
||
|
||
```
|
||
|
||
Out[65]:
|
||
|
||
```py
|
||
<matplotlib.axes._subplots.AxesSubplot at 0xc2e3198>
|
||
```
|
||
|
||
 |