Python函数

来源:互联网 发布:数据库中存储的是什么 编辑:程序博客网 时间:2024/05/17 09:08

函数参数

def say_hello():print('hello word')say_hello()say_hello()
函数变量

def print_max(a,b):if a > b:print(a,'is max')elif a == b:print(b,'is equal to',a)else:print(b,'is max')print_max(3,4);x = 5y = 7print_max(x,y)
函数默认值

def say(message,times=1):print(message * times)say('hello')say('word ',10)
关键字参数
def func(a,b=5,c=15):print('a is ',a,'and b is',b,'and c is',c)func(3,7)func(25,c=24)func(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=123,Shon=4156151,Inge=151651561))
return 语句

def max(x,y):if x > y:return x;elif x == y:return 'The numbers are equal';else:return y;print(max(2,3))
DocStrings

def printf_max(x,y):'''Printfs ths max of two numbers. The two values must be integers. '''x = int(x)y = int(y)if x > y:print(x,'is max')else:print(y,'is max')printf_max(3,5)print(printf_max.__doc__)

原创粉丝点击