Python编程Day05-Python函数

Python函数

  • 函数是可重复调用的代码段,能提高代码的复用率

定义格式

无参数

def print_hello():
	print "hello"

带参数

def print_str(s):
	print s
	return s*2
print_str("hello")

带默认参数

def print_default(s="hello"):
	print s
print_default()
print_default("default")

不定长参数

def print_args(s,*arg):
	print s
	for a in arg:
		print a
	return
print_args("hello")
print_args("hello","world","1")

参数次序可以变

def print_two(a,b):
	print a,b
print_two(a="a",b="b")
print_two(b="b",a="a")
发布了113 篇原创文章 · 获赞 95 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/JewaveOxford/article/details/103131097