標籤:一個 div 題目 lov obj 水仙花數 com return ase
筆記:1、分清楚形參和實參2、函數文檔:是函數的一部分,於解釋不同,使用help(函數名)或者 函數名__doc__可以查看到3、關鍵字參數(在一個函數的參數較多的時候作用比較明顯):給參數的名字下定義,例如:def F(name,words)如下兩種引用的方法是等價的F(A,B) = F(words=B,name=A)4、預設參數:函數定義時為形參賦初值,函數調用時若沒有傳遞參數,則自動使用初值def F(name=C,words=D)5、收集參數:def test(*params)test(1,‘小甲魚‘,3.14,7,8,9) 測試題:0. 請問以下哪個是形參哪個是實參?
def MyFun(x):
return x ** 3
y = 3
print(MyFun(y))
x是形參,y是實參。形參指的是函數建立和定義過程中小括弧裡的參數,而實參指的是函數在調用過程中傳遞進去的參數。
1. 函數文檔和直接用“#”為函數寫注釋有什麼不同?
給函數寫文檔是為了讓別人可以更好的理解你的函數,所以這是一個好習慣:
def MyFirstFunction(name):
‘函數文檔在函數定義的最開頭部分,用不記名字串表示‘
print(‘I love FishC.com!‘)
我們看到在函數開頭寫下的字串Ta是不會列印出來的,但Ta會作為函數的一部分儲存起來,這個我們稱之為函數文檔字串,Ta的功能跟注釋是一樣的。函數的文檔字串可以按如下方式訪問:
>>> MyFirstFunction.__doc__ #雙低線
‘函數文檔在函數定義的最開頭部分,用不記名字串表示‘
另外,我們用help()來訪問這個函數也可以看到這個文檔字串:
>>> help(MyFirstFunction)
Help on function MyFirstFunction in module __main__:
MyFirstFunction(name)
函數文檔在函數定義的最開頭部分,用不記名字串表示
2. 使用關鍵字參數,可以有效避免什麼問題的出現呢?
關鍵字參數,是指函數在調用的時候,帶上參數的名字去指定具體調用的是哪個參數,從而可以不用按照參數的順序調用函數
3. 使用help(print)查看print()這個BIF有哪些預設參數?分別起到什麼作用?
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.
4. 預設參數和關鍵字參數表面最大的區別是什嗎?
預設參數是賦予形式參數預設值,關鍵字參數是使得實際參數與形參相對應而避免順序錯誤引發的系統報錯。
關鍵字參數是在函數調用的時候,通過參數名制定需要賦值的參數,這樣做就不怕因為搞不清參數的順序而導致函數調用出錯。而預設參數是在參數已定義流程中,為形參賦初值,當函數調用的時候,不傳遞實參,則預設使用形參的初始值代替
動動手:
0. 編寫一個符合以下要求的函數:
a) 計算列印所有參數的和乘以基數(base=3)的結果
b) 如果參數中最後一個參數為(base=5),則設定基數為5,基數不參與求和計算。
不會:
def Sum(*params,base=3):
result = 0
for i in params:
result += i
return result*base
1. 尋找水仙花數
題目要求:如果一個3位元等於其各位元字的立方和,則稱這個數為水仙花數。例如153 = 1^3+5^3+3^3,因此153是一個水仙花數。編寫一個程式,找出所有的水仙花數。
自己寫的:
def hua():
for x in range(100,1000):
a =x%10
b =x%100//10
c =x //100
if x ==a**3+b**3+c**3:
print(x)
print(hua())
小甲魚:
def Daffodils():
print(‘所有的水仙花數為:‘,end=‘‘)
temp = 100
while temp < 1000:
if temp == (temp//100)**3 + ((temp%100)//10)**3 + (temp%10)**3:
print(temp,end=‘ ‘)
temp += 1
else:
temp += 1
print(Daffodils())
2. 編寫一個函數findstr(),該函數統計一個長度為2的子字串在另一個字串中出現的次數。例如:假定輸入的字串為"You cannot improve your past, but you can improve your future. Once time is wasted, life is wasted.",子字串為"im",函數執行後列印“子字母串在目標字串中共出現3次”。
程式執行效果:不會:縮小化,按字母進行比較
def findstr():
print(‘請輸入目標字串:‘,end=‘‘)
temp = input()
print(‘請輸入子字串(兩個字元):‘,end=‘‘)
comp = input()
count = 0
i = 0
for i in range(len(temp)):
if temp[i] == comp[0] and temp[i+1] == comp[1]:
count += 1
i += 1
else:
i += 1
count = int(count)
print(‘子字串在目標字串中總共出現 %d 次‘%count)
findstr()
小甲魚:
def findStr(desStr, subStr):
count = 0
length = len(desStr)
if subStr not in desStr:
print(‘在目標字串中未找到字串!‘)
else:
for each1 in range(length):
if desStr[each1] == subStr[0]:
if desStr[each1 + 1] == subStr[1]:
count += 1
print(‘子字串在目標字串中共出現 %d 次‘ % count)
desStr = input(‘請輸入目標字串:‘)
subStr = input(‘請輸入子字串(兩個字元):‘)
findStr(desStr, subStr)
小甲魚Python第十七講課後習題