學習PYTHON之路, DAY 4 - PYTHON 基礎 4 (內建函數)

來源:互聯網
上載者:User

標籤:報錯   nbsp   false   encoding   ges   glob   1.5   添加   字元   

註:查看詳細請看https://docs.python.org/3/library/functions.html#next

一 all(), any()

False: 0, Noe, ‘‘, [], {}, ()

all()  全部為真是, 才為真

any() 任何一個為真, 都為真

二 bin(), oct(),hex()

bin(), 接收十進位轉二進位 (0b)

oct(), 接收十進位轉八進位 (0o)

hex(), 接收十進位轉十六進位 (0x)

三 bytes()

bytes(只要轉換的字串,按照什麼編碼)

1 s = ‘nikita‘2 n = bytes(s, encoding=‘utf-8‘)
四 檔案操作1 開啟檔案

f = open(‘檔案名稱‘, ‘r’) #唯讀

f = open(‘檔案名稱‘, ‘w’) #唯寫,先清空原檔案

f = open(‘檔案名稱‘, ‘x‘) #如果檔案存在,報錯;不存在,建立並寫內容(python3.0新加)

f = open(‘檔案名稱‘, ‘a‘) #追加

 

2 操作檔案

read() #無參數,讀全部; 有參數,有參數,有b,按位元組,無b,按字元

readline() #僅讀取第一行

f.tell() #tell 當前指標所在的位置(位元組)

f.seek() #指標去到指定位置(位元組),但是會覆蓋後來的內容,如果是中文,結果會亂碼

write() #寫資料

flush 強刷

1 f = open(‘檔案名稱‘, ‘a’)2 f.write(‘123‘)3 f.flush() #有無沒有close,‘123‘只放在緩衝區,用來強刷就可以添加到檔案裡4 input(‘abcd‘)

readable() 是否可讀

truncate()

1 f = open(‘檔案名稱‘, ‘r+’)2 f.seek(3)3 f.truncate() #把指標後面的內容刪除4 f.close

for 迴圈檔案對象

1 f = open(‘檔案名稱‘, ‘a’)2 for line in f:3     print (line)

3 關閉檔案

f.close()

1 #讀一行,寫一行,同時開啟兩個檔案2 with open(‘檔案1‘) as f1, open(‘檔案2‘) as f2:3     for line in f1:4         f2.write(line)

replace(‘a‘, ‘b‘) #把a替換成b

 五 chr() ord()

兩者是ascii碼錶互換的函數。ord(),把字元轉成相應的ascii碼, chr(),把ascii碼轉成相應的字元。

六 compile() eval() exec()

compile() 把字串編譯成python的code, exec()執行代碼,沒有傳回值

1 s =‘print (123)‘2 r = compile(s, ‘<string>‘, ‘exec‘)3 exec(r)

eval()把字串轉成運算式,有傳回值

1 s = ‘8*8‘2 ret = evel(s)3 print(ret)
七 divmod()得到商和餘數
1 r = divmod(97, 10)2 print (r[0]) # 商3 print (r[1]) # 餘數
八 filter(), map()

filter(函數,可迭代的對象)

filter, 迴圈第二個參數,讓每個元素執行函數,如果函數傳回值為True,把元素添加到結果中

1 def f1(a):2     if a >11:3         return True4 li = [1, 22, 33]5 ret = filter(f1, li)6 print(list(ret))

map(函數,可迭代的對象)

map 將函數傳回值添加到結果中

li = [1, 2, 3]ret = map(lambda a: a +10, li)print(list(ret))
九 zip()
1 I1 = [‘I‘, ‘0‘, ‘2‘]2 I2 = [‘am‘, ‘1‘, ‘2‘]3 I3 = [‘Nikita‘, ‘1‘, ‘2‘]4 r = zip (I1, I2, I3)5 temp = list(r)[0]6 ret = ‘ ‘.join(temp)7 print(ret)

如果其中一個列表元素比其他列表少了一個, 其他列表的也不會放入r裡

I1 = [‘I‘, ‘0‘, ‘4‘]I2 = [‘am‘, ‘1‘]I3 = [‘Nikita‘, ‘3‘, ‘5‘]r = zip (I1, I2, I3)
print(list(r))

r的結果是[(‘I‘, ‘am‘, ‘Nikita‘), (‘0‘, ‘1‘, ‘3‘)]

十其他dir() help()

dir()查看模組,對象或者類的提供的功能

help()列出詳細功能

isinstance()
1 s = ‘abc‘2 r = isinstance(s, list) #判斷s是否屬於list的對象3 print(r)
locals() globals()

locals() 查看局部變數

globals() 查看全部變數

hash()

把任意長度的輸入的二進位值,通過散列演算法,變換成固定長度的輸出,該輸出就是散列值

round()

四捨五入

學習PYTHON之路, DAY 4 - PYTHON 基礎 4 (內建函數)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.