當前位置:成語大全網 - 新華字典 - python3--內置函數

python3--內置函數

python的常用內置函數

1.abs() 函數返回數字的絕對值

abs(-40)=40

2. dict() 函數用於創建壹個字典

>>dict()

>>{} ?#創建壹個空字典類似於u={},字典的存取方式壹般為key->value

例如u = {"username":"tom", ?"age":18}

3. help() 函數用於查看函數或模塊用途的詳細說明

>>help('math')查看math模塊的用處

>>a=[1,2,3,4]

>>help(a)查看列表list幫助信息

4.dir()獲得當前模塊的屬性列表

>> dir(help)

['__call__', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']

>>>

5.min() 方法返回給定參數的最小值 /參數可以為序列

>>> a=? min(10,20,30,40)

>>> a

10

6. next() 返回叠代器的下壹個項目

>>> it = iter([1, 2, 3, 4, 5])

>>> next(it)

1

>>>

>>> next(it)

2

>>>

7. id() 函數用於獲取對象的內存地址

>>> a=12

>>> id(a)

1550569552

8.enumerate() 函數用於將壹個可遍歷的數據對象(如列表、元組或字符串)組合為壹個索引序列,同時列出數據和數據下標,壹般用在 for 循環當中。

>>> a=["tom","marry","leblan"]

>>> list(enumerate(a))

[(0, 'tom'), (1, 'marry'), (2, 'leblan')]

>>>

9. oct() 函數將壹個整數轉換成8進制字符串

>>> oct(15)

'0o17'

>>> oct(10)

'0o12'

>>>

10. bin() 返回壹個整數 int 或者長整數 long int 的二進制表示

>>> bin(10)

'0b1010'

>>> bin(15)

'0b1111'

>>>

11.eval() 函數用來執行壹個字符串表達式,並返回表達式的值

>>> eval('2+2')

4

12.int() 函數用於將壹個字符串會數字轉換為整型

>>> int(3)

3

>>> int(3.6)

3

>>> int(3.9)

3

>>> int(4.0)

4

>>>

13.open() 函數用於打開壹個文件,創建壹個file對象,相關的方法才可以調用它進行讀寫

>>>f=open('test.txt')

14.str() 函數將對象轉化為適於人閱讀的形式

>>> str(3)

'3'

>>>

15. bool() 函數用於將給定參數轉換為布爾類型,如果沒有參數,返回 False

>>> bool()

False

>>> bool(1)

True

>>> bool(10)

True

>>> bool(10.0)

True

16.isinstance() 函數來判斷壹個對象是否是壹個已知的類型

>>> a=5

>>> isinstance(a,int)

True

>>> isinstance(a,str)

False

>>>

17. sum() 方法對系列進行求和計算

>>> sum([1,2,3],5)

11

>>> sum([1,2,3])

6

>>>

18. super() 函數用於調用下壹個父類(超類)並返回該父類實例的方法。super 是用來解決多重繼承問題的,直接用類名調用父類方法

class ? User(object):

? def__init__(self):

class Persons(User):

super(Persons,self).__init__()

19. float() 函數用於將整數和字符串轉換成浮點數

>>> float(1)

1.0

>>> float(10)

10.0

>>>

20. iter() 函數用來生成叠代器

>>> a=[1,2,3,4,5,6]

>>> iter(a)

>>> for i in iter(a):

... print(i)

...

1

2

3

4

5

6

>>>

21.tuple 函數將列表轉換為元組

>>> a=[1,2,3,4,5,6]

>>> tuple(a)

(1, 2, 3, 4, 5, 6)

>>>

22.len() 方法返回對象(字符、列表、元組等)長度或項目個數

>>> s = "playbasketball"

>>> len(s)

14

>>>a=[1,2,3,4,5,6]

>>> len(a)

6

>>>

23. property() 函數的作用是在新式類中返回屬性值

class User(object):

?def __init__(self,name):

? self.name = name

def get_name(self):

? return self.get_name

@property

?def name(self):

?return self_name

24.type() 函數返回對象的類型

25.list() 方法用於將元組轉換為列表

>>> b=(1,2,3,4,5,6)

>>> list(b)

[1, 2, 3, 4, 5, 6]

>>>

26.range() 函數可創建壹個整數列表,壹般用在 for 循環中

>>> range(10)

range(0, 10)

>>> range(10,20)

range(10, 20)

>>>

27. getattr() 函數用於返回壹個對象屬性值

>>> class w(object):

... s=5

...

>>> a = w()

>>> getattr(a,'s')

5

>>>

28. complex() 函數用於創建壹個復數或者轉化壹個字符串或數為復數。如果第壹個參數為字符串,則不需要指定第二個參數

>>> complex(1,2)

(1+2j)

>>> complex(1)

(1+0j)

>>> complex("1")

(1+0j)

>>>

29.max() 方法返回給定參數的最大值,參數可以為序列

>>> b=(1,2,3,4,5,6)

>>> max(b)

6

>>>

30. round() 方法返回浮點數x的四舍五入值

>>> round(10.56)

11

>>> round(10.45)

10

>>> round(10.45,1)

10.4

>>> round(10.56,1)

10.6

>>> round(10.565,2)

10.56

>>>

31. delattr 函數用於刪除屬性

>>> class Num(object):

... a=1

... b=2

... c=3.

..>>> print1 = Num()

>>> print('a=',print1.a)

a= 1

>>> print('b=',print1.b)

b= 2

>>> print('c=',print1.c)

c= 3

>>> delattr(Num,'b')

>>> print('b=',print1.b)

Traceback (most recent call last):? File "", line 1, inAttributeError: 'Num' object has no attribute 'b'

>>>

32. hash() 用於獲取取壹個對象(字符串或者數值等)的哈希值

>>> hash(2)

2

>>> hash("tom")

-1675102375494872622

33. set() 函數創建壹個無序不重復元素集,可進行關系測試,刪除重復數據,還可以計算交集、差集、並集等。

>>> a= set("tom")

>>> b = set("marrt")

>>> a,b

({'t', 'm', 'o'}, {'m', 't', 'a', 'r'})

>>> a&b#交集

{'t', 'm'}

>>> a|b#並集

{'t', 'm', 'r', 'o', 'a'}

>>> a-b#差集

{'o'}

>>>