python 基礎 內建函數

來源:互聯網
上載者:User

標籤:ted   lob   reverse   ext   pre   end   pop   copy   nta   

 

內建參數
print(all([5,-1,5]))  # 非0都是真 trueprint(all([0,-1,5]))  # falseprint(any([1,0,5]))  # 有一個資料為真,就為真print(any([]))       # false# 把數字轉換成二進位print(bin(1))‘‘‘>>> bin(1)‘0b1‘>>> bin(2)‘0b10‘>>> bin(16)‘0b10000‘>>> bin(255)‘0b11111111‘>>>‘‘‘‘‘‘# 判斷真假>>> bool(1)True>>> bool(0)False>>> bool(5)True>>> bool([])False>>> bool({})False>>> bool({1})True>>> bool([241])True‘‘‘‘‘‘a = bytes("abcd",encoding="utf8")print(a.capitalize(),a)b = bytearray("abcd",encoding="utf8")print(b[1])  # 列印asciib[1]= 50print(b)‘‘‘# 判斷一個事情可否調用 可調用true 不可調用falseprint(callable([]))Falsedef abc1():passprint(callable(abc1) )True‘‘‘# ascii數字對應字串轉換>>>>>>>>> chr(97)‘a‘>>> chr(98)‘b‘>>> chr(90)‘Z‘>>> chr(99)‘c‘>>># 反過來 必須輸入ascii字元 轉換成數字>>> ord(‘a‘)97>>> ord(‘b‘)98>>> ord(‘c‘)99>>> ord(‘1‘)49>>>‘‘‘‘‘‘# 查看 可以用什麼方法>>> a = []>>>>>> dir(a)[‘__add__‘, ‘__class__‘, ‘__contains__‘, ‘__delattr__‘, ‘__delitem__‘, ‘__dir__, ‘__doc__‘, ‘__eq__‘, ‘__format__‘, ‘__ge__‘, ‘__getattribute__‘, ‘__getitem__, ‘__gt__‘, ‘__hash__‘, ‘__iadd__‘, ‘__imul__‘, ‘__init__‘, ‘__init_subclass__‘ ‘__iter__‘, ‘__le__‘, ‘__len__‘, ‘__lt__‘, ‘__mul__‘, ‘__ne__‘, ‘__new__‘, ‘__educe__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__reversed__‘, ‘__rmul__‘, ‘__setattr__, ‘__setitem__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘, ‘append‘, ‘clear‘ ‘copy‘, ‘count‘, ‘extend‘, ‘index‘, ‘insert‘, ‘pop‘, ‘remove‘, ‘reverse‘, ‘sor‘]>>>‘‘‘‘‘‘可以把 字串轉換成原來的資料類型  例如:原來是 list ,dicteval()‘‘‘‘‘‘# exec函數主要用於執行語句塊>>> exec(‘a=1+3*2*2‘)>>> exec<built-in function exec>>>> a13>>>‘‘‘

 

def abc1(n):    print(n)abc1(3)# 傳參數(lambda c:print(c))(110)abc = lambda c:print(c)abc(5)abc = lambda c:10 if c<5 else cprint(abc(3))print("===========================================")# filter# 列印>6的res = filter(lambda n:n>6,range(10))for i in res:    print(i)print("===========================================")# map# 把裡面的集合每個資料 拿出來給前面的函數處理 然後用list方式列印出來res = map(lambda n:n*2,range(10))for i in res:    print(i)024681012141618print("===========================================")# 累加 reduceimport functoolsres = functools.reduce(lambda x,y:x+y,range(1,10))print(res)# 累乘res = functools.reduce(lambda x,y:x*y,range(1,10))print(res)print("===========================================")# 判斷變數存在否# print(globals())‘‘‘>>>>>> hash(1)1>>> hash(2)2>>> hash("ming")2265504022069637367>>>>>> hash("mike")-5868197253725756830>>>‘‘‘# 把一個數 轉換成16進位‘‘‘>>>>>> hash(1)1>>> hash(2)2>>> hash("ming")2265504022069637367>>>>>> hash("mike")-5868197253725756830>>>‘‘‘# 返回多少次冪  例如 pow(x,y) x的y次方‘‘‘>>>>>> pow(3,3)27>>> pow(5,2)25>>> pow(8,2)64>>>‘‘‘# 排序 從小到大a = {6:2,8:0,1:4,-5:6,99:11,4:22}#print(a)print(sorted(a))[-5, 1, 4, 6, 8, 99]print(sorted(a.items()))    # key排序[(-5, 6), (1, 4), (4, 22), (6, 2), (8, 0), (99, 11)]print(sorted(a.items(),key=lambda x:x[1]))    # 按value排序,x代表一個元素[(8, 0), (6, 2), (1, 4), (-5, 6), (99, 11), (4, 22)]print("===========================================")# 把兩個列表對應起來 合并d = [1,2,3,4,5,6]e = [‘a‘,‘b‘,‘c‘,‘d‘,‘e‘,‘f‘]for i in zip(d,e):    print(i)(1, ‘a‘)(2, ‘b‘)(3, ‘c‘)(4, ‘d‘)(5, ‘e‘)(6, ‘f‘)print("===========================================")__import__(‘產生器‘)

 

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.