Python函数

# 局部参数
def func(x):
    print('方法体内x=', x)
    x = 2
    print("change local x to", x)


x = 50
func(x)
print('x=', x)
# tab键的空格作用  相当于{},层级的逻辑就靠它了
print('========================')
print('使用默认参数')


def say(message, time=1):  # 不能反过来
    print(message * time)


say('hello')
say('hello', 5)
print('========================')
print('关键参数')


def func(a, b=5, c=10):
    print('a=', a, ',b=', b, ',c=', c)


func(0)
func(0, 0)
func(0, 0, 0)
func(0, c=1)

print('========================')


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


num1 = int(input('1输入数字:'))
num2 = int(input('2输入数字:'))
print('max result:')
print(maxNum(20, 30))

print('========================')
print('文档字符串')


def printMaxValue(x, y):
    '''print the max value of two '''
    if x > y:
        print('x ')
    else:
        print('y')


print(printMaxValue(1, 2))
print(printMaxValue.__doc__)

运行结果
C:\Users\dell\AppData\Local\Programs\Python\Python35\python.exe O:/Python/demo7_3.py
方法体内x= 50
change local x to 2

x= 50

使用默认参数
hello

hellohellohellohellohello

关键参数
a= 0 ,b= 5 ,c= 10
a= 0 ,b= 0 ,c= 10
a= 0 ,b= 0 ,c= 0

a= 0 ,b= 5 ,c= 1

1输入数字:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值