# 零基础学编程022：函数的世界

```import urllib.request as req

with req.urlopen('http://hq.sinajs.cn/list=gb_goog') as f :
v = hq.split(',')
print(v[1])```

import urllib.request as req with req.urlopen('http://hq.sinajs.cn/list=gb_goog') as f : hq = f.read().decode('GBK') v = hq.split(',') print(v[1]) with req.urlopen('http://hq.sinajs.cn/list=gb_amzn') as f : hq = f.read().decode('GBK') v = hq.split(',') print(v[1]) with req.urlopen('http://hq.sinajs.cn/list=gb_fb') as f : hq = f.read().decode('GBK') v = hq.split(',') print(v[1]) with req.urlopen('http://hq.sinajs.cn/list=gb_aapl') as f : hq = f.read().decode('GBK') v = hq.split(',') print(v[1]) with req.urlopen('http://hq.sinajs.cn/list=rt_hk00700') as f : hq = f.read().decode('GBK') v = hq.split(',') print(v[2]) #注意港股有所不同 with req.urlopen('http://hq.sinajs.cn/list=gb_baba') as f : hq = f.read().decode('GBK') v = hq.split(',') print(v[1])

```def f(x) :
return (1 + 0.01) ** x```

```print( f(100) )
print( f(365) )```

```def price(stock) :
url = 'http://hq.sinajs.cn/list=' + stock
with req.urlopen(url) as f :
v = hq.split(',')
return v[1]```

```print( price('gb_goog') )
print( price('gb_amzn') )
print( price('gb_fb') )
print( price('gb_aapl') )
print( price('rt_hk00700') )
print( price('gb_baba') ) ```

• 函数function可以减少重复性代码，便于将来的维护
• 有内置函数，我们自己写的叫自定义函数
• python中大量模块中包括了大量函数
• def 关键字用于定义一个函数块
• 冒号之后的各行是函数体，要注意缩进
• 用return返回想要的结果
• 调用函数的基本形式：函数名( 函数参数 )

--- END ---

178 篇文章50 人订阅

0 条评论

## 相关文章

●  工厂方法模式的具体工厂类只能创建一个具体产品类的实例，而抽象工厂模式可以创建多个。

902

2432

2635

823

1937

1304

### 对"for循环中尽量减少对变量的重复计算"提出质疑

===========================================================

921

1013

3595

852