0
点赞
收藏
分享

微信扫一扫

Python编程Day05-Python函数



Python中的函数


  • ​​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")



举报

相关推荐

0 条评论