"""
Version: 0.1
Author: freshbin
Date: 2019年8月20日
"""
print("=================================函数 start================================================")
# 局部变量
x = 50
def func_x(x):
print('x is ', x)
x = 2
print('Changed local x to ', x)
func_x(x)
print('x is still ', x)
# 全局变量
y = 50
def func_y():
global y
print('y is ', y)
y = 2
print('Changed global y to ', y)
func_y()
print('Value of y is ', y)
# 默认参数值
def say(message, times=1):
print(message * times)
say('Hello')
say('Hello', 5)
# 关键字参数
def func_3(a, b=5, c=10):
print('a is', a, 'and b is', b, 'and c is', c)
func_3(3, 7)
func_3(25, c=24)
func_3(c=25, a=100)
# 可变参数
def total(a=5, *numbers, **phonebook):
print('a', a)
for single_item in numbers:
print('single_item', single_item)
for first_part, second_part in phonebook.items():
print(first_part, second_part)
print(total(10, 1, 2, 3, Jack=1123, John=2231, Inge=1560))
# return语句
def maximum(x, y):
if x > y:
return x
elif x == y:
return 'The numbers are equal'
else:
return y
print(maximum(2, 3))
def some_function():
pass
print(some_function())
# DocStrings
def print_max(x, y):
'''Prints the maximum of two numbers.
The tow values must be integers.
:param x:
:param y:
:return:
'''
x = int(x)
y = int(y)
if x > y:
return x
elif x == y:
return 'equal numbers'
else:
return y
print(print_max(3, 5))
print(print_max.__doc__)
print("=================================函数 end================================================")