python內建方法總結

來源:互聯網
上載者:User

標籤:應該   功能   enter   調用   res   with open   製造   pos   類型   

# 注意:內建函數id()可以返回一個對象的身份,傳回值為整數。這個整數通常對應與該對象在記憶體中的位置,但這與python的具體實現有關,不應該作為對身份的定義,即不夠精準,最精準的還是以記憶體位址為準。# is運算子用於比較兩個對象的身份,等號比較兩個對象的值,內建函數type()則返回一個對象的類型以下優先列出需要掌握的內建函數print(abs(-1))  # 1  取絕對值

print(all([2, 3, 4, ‘sss‘, ‘True‘])) # 裡面的值全為Ture,則返回True,如果裡面的的值為空白也返回True
print(any(‘3‘))      #裡面的值只要有一個為Ture,擇返回True,如果裡面的值為空白,則返回False

print(bytes(‘你好,albert‘, encoding=‘utf-8‘)) # 把utf-8編碼的字串轉化為二進位
print(callable(‘d‘.strip)) # 判斷裡面的值是不是可迭代類型 後面可以加()的都是,比如‘abc’.strip() 、 max() 等等

print(bin(11)) #十進位轉二進位
print(oct(11)) #十進位轉八進位
print(hex(11)) #十進位轉十六進位

print(bool(0)) #0,None,空的布爾值為假

res=‘你好egon‘.encode(‘utf-8‘) # unicode按照utf-8進行編碼,得到的結果為bytes類型
res=bytes(‘你好egon‘,encoding=‘utf-8‘) # 同上
print(res)

def func():
pass
print(callable(‘aaaa‘.strip)) #判斷某個對象是否是可以調用的,可調用指的是可以加括弧執行某個功能

print(chr(90)) #按照ascii碼錶將十進位數字轉成字元
print(ord(‘Z‘)) #按照ascii碼錶將字元轉成十進位數字

print(dir(‘abc‘)) # 查看某個對象下可以用通過點調用到哪些方法
輸出:

[‘__add__‘, ‘__class__‘, ‘__contains__‘, ‘__delattr__‘, ‘__dir__‘, ‘__doc__‘, ‘__eq__‘, ‘__format__‘, ‘__ge__‘, ‘__getattribute__‘, ‘__getitem__‘, ‘__getnewargs__‘, ‘__gt__‘, ‘__hash__‘, ‘__init__‘, ‘__init_subclass__‘, ‘__iter__‘, ‘__le__‘, ‘__len__‘, ‘__lt__‘, ‘__mod__‘, ‘__mul__‘, ‘__ne__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__rmod__‘, ‘__rmul__‘, ‘__setattr__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘, ‘capitalize‘, ‘casefold‘, ‘center‘, ‘count‘, ‘encode‘, ‘endswith‘, ‘expandtabs‘, ‘find‘, ‘format‘, ‘format_map‘, ‘index‘, ‘isalnum‘, ‘isalpha‘, ‘isdecimal‘, ‘isdigit‘, ‘isidentifier‘, ‘islower‘, ‘isnumeric‘, ‘isprintable‘, ‘isspace‘, ‘istitle‘, ‘isupper‘, ‘join‘, ‘ljust‘, ‘lower‘, ‘lstrip‘, ‘maketrans‘, ‘partition‘, ‘replace‘, ‘rfind‘, ‘rindex‘, ‘rjust‘, ‘rpartition‘, ‘rsplit‘, ‘rstrip‘, ‘split‘, ‘splitlines‘, ‘startswith‘, ‘strip‘, ‘swapcase‘, ‘title‘, ‘translate‘, ‘upper‘, ‘zfill‘]

print(divmod(1311,25)) # 輸出一個元祖,包含商和餘數 (52,11)

eval 重點介紹,可用於檔案讀寫操作
# 將字元內的運算式拿出運行一下,並拿到該運算式的執行結果
res=eval(‘{"name":"egon","age":18}‘)
print(res,type(res))
輸出:{‘name‘: ‘egon‘, ‘age‘: 18} <class ‘dict‘>
# eval 在檔案中的應用
with open(‘db.txt‘,‘r‘,encoding=‘utf-8‘) as f:
s=f.read()
dic=eval(s)
print(dic,type(dic))
print(dic[‘egon‘])
# eval 可以吧預設檔案開啟得到的字串運算式拿出來運行一下,並拿到運算式的執行結果, 轉化成原本的資料類型,進而更方便進行讀寫操作

fset=frozenset({1,2,3})  #集合是一個可變類型,frozenset可以製造不可變集合,fset已沒有.add方法。

print(len({‘x‘:1,‘y‘:2})) #{‘x‘:1,‘y‘:2}.__len__()

obj=iter(‘egon‘) #‘egon‘.__iter__()
print(next(obj)) #obj.__next__()
print(next(obj))#obj.__next__()
print(iter(obj))#obj.__next__()

輸出結果

2
e
g
<str_iterator object at 0x00000000021E7160>





 

python內建方法總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.