12 Python內建函式

2021-10-04 15:23:21 字數 2132 閱讀 4042

help

# 下面兩行分別為sys模組和str類的具體說明。

help

('sys'

)help

('str'

)help([

1,2,

3])#會詳細說明list的用法

help([

1,2,

3]

dict:
class

dict

(object)|

dict()

-> new empty dictionary

|dict- |

(key, value) pairs

|dict

(iterable)

-> new dictionary initialized as

if via:

| d =

|for k, v in iterable:

| d[k]

= v |

dict

(**kwargs)

-> new dictionary initialized with the name=value pairs

|in the keyword argument list

. for example:

dict

(one=

1, two=2)

|| methods defined here:

|| __contains__(self, key,/)

|true

if d has a key k,

else

false.|

| __delitem__(self, key,/)

| delete self[key]

.

通過上面help得到的資訊,我們可以通過四種方式進行建立字典,

# 1. 建立空字典 

a =dict()

#空字典

# 2. 通過關鍵字

dictionary =

dict

(a =

'1', b =

'2')

# # 3. 通過隱射

a =dict

(zip([

'a',

'b',

'c'],[

1,2,

3]))

# # 4. 通過傳入可迭代物件

a =dict([

('one',1

),('two',2

),('three',3

)])#

zip([iterable, …])

將可迭代的物件進行壓縮成乙個物件,可以通過*進行解壓縮

a =[1

,2,3

]b =[2

,4,6

]zipped =

zip(a, b)

print

(zippend)

## 可以通過list進行解壓縮,

list

(zipped)

# [(1, 2), (2, 4), (3, 6)] 返回乙個list

abs:

取絕對值

a =

abs(-1

)# 1

all:

用於判斷給定的可迭代引數中的所有元素是否全部不為0, false, none,空

但是值得注意的是,空元組或者是空列表返回的值為true

all([

1,2,

3])# true

all([0

,1,2

])# false 因為有個0

all(

['a'

,'b',''

])# false, 因為其中有個空 ''

all(()

)# true

all(

)# true

dir:

返回模組中的變數,方法和定義,如果帶引數則返回引數的屬性,方法列表

dir([

])# 返回list的方法列表

12 python基礎 函式

12.1 函式簡介一段具有特定功能的 可重用的語句組 函式規則 1.def 2.return 表示式 結束函式,不帶表示式的return相當於返回 none 作用 降低程式設計難度和 復用def 函式名 引數 引數是佔位符 函式體return 返回值 引數是輸入 函式體是處理 結果是輸出 ipo 函...

12 Python函式學習(中)

作用域 區域性和全域性變數 前向引用 deftest name,age 18,args,kwargs print name print age,args,kwargs school test 程式執行從上到下的,這裡的school還沒定義,所以執行報錯。test alex age 20,flag f...

12 Python 檔案處理

資料夾 得到當前工作目錄,即當前python指令碼工作的目錄路徑 os.getcwd 返回指定目錄下的所有檔案和目錄名 os.listdir 函式用來刪除乙個檔案 os.remove 刪除多個目錄 os.removedirs r c python 檢驗給出的路徑是否是乙個檔案 os.path.isf...