全部博文(64)
分类: Python/Ruby
2009-09-21 14:22:22
#!/usr/bin/python
# Filename: function1.pydef
sayHello
():
print
'Hello World!'
# block belonging to the function
sayHello()
# call the function
#!/usr/bin/python
# Filename: func_param.pydef
printMax
(a, b):
if
a > b:
print
a,
'is maximum'
else
:
print
b,
'is maximum'
printMax(
3
,
4
)
# directly give literal values
x =
5
y =
7
printMax(x, y)
# give variables as arguments
#!/usr/bin/python
# Filename: func_local.pydef
func
(x):
print
'x is'
, x
x =
2
print
'Changed local x to'
, x
x =
50
func(x)
print
'x is still'
, x
#!/usr/bin/python
# Filename: func_global.pydef
func
():
global
x
print
'x is'
, x
x =
2
print
'Changed local x to'
, x
x =
50
func()
print
'Value of x is'
, x
#!/usr/bin/python
# Filename: func_default.pydef
say
(message, times =
1
):
print
message * times
say(
'Hello'
)
say(
'World'
,
5
)
#!/usr/bin/python
# Filename: func_key.pydef
func
(a, b=
5
, c=
10
):
print
'a is'
, a,
'and b is'
, b,
'and c is'
, c
func(
3
,
7
)
func(
25
, c=
24
)
func(c=
50
, a=
100
)
#!/usr/bin/python
# Filename: func_return.pydef
maximum
(x, y):
if
x > y:
return
x
else
:
return
y
print
maximum(
2
,
3
)
#!/usr/bin/python
# Filename: func_doc.pydef
printMax
(x, y):
'''Prints the maximum of two numbers.
The two values must be integers.''' x =
int
(x)
# convert to integers, if possible
y =
int
(y)
if
x > y:
print
x,
'is maximum'
else
:
print
y,
'is maximum'
printMax(
3
,
5
)
print
printMax.__doc__