1. 程式人生 > >05python 的內置函數以及匿名函數(python函數)

05python 的內置函數以及匿名函數(python函數)

abs 3.6 object 叠代 pytho std for 函數 word

內置函數

截止到python版本3.6.2,現在python一共為我們提供了68個內置函數。它們就是python提供給你直接可以拿來使用的所有函數。

技術分享圖片

作用域相關

技術分享圖片

print(globals())        # 返回本地作用域中的所有名字
print(locals())         # 返回全局作用域中的所有名字

註意與函數中的global、local、nonlocal 關鍵字的區別

生成器叠代器相關

技術分享圖片

for i in range(10):
    print(i, end= )
    
>>>0 1 2 3 4 5 6 7 8 9

for
i in range(1, 10, 2): print(i, end= ) >>>1 3 5 7 9

技術分享圖片

其他

技術分享圖片

dir()                        # pass
print(callable(func))        # callable()接受一個函數名則返回True,接收其他變量返回False,可用於判斷變量是否為函數!雞肋    
help()                       # 雞肋
import                       # 導入模塊
open()                       #
打開文件 hash() # 字典中的鍵所對應的就是hash值 ------------------------------------------------ input() # 用於與命令行的交互 print() # 查看print的源碼 def print(self, *args, sep= , end=\n, file=None): # known special case of print """ print(value, ..., sep=‘ ‘, end=‘\n‘, file=sys.stdout, flush=False) Prints the values to a stream, or to sys.stdout by default. Optional keyword arguments: file: a file-like object (stream); defaults to the current sys.stdout. sep: string inserted between values, default a space. end: string appended after the last value, default a newline. flush: whether to forcibly flush the stream.
""" pass print(1, 2, 3, sep=|) >>>1|2|3 f = open(file, w) print(aaa, file=f) # 打印內容到指定文件 ----------------------------------------------------------------------- eval(print(123)) exec(print(123)) print(eval(1+2+3+4)) # 有返回值 print(exec(1+2+3+4)) # 無返回值

和數字處理相關

技術分享圖片

print(round(3.1415926, 2))            # 限制小數後幾位

>>>3.14

print(pow(2, 3))

>>> 8

print(pow(2, 3, 2))                    # 2的3次方 / 2 取余

>>> 0

print(sum([1, 2, 3, 4, 5]))
print(sum([1, 2, 3, 4, 5], 10))        # sum(iterator, star)    
>>> 15
25

-----------------------------------------------------------------------------------
print(min([1, 2, 3, 4, -5]))
print(min([1, 2, 3, 4, -5], key=abs))  # 還可以接收函數  

>>> -5
1

未完待續。。。

05python 的內置函數以及匿名函數(python函數)