關鍵字參數:參數的名稱必須和 The parameter name of the function definition 相同
若采用Pass parameters in a mixed way: If positional arguments come after keyword arguments,程序會報錯;In turn, no error will be reported
Default value parameter form:def happy_birthday(name="abc",age=18) #That is, the formal parameter has a given value
注意項:
①【函數定義時】,When there are positional parameters and default value parameters for formal parameters,Default value parameters are placed last
可變位置參數:注意 列表傳遞 的 解包操作,前加*
可變關鍵字參數:注意 直接傳遞字典 的 解包操作,前加**
#個數可變的位置參數
def fun(*para):
print(type(para))
for item in para:
print(item)
fun(10,20,30,52) #輸出時tuple類型,(10,20,30,52)
fun(10) #輸出tuple類型,(10)
fun([10,20,30]) #輸出tuple類型,[10,20,30],A tuple has only one element and the element type is a list
fun(*[10,20,30]) #實參前加*No. to unpack the list,輸出是(10,20,30)
#個數可變的關鍵字參數
def fun2(**ecpara):
print(type(ecpara))
for key,value in ecpara.items():
print(key,value)
fun2(name="abc",age=18,height=180,weight=160) #輸出dict類型
#Pass the dictionary type directlydict
d={"name":"abc","age":18,"height":180,"weight":160}
fun2(**d) #Dictionary-like unpacking,You can wear the dictionary type
Bug的由來:
When someone was writing a program with their first computer,An error occurs consistently,So he took apart the computer and found a bug stuck in a register,成為Bug,排除錯誤debug
(1)是因為input()The function converts the input to str類型,str不能與int比較,故語法錯誤
(2)because it was not given i 賦值,printare Chinese brackets,There are no statements inside the loop body that change the loop variable
For unclear logicBugDebug using: ①使用print()Function output check ②使用“#”暫時注釋部分代碼
BaseExceptiom It is the most basic and largest exception error ,是最大的父類
BaseException as e:is to use the module as an alias e
finally模塊:常用來釋放try塊中申請的資源
*後面的 File processing closed 和 數據庫的連接 都會 放在finally模塊中
try-except-else-finally結構 compared to the previous two A more complete structure for handling program exceptions
import traceback
try:
print("---------------")
print(1/0)
except:
traceback.print_exc() #打印錯誤信息
traceback模塊 作用:use this module打印錯誤信息,may be stored in a filePut it in the log to becomelog日志