Chinaunix首页 | 论坛 | 博客
  • 博客访问: 202908
  • 博文数量: 64
  • 博客积分: 2536
  • 博客等级: 少校
  • 技术积分: 610
  • 用 户 组: 普通用户
  • 注册时间: 2009-07-16 16:39
文章分类

全部博文(64)

文章存档

2011年(2)

2010年(1)

2009年(61)

我的朋友

分类: Python/Ruby

2009-09-21 14:22:22

#!/usr/bin/python
# Filename: function1.py


def sayHello():
    print 'Hello World!' # block belonging to the function

sayHello() # call the function

#!/usr/bin/python
# Filename: func_param.py


def 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.py


def 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.py


def 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.py


def say(message, times = 1):
    print message * times

say('Hello')
say('World', 5)

#!/usr/bin/python
# Filename: func_key.py


def 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.py


def maximum(x, y):
    if x > y:
        return x
    else:
        return y

print maximum(2, 3)

#!/usr/bin/python
# Filename: func_doc.py


def 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__

 

 

 

阅读(1161) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~