python之內建函數

來源:互聯網
上載者:User

標籤:dom   dict   通過   字串   div   ict   encoding   絕對值   隨機   

#abs函數,求絕對值


#all函數,傳入的參數是序列,只有list中所有的元素都為真,才為真,那麼什麼為真呢?
#None、‘‘Null 字元串、[]空列表、{}空字典、0均為假


#any函數,傳入的參數也是一個序列,只要有一個元素為真,則結果為真


#bin,把一個十進位數轉換成位元

#oct,把一個十進位轉換成八進位

#hex,把一個十進位轉換為十六進位



#bool,查看一個參數的布爾值

# ret = bool(0)
# print(ret)
# # False
# ret = bool(None)
# print(ret)
# # False
# ret = bool(1)
# print(ret)
# # True


#bytes函數,utf-8編碼,一個漢字是3個位元組,gbk編碼,一個漢字是2個位元組,可以把一個字串轉換成位元組類型
s = ‘施丹‘
x = bytes(s,encoding=‘utf-8‘)
print(x)
# b‘\xe9\xbd\x90\xe8\xbe\xbe\xe5\x86\x85‘

x = bytes(s,encoding=‘gbk‘)
print(x)
# b‘\xc6\xeb\xb4\xef\xc4\xda‘



#str函數,把位元組轉換成字串

#callable,是否可以執行,如果能執行則返回True,如果不能執行則返回False

f = lambda a:a-1
l = [1,2,3]

print(callable(f))
print(callable(l))

# True
# False



#ord,把一個字元轉換成ascil碼,chr是把一個ascil碼轉換字元

print(ord(‘a‘))
print(chr(65))

# 97
# A

#這裡在介紹一個random的函數,可以產生隨機數,這個在產生驗證碼的時候有用,通過random
#函數產生隨機的數字,然後在通過chr函數把這個隨機數轉換成對應的ascil碼
import random
ret = random.randint(1,34)
print(ret)



#dict,建立函數的


#dir

#divmod,求商和餘數

# ret = divmod(7,3)
# print(ret)

#enumerate,可以給輸出加一個序列
li = [‘abc‘,‘cda‘,‘afe‘]
# for i in li:
# print(i)

# abc
# cda
# afe



# for i,item in enumerate(li,1):
# print(i,item)

# 1 abc
# 2 cda
# 3 afe

def show():
print(‘a‘)
return [11,33,44]
print(‘b‘)

ret = show()
print(ret)


def show():
print(‘a‘)
if 1 == 2:
return [‘a‘,‘b‘]
print(‘b‘)
ret = show()

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.