1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
#c:\pyhone33
#File name: def.py
# 基本实现语句:
def
printMax(a,b):
if
a > b:
print
(a),
"is maximum"
else
:
print
(b),
"is maximum"
printMax(
3
,
4
)
x
=
5
y
=
8
printMax(x,y)
#局限于语句块的赋值:
def
func(x):
print
(
"x is"
, x)
x
=
2
print
(
"changed local x to "
, x)
x
=
50
func(x)
#使用 golbal 命令使全局赋值:
def
func1():
global
x
print
(
"x is"
, x)
x
=
2
print
(
"changed lcoal x to"
, x)
x
=
50
func1()
print
(x)
#给形参指定值:
def
func2(a,b
=
20
,c
=
30
):
print
(
"This a"
, a,
"This b"
, b,
"This c"
,c)
func2(
10
)
func2(
100
,
200
)
func2(c
=
300
,a
=
1000
)
|
本文转自bard_zhang51CTO博客,原文链接:http://blog.51cto.com/timefiles/1619810 ,如需转载请自行联系原作者