零基础学习Python 作业 第18章
0 请问以下哪个是形参哪个是形参?
def MyFun(x):
return x ** 3
y = 3
print(MyFun(y))
x 是形参,y是实参
1 函数文档和直接使用’#’有为函数写注释有什么不同?
函数文档可以通过help进行函数名查询,但’#’注释的不行
2 使用关键字参数,可以有效避免什么问题出现呢?
准确赋值,避免赋值错误, 可以不按顺序进行赋值
3 使用help(print)查看print()这个BIF有哪些默认参数?分别起到什么作用?
print(...)
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.
# file: value, 比如一个文件; 或者系统标准输出,第一个参数如果有多个参数,作为收集参数,默认用空格隔开;
# sep: 如果在值中间插入字符串,默认每个值之间是空格
# end: 最后一个值的最后默认是换行
# flush: 是否强制清除file或者说刷新流。
4 默认参数和关键字参数表面最大的区别是什么?
关键字参数是在函数调用的时候,通过函数名制定需要赋值的参数,这样做就不怕因为搞不清参数的顺序,
出现调用出错,而默认参数是在参数定义的过程中,为形参赋初值,当函数调用的时候,不传递实参,
不传递实参,则默认使用形参的初始值代替。
def pramsCompute(*prams):
length = len(prams)
base1 = 3
base2 = 5
result = 0
for each in prams:
if prams[length - 1] != 5:
result += each
elif prams[length - 1] == 5:
if each != 5:
result += each
if prams[length - 1] != 5:
return result * base1
else:
return result * base2