Python函數

函數參數

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 = 5
y = 7
print_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__)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章