程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[python learning 9] functions, commonly used built-in functions in python (to be improved), bugs in python

編輯:Python

(一)函數的定義和調用

1,函數基礎知識 

 

 2,函數的參數傳遞

(1)形參與實參 

 (2)參數的類型

關鍵字參數:參數的名稱必須和 The parameter name of the function definition 相同 

若采用Pass parameters in a mixed wayIf 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的介紹

1,Bug簡介和分類

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   ②使用“#”暫時注釋部分代碼 

2 ,For passive drop pits(上圖),pythonHas an exception handling mechanism:

(1)使用 try-except 結構

 

BaseExceptiom It is the most basic and largest exception error ,是最大的父類

BaseException as e:is to use the module as an alias e

 (2)使用try-except-else結構

 (3)使用try-except-else-finally結構

 finally模塊:常用來釋放try塊中申請的資源

        *後面的 File processing closed數據庫的連接 都會 放在finally模塊中

try-except-else-finally結構 compared to the previous two A more complete structure for handling program exceptions

3,python中常見的異常類型

 (1)traceback模塊的使用

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日志


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved