python3--內置函數(shù)

python的常用內置函數(shù)

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

abs(-40)=40

2.dict()函數(shù)用于創(chuàng)建一個字典

>>dict()

>>{} ? ? ?#創(chuàng)建一個空字典類似于u={},字典的存取方式一般為key->value

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

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

>>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() 方法返回給定參數(shù)的最小值 /參數(shù)可以為序列


>>> 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()函數(shù)用于獲取對象的內存地址


>>> a=12

>>> id(a)

1550569552

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

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

>>> list(enumerate(a))

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

>>>

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

>>> oct(15)

'0o17'

>>> oct(10)

'0o12'

>>>

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

>>> bin(10)

'0b1010'

>>> bin(15)

'0b1111'

>>>

11.eval() 函數(shù)用來執(zhí)行一個字符串表達式,并返回表達式的值

>>> eval('2+2')

4

12.int() 函數(shù)用于將一個字符串會數(shù)字轉換為整型

>>> int(3)

3

>>> int(3.6)

3

>>> int(3.9)

3

>>> int(4.0)

4

>>>

13.open() 函數(shù)用于打開一個文件,創(chuàng)建一個file對象,相關的方法才可以調用它進行讀寫

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

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

>>> str(3)

'3'

>>>

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

>>> bool()

False

>>> bool(1)

True

>>> bool(10)

True

>>> bool(10.0)

True

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

>>> 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()函數(shù)用于調用下一個父類(超類)并返回該父類實例的方法。super 是用來解決多重繼承問題的,直接用類名調用父類方法

class ? User(object):

? ? ? def__init__(self):

class Persons(User):

? ? ? ? ? ? super(Persons,self).__init__()

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

>>> float(1)

1.0

>>> float(10)

10.0

>>>

20.iter()函數(shù)用來生成迭代器

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

>>> iter(a)

>>> for i in iter(a):

... ? ? ? ? print(i)

...

1

2

3

4

5

6

>>>

21.tuple 函數(shù)將列表轉換為元組

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

>>> tuple(a)

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

>>>

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

>>> s = "playbasketball"

>>> len(s)

14

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

>>> len(a)

6

>>>

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

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() 函數(shù)返回對象的類型


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

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

>>> list(b)

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

>>>

26.range() 函數(shù)可創(chuàng)建一個整數(shù)列表,一般用在 for 循環(huán)中

>>> range(10)

range(0, 10)

>>> range(10,20)

range(10, 20)

>>>

27.getattr()函數(shù)用于返回一個對象屬性值

>>> class w(object):

... ? ? ? ? ? ? s=5

...

>>> a = w()

>>> getattr(a,'s')

5

>>>

28.complex()函數(shù)用于創(chuàng)建一個復數(shù)或者轉化一個字符串或數(shù)為復數(shù)。如果第一個參數(shù)為字符串,則不需要指定第二個參數(shù)

>>> complex(1,2)

(1+2j)

>>> complex(1)

(1+0j)

>>> complex("1")

(1+0j)

>>>

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

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

>>> max(b)

6

>>>

30.round()方法返回浮點數(shù)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函數(shù)用于刪除屬性

>>> 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()用于獲取取一個對象(字符串或者數(shù)值等)的哈希值

>>> hash(2)

2

>>> hash("tom")

-1675102375494872622

33.set()函數(shù)創(chuàng)建一個無序不重復元素集,可進行關系測試,刪除重復數(shù)據(jù),還可以計算交集、差集、并集等。

>>> 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'}

>>>

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容