- 课上代码
#形参和实参
>>> def MyFirstFunction(name):
'函数定义过程中的name是形参'
#因为它只是一个形式,表示占据一个参数位置
print('传递进来的' + name + '叫做实参,因为它是具体的参数值')
>>> MyFirstFunction('Jack')
传递进来的Jack叫做实参,因为它是具体的参数值
>>> MyFirstFunction.__doc__
'函数定义过程中的name是形参'
>>> help(MyFirstFunction)
Help on function MyFirstFunction in module __main__:
MyFirstFunction(name)
函数定义过程中的name是形参
>>> print.__doc__
"print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)\n\nPrints the values to a stream, or to sys.stdout by default.\nOptional keyword arguments:\nfile: a file-like object (stream); defaults to the current sys.stdout.\nsep: string inserted between values, default a space.\nend: string appended after the last value, default a newline.\nflush: whether to forcibly flush the stream."
>>> help(print)
Help on built-in function print in module builtins:
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.
#关键字参数
>>> def SaySome(name, words):
print(name + '->' + words)
>>> SaySome('Jack', ' You are amazing')
Jack-> You are amazing
>>> SaySome('You are amazing', 'Jack')
You are amazing->Jack
>>> SaySome(words = 'You are amazing', name = 'Jack')
Jack->You are amazing
#默认参数
>>> def SaySome(name = 'Jack', words = 'You are amzing'):
print(name + '->' + words)
>>> SaySome()
Jack->You are amzing
>>> SaySome('Rose')
Rose->You are amzing
>>> SaySome('Rose', '你臭不要脸!')
Rose->你臭不要脸!
#收集参数
>>> def test(*params):
print('参数的长度是: ', len(params))
print('第二个参数是: ', params[1])
>>> test(1, 'Jack', 3.14, 5, 6, 7, 8)
参数的长度是: 7
第二个参数是: Jack
>>> def test(*params, exp):
print('参数的长度是:', len(params), exp)
print('第二个参数是:', params[1])
>>> test(1, 'Jack', 3.14, 5, 6, 7, 8)
Traceback (most recent call last):
File "<pyshell#29>", line 1, in <module>
test(1, 'Jack', 3.14, 5, 6, 7, 8)
TypeError: test() missing 1 required keyword-only argument: 'exp'
>>> test(1, 'Jack', 3.14, 5, 6, 7, exp = 8)
参数的长度是: 6 8
第二个参数是: Jack
>>> def test(*params, exp = 8):
print('参数的长度是:', len(params), exp)
print('第二个参数是:', params[1])
>>> test(1, 'Jack', 3.14, 5, 6, 7, 8)
参数的长度是: 7 8
第二个参数是: Jack
二. 动动手
- 编写一个符合以下要求的函数:
a. 计算打印所有参数的和乘以基数(base = 3)的结果
b. 如果参数中最后一个参数为(base = 5), 则设定基数为5,基数不参与求和计算
def mFun(*param, base = 3):
result = 0
for each in param:
result += each
result *= base
print('The result is ', result)
mFun(1, 2, 3, 4, 5, base = 5)
- 寻找水仙花数
#个人代码
for i in range(100, 1000):
a = i % 10
b = i // 10 % 10
c = i // 100
if i == a ** 3 + b ** 3 + c ** 3:
print(i)
#参考代码
def Narcissus():
for each in range(100, 1000):
temp = each
sum = 0
while temp:
sum = sum + (temp % 10) ** 3
temp = temp // 10
#这一步很巧妙呀
if sum == each:
print(each, end = '\t')
print("Narcissus is ", end = '')
Narcissus()
- 编写一个函数findstr(),该函数统计一个长度为2的子字符串在另一个字符串中出现的次数
#个人代码
#但是不会把它转变成函数模式
num = 0
total_string = input("Please input your string: ")
check_string = input('Please input your checking string: ')
length = len(total_string) - 1
for i in range(length):
if check_string[0] == total_string[i] and check_string[1] == total_string[i+1]:
num += 1
print('Song string have been appearing {} times'.format(num))
#参考代码
def findStr(desStr, subStr):
count = 0
length = len(desStr)
if subStr not in desStr:
print('No finding')
else:
for each1 in range(length - 1):
if desStr[each1] == subStr[0]:
if desStr[each1 + 1] == subStr[1]:
count += 1
print('The checking string is appearing %d times' % count)
desStr = input("Please input your string: ")
subStr = input("Please input your checking string: ")
findStr(desStr, subStr)