日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python一共有多少个内置函数_Python 35个内置函数,你都ok吗?

發布時間:2024/4/13 python 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python一共有多少个内置函数_Python 35个内置函数,你都ok吗? 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Python一共有60多個內置函數,今天先梳理其中35 個

1 abs()

絕對值或復數的模

In [1]: abs(-6)

Out[1]: 6

2 all()

接受一個迭代器,如果迭代器的所有元素都為真,那么返回True,否則返回False

In [2]: all([1,0,3,6])

Out[2]: False

In [3]: all([1,2,3])

Out[3]: True

3 any()

接受一個迭代器,如果迭代器里有一個元素為真,那么返回True,否則返回False

In [4]: any([0,0,0,[]])

Out[4]: False

In [5]: any([0,0,1])

Out[5]: True

4 ascii()

調用對象的repr() 方法,獲得該方法的返回值

In [30]: class Student():

...: def __init__(self,id,name):

...: self.id = id

...: self.name = name

...: def __repr__(self):

...: return 'id = '+self.id +', name = '+self.name

In [33]: print(xiaoming)

id = 001, name = xiaoming

In [34]: ascii(xiaoming)

Out[34]: 'id = 001, name = xiaoming'

5 bin()

將十進制轉換為二進制

In [35]: bin(10)

Out[35]: '0b1010'

6 oct()

將十進制轉換為八進制

In [36]: oct(9)

Out[36]: '0o11'

7 hex()

將十進制轉換為十六進制

In [37]: hex(15)

Out[37]: '0xf'

8 bool()

測試一個對象是True, 還是False.

In [38]: bool([0,0,0])

Out[38]: True

In [39]: bool([])

Out[39]: False

In [40]: bool([1,0,1])

Out[40]: True

9 bytes()

將一個字符串轉換成字節類型

In [44]: s = "apple"

In [45]: bytes(s,encoding='utf-8')

Out[45]: b'apple'

10 str()

將字符類型、數值類型等轉換為字符串類型

In [46]: integ = 100

In [47]: str(integ)

Out[47]: '100'

11 callable()

判斷對象是否可以被調用,能被調用的對象就是一個callable 對象,比如函數 str, int 等都是可被調用的,但是例子4 中xiaoming這個實例是不可被調用的:

In [48]: callable(str)

Out[48]: True

In [49]: callable(int)

Out[49]: True

In [50]: xiaoming

Out[50]: id = 001, name = xiaoming

In [51]: callable(xiaoming)

Out[51]: False

12 chr()

查看十進制整數對應的ASCII字符

In [54]: chr(65)

Out[54]: 'A'

13 ord()

查看某個ascii對應的十進制數

In [60]: ord('A')

Out[60]: 65

14 classmethod()

classmethod 修飾符對應的函數不需要實例化,不需要 self 參數,但第一個參數需要是表示自身類的 cls 參數,可以來調用類的屬性,類的方法,實例化對象等。

In [66]: class Student():

...: def __init__(self,id,name):

...: self.id = id

...: self.name = name

...: def __repr__(self):

...: return 'id = '+self.id +', name = '+self.name

...: @classmethod

...: def f(cls):

...: print(cls)

15 complie()

將字符串編譯成python 能識別或可以執行的代碼,也可以將文字讀成字符串再編譯。

In [74]: s = "print('helloworld')"

In [75]: r = compile(s,"", "exec")

In [76]: r

Out[76]: at 0x0000000005DE75D0, file "", line 1>

In [77]: exec(r)

helloworld

16 complex()

創建一個復數

In [81]: complex(1,2)

Out[81]: (1+2j)

17 delattr()

刪除對象的屬性

In [87]: delattr(xiaoming,'id')

In [88]: hasattr(xiaoming,'id')

Out[88]: False

18 dict()

創建數據字典

In [92]: dict()

Out[92]: {}

In [93]: dict(a='a',b='b')

Out[93]: {'a': 'a', 'b': 'b'}

In [94]: dict(zip(['a','b'],[1,2]))

Out[94]: {'a': 1, 'b': 2}

In [95]: dict([('a',1),('b',2)])

Out[95]: {'a': 1, 'b': 2}

19 dir()

不帶參數時返回當前范圍內的變量,方法和定義的類型列表;帶參數時返回參數的屬性,方法列表。

In [96]: dir(xiaoming)

Out[96]:

['__class__',

'__delattr__',

'__dict__',

'__dir__',

'__doc__',

'__eq__',

'__format__',

'__ge__',

'__getattribute__',

'__gt__',

'__hash__',

'__init__',

'__init_subclass__',

'__le__',

'__lt__',

'__module__',

'__ne__',

'__new__',

'__reduce__',

'__reduce_ex__',

'__repr__',

'__setattr__',

'__sizeof__',

'__str__',

'__subclasshook__',

'__weakref__',

'name']

20 divmod()

分別取商和余數

In [97]: divmod(10,3)

Out[97]: (3, 1)

21 enumerate()

返回一個可以枚舉的對象,該對象的next()方法將返回一個元組。

In [98]: s = ["a","b","c"]

...: for i ,v in enumerate(s,1):

...: print(i,v)

...:

1 a

2 b

3 c

22 eval()

將字符串str 當成有效的表達式來求值并返回計算結果;取出字符串中內容

In [99]: s = "1 + 3 +5"

...: eval(s)

...:

Out[99]: 9

23 exec()

執行字符串或complie方法編譯過的字符串,沒有返回值

In [74]: s = "print('helloworld')"

In [75]: r = compile(s,"", "exec")

In [76]: r

Out[76]: at 0x0000000005DE75D0, file "", line 1>

In [77]: exec(r)

helloworld

24 filter()

過濾器,構造一個序列,等價于

[ item for item in iterables if function(item)]

在函數中設定過濾條件,逐一循環迭代器中的元素,將返回值為True時的元素留下,形成一個filter類型數據。

In [101]: fil = filter(lambda x: x>10,[1,11,2,45,7,6,13])

In [102]: list(fil)

Out[102]: [11, 45, 13]

25 float()

將一個字符串或整數轉換為浮點數

In [103]: float(3)

Out[103]: 3.0

26 format()

格式化輸出字符串,format(value, format_spec)實質上是調用了value的format(format_spec)方法。

In [104]: print("i am {0},age{1}".format("tom",18))

i am tom,age18

27 frozenset()

創建一個不可修改的集合。

In [105]: frozenset([1,1,3,2,3])

Out[105]: frozenset({1, 2, 3})

28 getattr()

獲取對象的屬性

In [106]: getattr(xiaoming,'name')

Out[106]: 'xiaoming'

29 globals()

返回一個描述當前全局變量的字典

30 hasattr()

In [110]: hasattr(xiaoming,'name')

Out[110]: True

In [111]: hasattr(xiaoming,'id')

Out[111]: False

31 hash()

返回對象的哈希值

In [112]: hash(xiaoming)

Out[112]: 6139638

32 help()

返回對象的幫助文檔

In [113]: help(xiaoming)

Help on Student in module __main__ object:

class Student(builtins.object)

| Methods defined here:

|

| __init__(self, id, name)

|

| __repr__(self)

|

| ----------------------------------------------------------------------

| Data descriptors defined here:

|

| __dict__

| dictionary for instance variables (if defined)

|

| __weakref__

| list of weak references to the object (if defined)

33 id()

返回對象的內存地址

In [115]: id(xiaoming)

Out[115]: 98234208

34 input()

獲取用戶輸入內容

In [116]: input()

aa

Out[116]: 'aa'

35 int()

int(x, base =10) , x可能為字符串或數值,將x 轉換為一個普通整數。如果參數是字符串,那么它可能包含符號和小數點。如果超出了普通整數的表示范圍,一個長整數被返回。

In [120]: int('12',16)

Out[120]: 18

總結

以上是生活随笔為你收集整理的python一共有多少个内置函数_Python 35个内置函数,你都ok吗?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。