# 局部参数
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输入数字: