python---內置函數

#print(abs(-5))                      #取絕對值
# print(bin(4) )                     #把數字轉成二進制格式
# print(bool())                      #判斷真假,0爲假,1爲真,空位假,非空爲真
# print(chr(98))                    #輸入ascill碼中的數字返回相應的字符
# print(ord("a"))                   #輸入ascill碼中的字符返回相應的數字
# __import__("模塊名")              #以字符串的格式導入模塊
# print(divmod(5,3))                #返回商和餘數
# enumerate()                       #可列出列表索引號
#oct(23)                            #將十進制轉爲八進制
# pow(3,2)                          #返回3的2次方
# reversed()                        #反轉
# round(1.3342,2)                   #小數點後保留兩位
# print(max([1,2,3,4]))             #返回最大值
# print(min([1,2,3,4]))
# print(callable(test))             #判斷是否可調用,如函數,類可調用。。
# a = frozenset(set([1,4,7,34,45,33,33,34]))          #將集合變爲不可修改
# print(globals())                  #以key-vlaue的形式返回當前程序的全部的全局變量--值
# print(locals())                   #以key-vlaue的形式返回當前程序的全部的局部變量--值
# print(hash("fxl"))                #將字符串轉化成一個hash值,python 中字典就是運用hash算法,每個key都對應了一個hash值
# print(hex(255) )                  # 將一個數字轉化爲16進制
# print(id(a))                      #返回內存地址
#----------------------

#a = [-5,0,6]                 #零爲假,非零爲真
# print(all(a))               # 可迭代對象中全部值爲真返回Ture,不然返回False。空返回Ture
# print(any(a))               #可迭代對象中的值只要有一個爲真,返回Ture,不然False。空返回False
# print(ascii(a))             #將可迭代對象變成字符串格式
# print(dir(a))               #查看數據對象有哪些方法
#----------------------

# c = bytes("abcde",encoding="utf-8")        #將字符串轉化爲bytes類型,不能修改
# d = bytearray("abcde",encoding="utf-8")    #將字符串轉化爲bytes類型,能夠修改
# d[1] = 50
# print(d)
#----------------------

code = '''
import sys,time
for i in range(20):
    sys.stdout.write("#")       #標準輸出到屏幕
    sys.stdout.flush()          #輸出一個當即刷新
    time.sleep(0.1)
'''
exec(code)                           #exec 能夠將字符串轉化爲可執行的代碼
#----------------------

dic = '''{
"name1":"cx",
"name2":"fxl"
}
'''
print(eval(dic))                            #將字符串變成字典
#----------------------

#匿名函數
# calc = lambda n:print(n)                 #匿名函數,只能進行一些簡單的操做
# calc(5)
# calc = lambda n:3 if n>5 else 4
# print(calc(6))

#匿名函數結合內置方法使用
#res = filter(lambda n:n>5,range(10))       #過濾出大於5的列表
#res = map(lambda n:n*2,range(10))          #過濾出全部值
#res = map(lambda n:n>5,range(10))          #符合條件Ture,不符合爲false
# for i in res:
#     print(i)

# import functools
# res = functools.reduce(lambda x,y:x+y,range(10))        #累加
# print(res)
#----------------------

# a = {2:4,8:16,1:4,4:32,-5:3}
# print(sorted(a.items()))                                 #將字典變爲列表,在按照key排序
# #[(-5, 3), (1, 4), (2, 4), (4, 32), (8, 16)]
# print(sorted(a.items(),key=lambda x:x[1]))                #將字典變爲列表,在按照vlaue排序
# #[(-5, 3), (1, 4), (2, 4), (8, 16), (4, 32)]
#----------------------

# a = [1,2,3,4]
# b = ["a","b","c","d"]
# c = zip(a,b)                                              #將兩個列表組合,按最少的那個列表組合
# for i in c:
#     print(i)
# # (1, 'a')
# # (2, 'b')
# # (3, 'c')
# # (4, 'd')
相關文章
相關標籤/搜索