所謂內置函數,就是無需import,就能夠直接使用的函數html
序號 | 函數 | 做用 | 示例 |
---|---|---|---|
1 | abs() | 求絕對值 | >>> abs(-1) 1 |
2 | round() | 將小數四捨五入,默認保留0位小數 | >>> round(1.4) 1 >>> round(1.55, 1) 1.6 |
3 | pow() | 指數求冪, 等價於冪運算符: x**y | >>> pow(2, 3) 8 |
4 | min() | 求可迭代對象的最小值, 傳入字典,須要提供key參數,返回最小值對應鍵 | >>> min([2, 1, 3]) 1 >>> d={'a': 2, 'b':1}; min(d, key=d.get) b |
5 | max() | 求可迭代對象的最大值, 傳入字典,須要提供key參數,返回最大值對應鍵 | >>> max([2, 1, 3]) 3 >>> d={'a': 2, 'b':1}; max(d, key=d.get) a |
6 | sum() | 求可迭代對象的和, 可提供初始累加值,默認爲0 | >>> sum([1, 2, 3]) 6 >>> sum([1, 2, 3], 1) 7 |
7 | divmod() | 返回一對商和餘數 | >>> divmod(5, 2) (2, 1) |
序號 | 函數 | 做用 | 示例 |
---|---|---|---|
1 | len() | 返回元素個數 | >>> len([1, 2]) 2 |
2 | sorted() | 排序 | >>> sorted([2, 1]) [1, 2] >>> sorted([{'a': 2, 'b': '1'}, {'a': 1, 'b': 2}], key=lambda x: x['a']) [{'a': 1, 'b': 2}, {'a': 2, 'b': '1'}] >>> d = {'a':2, 'b':1}; sorted(d, key=d.get, reverse=True) ['a', 'b'] |
3 | reversed() | 反向, 返回iterator對象 | >>> list(reversed([2, 1])) [1, 2] |
4 | slice() | 返回slice(切片)對象, 等價於[start:stop:step] | >>> [1, 2, 3][slice(1, 2)] [2] |
5 | next() | 調用__next__() 方法獲取下一個元素 | >>> next(iter([1,2,3])) 1 |
6 | all() | 全部元素爲真,返回真 | >>> all([True, True]) True |
7 | any() | 任一元素爲真,返回真 | >>> any([True, False]) True |
8 | filter() | 返回爲真的元素 | >>> [i for i in filter(None, [True, False])] [True] >>> list(filter(lambda x: x['s'], [{'s': False}, {'s': None}, {'s': True}])) [{'s': True}] |
9 | map() | 對元素進行處理,返回新的迭代器 | >>> list(map(lambda x: round(x), [1.4, 1.5])) [1, 2] |
序號 | 函數 | 做用 | 示例 |
---|---|---|---|
1 | open() | 打開文件 | >>> with open('1.txt', 'w') as f: ... f.write('hello world!') 12 >>> with open('1.txt', 'r') as f: ... f.read() 'hello world!' |
2 | input() | 等待獲取用戶輸入 | >>> input('are you ok?\n') are you ok? fuck 'fuck' |
3 | print() | 打印到標準輸出 | >>> print('hello world!') hello world! |
4 | locals() | 更新並返回表示當前本地符號表的字典 | >>> locals() |
5 | globals() | 返回表示當前全局符號表的字典 | >>> globals() |
6 | vars() | 返回對象的__dict__屬性;不帶參數, 等價locals |
>>> class Klass: ... __dict__ = ['a', 'b'] >>> vars(Klass()) ['a', 'b'] |
序號 | 函數 | 做用 | 示例 |
---|---|---|---|
1 | type() | 返回對象類型, 等效於object.__class__ | >>> type({'a': 1}) <class 'dict'> |
2 | isinstance() | 對象是不是類的實例 | >>> k = Klass(); isinstance(k, Klass) True |
3 | issubclass() | 類是不是父類的子類 | >>> class SubKlass(Klass): ... pass >>> issubclass(SubKlass, Klass) True |
4 | classmethod() | decorator把方法封裝成類方法 | >>> class C: ... @classmethod ... def f(cls): ... pass |
5 | staticmethod() | decorator將方法轉換爲靜態方法 | >>> class C: ... @staticmethod ... def f(cls): ... pass |
6 | super() | 返回一個代理對象,將方法調用 委託給父類或兄弟類。瞭解super設計協做類 |
>>> class Animal: ... def eat(self): print('wa wa ...') >>> class Dog(Animal): ... def eat(self): ... super().eat() ... print('give me meat.') >>> Dog().eat() wa wa ... give me meat. |
7 | hasattr() | 對象是否具備屬性 | >>> dog = Dog(); hasattr(dog, 'name') False |
8 | setattr() | 給對象添加新屬性 | >>> setattr(dog, 'name', "旺財") |
9 | getattr() | 獲取對象屬性值 | >>> getattr(dog, 'name') '旺財' |
10 | dir() | 返回模塊或對象的有效屬性列表 | >>> dir(dog) [..., 'eat', 'name'] |
11 | delattr() | 刪除對象屬性 | >>> delattr(dog, 'name') |
12 | property() | 返回property屬性 | >>> class Klass: ... def __init__(self):self._x = 0 ... @property ... def x(self):return self._x >>> Klass().x 0 |
序號 | 函數 | 做用 | 示例 |
---|---|---|---|
1 | int() | 返回十進制整數, 默認傳入爲十進制 |
>>> int() 0 >>> int('f', 16) 15 >>> int('g', 17) 16 |
2 | float() | 返回小數 | >>> float() 0.0 >>> float('1e-2') 0.01 >>> float('-Infinity') -inf |
3 | str() | 將數字或對象轉換成字符串 瞭解更多 | >>> str(1.5) '1.5' >>> str([1, 2, 3]) '[1, 2, 3]' |
4 | bool() | 返回一個布爾值 | >>> bool(0) False >>> bool(None) False >>> bool([]) False >>> bool({}) False >>> bool(1) True |
5 | tuple() | 返回一個元組,瞭解更多 | >>> tuple([1, 2, 3]) (1, 2, 3) >>> tuple({1, 2, 3}) (1, 2, 3) >>> tuple(range(1, 3)) (1, 2) |
6 | set() | 返回一個集合 | >>> set([1, 2, 3]) {1, 2, 3} >>> set((1, 2, 3)) {1, 2, 3} >>> set(range(1, 3)) {1, 2} |
7 | list() | 返回一個列表 | >>> list((1, 2, 3)) [1, 2, 3] >>> list({1, 2, 3}) [1, 2, 3] >>> list(range(1, 3)) [1, 2] |
8 | dict() | 返回一個字典 | >>> dict(one=1, two=2, three=3) {'one': 1, 'two': 2, 'three': 3} >>> dict([('two', 2), ('one', 1), ('three', 3)]) {'two': 2, 'one': 1, 'three': 3} >>> dict(zip(['one', 'two', 'three'], [1, 2, 3])) {'one': 1, 'two': 2, 'three': 3} |
9 | complex() | 返回一個複數 | >>> complex(1, 2) (1+2j) |
10 | bin() | 返回一個二進制字符串 | >>> bin(10) '0b1010' |
11 | oct() | 返回一個八進制字符串 | >>> oct(10) '0o12' |
12 | hex() | 返回一個十六進制字符串 | >>> hex(10) '0xa' |
13 | range() | 返回一個range對象 | >>> list(range(1, 9, 2)) [1, 3, 5, 7] |
14 | zip() | 建立一個聚合了來自每一個可迭代對象中的元素的迭代器 | >>> list(zip((1, 4, 7), [2, 5, 8], range(3, 10, 3))) [(1, 2, 3), (4, 5, 6), (7, 8, 9)] |
15 | enumerate() | 返回一個枚舉對象 | >>> list(enumerate(['a', 'b', 'c'])) [(0, 'a'), (1, 'b'), (2, 'c')] >>> list(enumerate(['a', 'b', 'c'], 1)) [(1, 'a'), (2, 'b'), (3, 'c')] |
16 | iter() | 返回一個迭代器對象 | >>> next(iter([1, 2, 3])) 1 |
17 | object() | 返回一個沒有特徵的新對象 | >>> dir(object()) ['__class__', ...] |
18 | bytearray() | 返回一個新的 bytes 數組 | >>> [i for i in bytearray('hello world!', encoding='utf8')] [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33] |
19 | hash() | 返回對象的哈希值 | >>> hash('hello') 116457751260220756 |
20 | id() | 返回對象的「標識值」 | >>> id('hello') 2590383091640 |
21 | chr() | 返回整數Unicode的字符串格式 | >>> chr(8364) '€' |
22 | ord() | Unicode字符轉整數 | >>> ord('€') 8364 |
23 | frozenset() | 返回一個新的frozenset對象 | >>> frozenset([1, 2, 3]) frozenset({1, 2, 3}) |
序號 | 函數 | 做用 | 示例 |
---|---|---|---|
1 | callable() | 是否可調用 | >>> callable(abs) True |
2 | compile() | 將source編譯成代碼或AST對象 | >>> compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1) |
3 | eval() | 對錶達式字符串解析求值 | >>> x = 1; eval('x+1') 2 |
4 | help() | 啓動內置的幫助系統(此函數主要在交互式中使用) | >>> help() |
5 | repr() | 返回對象的可打印表示形式的字符串 | >>> repr(Klass()) '<main.Klass object at 0x000001CF7A8D2CC0>' |
6 | memoryview() | 返回由給定實參建立的「內存視圖」對象 | >>> memoryview(bytearray([1, 2])) <memory at 0x000001CF7AA357C8> |
7 | format() | 返回格式化字符串 | >>> format(value[, format_spec]) |
8 | __import__() | 此函數會由import語句發起調用,平常 Python 編程中不須要用到的高級函數 | - |
序號 | 函數 | python2.7.16 | python3.7.4 |
---|---|---|---|
1 | abs() | yes | yes |
2 | all() | yes | yes |
3 | any() | yes | yes |
4 | ascii() | no | yes |
5 | basestring() | yes | no |
6 | bin() | yes | yes |
7 | bool() | yes | yes |
8 | breakpoint() | no | yes |
9 | bytearray() | yes | yes |
10 | bytes() | no | yes |
11 | callable() | yes | yes |
12 | chr() | yes | yes |
13 | classmethod() | yes | yes |
14 | cmp() | yes | no |
15 | compile() | yes | yes |
16 | complex() | yes | yes |
17 | delattr() | yes | yes |
18 | dict() | yes | yes |
19 | dir() | yes | yes |
20 | divmod() | yes | yes |
21 | enumerate() | yes | yes |
22 | eval() | yes | yes |
23 | exec() | no | yes |
24 | execfile() | yes | no |
25 | file() | yes | no |
26 | filter() | yes | yes |
27 | float() | yes | yes |
28 | format() | yes | yes |
29 | frozenset() | yes | yes |
30 | getattr() | yes | yes |
31 | globals() | yes | yes |
32 | hasattr() | yes | yes |
33 | hash() | yes | yes |
34 | help() | yes | yes |
35 | hex() | yes | yes |
36 | id() | yes | yes |
37 | input() | yes | yes |
38 | int() | yes | yes |
39 | isinstance() | yes | yes |
40 | issubclass() | yes | yes |
41 | iter() | yes | yes |
42 | len() | yes | yes |
43 | list() | yes | yes |
44 | locals() | yes | yes |
45 | long() | yes | no |
46 | map() | yes | yes |
47 | max() | yes | yes |
48 | memoryview() | yes | yes |
49 | min() | yes | yes |
50 | next() | yes | yes |
51 | object() | yes | yes |
52 | oct() | yes | yes |
53 | open() | yes | yes |
54 | ord() | yes | yes |
55 | pow() | yes | yes |
56 | print() | yes | yes |
57 | property() | yes | yes |
58 | range() | yes | yes |
59 | raw_input() | yes | no |
60 | reduce() | yes | no |
61 | reload() | yes | no |
62 | repr() | yes | yes |
63 | reversed() | yes | yes |
64 | round() | yes | yes |
65 | set() | yes | yes |
66 | setattr() | yes | yes |
67 | slice() | yes | yes |
68 | sorted() | yes | yes |
69 | staticmethod() | yes | yes |
70 | str() | yes | yes |
71 | sum() | yes | yes |
72 | super() | yes | yes |
73 | tuple() | yes | yes |
74 | type() | yes | yes |
75 | unichr() | yes | no |
76 | unicode() | yes | no |
77 | vars() | yes | yes |
78 | xrange() | yes | no |
79 | zip() | yes | yes |
80 | __import__() | yes | yes |
堅持寫專欄不易,若是以爲本文對你有幫助,記得點個贊。感謝支持!python
微信掃描二維碼 獲取最新技術原創git