python 內建函數

來源:互聯網
上載者:User

標籤:匿名函數   als   items   字元   call   pre   判斷   資料   排序   

本篇主要總結一下Python常用的內建函數

#強制類型轉化
# print(int(‘333‘))#字串轉化成int型,字串必須全部是數字
# print(float(‘2‘))#把字串或整形轉化成float
# print(dict(k=1,v=2))#轉化為字典
# print(list(‘123ww‘))#轉為list
# print(set(‘123‘))#轉化為集合,把要傳入的元素拆分,做為個體傳入到集合
#判斷對象是否可調用
# print(callable(‘aa‘))#返回false,因為aa是一個字串,不是函數
# print(chr(98))#返回整數i對應的ASCII字元,與ord()作用相反。參數x:取值範圍[0, 255]之間的正數。
# print(ord(‘b‘))#列印字串對應的ascii碼
# print(eval(‘1+2‘))#執行簡單的Python代碼,定義資料類型和運算
# print(exec (‘def a(): print(1234)‘))#執行Python代碼
# print(a())
# print(round(3.1415926,2))#取幾位小數,會四捨五入
# print(sorted([5,3,2,6,7]))#排序,正序
# 匿名函數定義使用lambda關鍵字
s=lambda x,y:x+y #冒號前邊是參數,冒號後邊是傳回值
# 因為函數即變數,如果沒有定一個變數把lambda存起來的話,它就不在記憶體裡,沒法執行,所有把它放到s這個變數裡面
print(s(3,4))
dic ={
"zhu":18,
"yue":29
}
newname=sorted(dic.items())#預設按照字典的key進行排序(字典是無序的,把字典轉化成list)
print(‘newname‘,newname)
print(dic.items())
newname1=sorted(dic.items(),key=lambda x:x[1])#按照字典的vaule 排序
print(newname1)
print(dict(newname))#又轉化為字典

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.