def print_something():
print 'something'
print_something()
something
def div(lhs, rhs):
return lhs / rhs
print div(5, 2)
2
print print_something()
something None
print div(rhs=2, lhs=3.0)
1.5
具有默认值的参数必需写在参数列表的最后
def increase(x, step=1):
return x + step
print increase(3)
print increase(3, 2)
4 5
def print_twice(x):
print x
print x
print(42)
File "<ipython-input-6-854b0cb8133b>", line 3 print x ^ IndentationError: unindent does not match any outer indentation level
def print_twice(x):
print x
print x
print(42)
File "<ipython-input-7-784fcb9d908a>", line 3 print x ^ IndentationError: unexpected indent
函数体内的同一级代码必需与函数体内第一行代码的缩进保持一致:
def print_twice(x):
print x
print x
print(42)
42