Skip to content

Commit 43d30d6

Browse files
committed
函数学习
1 parent 42549a3 commit 43d30d6

File tree

8 files changed

+146
-0
lines changed

8 files changed

+146
-0
lines changed

python/函数/area.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# @Date : 2015-05-17 14:41:50
4+
# @Author : chenhao (wy.chenhao@qq.com)
5+
# @Link : http://www.xjchenhao.cn
6+
# @Version : $Id$
7+
8+
import math
9+
10+
11+
def area(radius):
12+
""" 返回一个圆的面积
13+
用给定的半径。
14+
例如:
15+
>>> area(5.5)
16+
95.0331777711
17+
"""
18+
return math.pi * radius ** 2
19+
20+
print(area(5.5))
21+
print(area.__doc__)

python/函数/global_corrent.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# @Date : 2015-05-17 15:05:22
4+
# @Author : chenhao (wy.chenhao@qq.com)
5+
# @Link : http://www.xjchenhao.cn
6+
# @Version : $Id$
7+
8+
name = 'Jack'
9+
10+
11+
def say_hello():
12+
print('Hello ' + name + ' !')
13+
14+
15+
def change_name(new_name):
16+
global name
17+
name = new_name
18+
say_hello()
19+
change_name('chenhao')
20+
say_hello()

python/函数/global_error.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# @Date : 2015-05-17 15:02:52
4+
# @Author : chenhao (wy.chenhao@qq.com)
5+
# @Link : http://www.xjchenhao.cn
6+
# @Version : $Id$
7+
8+
name = 'Jack'
9+
10+
11+
def say_hello():
12+
print('Hello ' + name + ' !')
13+
14+
15+
def change_name(new_name):
16+
name = new_name
17+
say_hello()
18+
change_name('chenhao')
19+
say_hello()

python/函数/greetings.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# @Date : 2015-05-17 15:17:04
4+
# @Author : chenhao (wy.chenhao@qq.com)
5+
# @Link : http://www.xjchenhao.cn
6+
# @Version : $Id$
7+
8+
9+
def greet(name, greeting='Hello'):
10+
print(greeting + ' ' + name + '!')
11+
greet('chenhao', 'hi')
12+
greet('chenhao')

python/函数/import_shapes1.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# @Date : 2015-05-17 15:50:58
4+
# @Author : chenhao (wy.chenhao@qq.com)
5+
# @Link : http://www.xjchenhao.cn
6+
# @Version : $Id$
7+
8+
import shapes
9+
# print(dir(shapes))
10+
11+
shapes.rectangle(2, 3)

python/函数/import_shapes2.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# @Date : 2015-05-17 15:57:43
4+
# @Author : chenhao (wy.chenhao@qq.com)
5+
# @Link : http://www.xjchenhao.cn
6+
# @Version : $Id$
7+
8+
from shapes import *
9+
10+
rectangle(2, 3)

python/函数/shapes.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# @Date : 2015-05-17 15:32:03
4+
# @Author : chenhao (wy.chenhao@qq.com)
5+
# @Link : http://www.xjchenhao.cn
6+
# @Version : $Id$
7+
8+
"""
9+
函数的集合
10+
打印基本形状
11+
"""
12+
CHAR = '*'
13+
14+
15+
def rectangle(heigh, width):
16+
"""打印矩形."""
17+
for row in range(heigh):
18+
for col in range(width):
19+
print(CHAR),
20+
print('')
21+
22+
23+
def square(side):
24+
"""打印一个正方形."""
25+
rectangle(side, side)
26+
27+
28+
def triangle(heigh):
29+
"""打印直角三角形."""
30+
for row in range(heigh):
31+
for col in range(1, row + 2):
32+
print(CHAR),
33+
print('')
34+
35+
# rectangle(2, 3)
36+
# square(3)
37+
# triangle(3)

python/函数/shopping.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# @Date : 2015-05-17 15:24:42
4+
# @Author : chenhao (wy.chenhao@qq.com)
5+
# @Link : http://www.xjchenhao.cn
6+
# @Version : $Id$
7+
8+
9+
def shop(where='store',
10+
what='pasta',
11+
howmuch='10 pounds'):
12+
print('I want you to go to the ' + where)
13+
print('and buy ' + howmuch + ' of ' + what + '.')
14+
shop()
15+
16+
shop(where='xianju', howmuch='30 pounds')

0 commit comments

Comments
 (0)