python基礎-內置函數(1)
阿新 • • 發佈:2017-11-11
iter fun 最小 sig case -a nbsp rom style
python 提供了很多的內置函數。
一、數值處理相關函數:
1、取絕對值:abs()
def abs(*args, **kwargs): # real signature unknown """ Return the absolute value of the argument. """ passabs()
2、轉二進制:bin()
def bin(*args, **kwargs): # real signature unknown; NOTE: unreliably restored from __doc__ """ Return the binary representation of an integer. >>> bin(2796202) ‘0b1010101010101010101010‘"""
3、轉八進制:oct()
def oct(*args, **kwargs): # real signature unknown; NOTE: unreliably restored from __doc__ """ Return the octal representation of an integer. >>> oct(342391) ‘0o1234567‘ """
4、轉十六進制:hex()
def hex(*args, **kwargs): # real signature unknown; NOTE: unreliably restored from __doc__""" Return the hexadecimal representation of an integer. >>> hex(12648430) ‘0xc0ffee‘ """
5、取最大值:max()
def max(*args, key=None): # known special case of max """ max(iterable, *[, default=obj, key=func]) -> value max(arg1, arg2, *args, *[, key=func]) -> value With a single iterable argument, return its biggest item. The 使用一個可叠代的參數,返回其中最大值的項 default keyword-only argument specifies an object to return if the provided iterable is empty. With two or more arguments, return the largest argument."""
6、取最小值:min()
def min(*args, key=None): # known special case of min """ min(iterable, *[, default=obj, key=func]) -> value min(arg1, arg2, *args, *[, key=func]) -> value With a single iterable argument, return its smallest item. The default keyword-only argument specifies an object to return if the provided iterable is empty. With two or more arguments, return the smallest argument. """
二、返回是布爾值的函數:
1、所有元素為真,函數返回真:all()
def all(*args, **kwargs): # real signature unknown """ Return True if bool(x) is True for all values x in the iterable. If the iterable is empty, return True. """
2、有一個元素為真,函數返回真:any()
def any(*args, **kwargs): # real signature unknown """ Return True if bool(x) is True for any x in the iterable. If the iterable is empty, return False. """
3、返回對象是否可調用:callable()
def callable(i_e_, some_kind_of_function): # real signature unknown; restored from __doc__ """ Return whether the object is callable (i.e., some kind of function). Note that classes are callable, as are instances of classes with a __call__() method. """
三、編譯和執行代碼
1、把字符串編譯成python代碼:compile()
s="print(123)" r=compile(s,‘<string>‘,"exec") exec(r)
2、執行代碼:exec() 和 eval()
兩者區別:a、exec()可以執行python 代碼,也可以執行表達式,而eval() 只能執行表達式
b、exec()只是執行,沒有返回值,而eval() 有返回值
s="print(123)" r=compile(s,‘<string>‘,"exec") exec(r) exec("print(‘10*10‘)") r=eval("8*8") print(r)
python基礎-內置函數(1)