Python小白必備的8個(gè)最常用的內(nèi)置函數(shù)
Python給我們內(nèi)置了大量功能函數(shù),官方文檔上列出了69個(gè),有些是我們是平時(shí)開發(fā)中經(jīng)常遇到的,也有一些函數(shù)很少被用到,這里列舉被開發(fā)者使用最頻繁的8個(gè)函數(shù)以及他們的詳細(xì)用法
print()
print函數(shù)是你學(xué)Python接觸到的***個(gè)函數(shù),它將對(duì)象輸出到標(biāo)準(zhǔn)輸出流,可將任意多個(gè)對(duì)象打印出來,函數(shù)的具體定義:
- print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
 
objects 是可變參數(shù),所以你可以同時(shí)將任意多個(gè)對(duì)象打印出來
- >>> print(1,2,3)
 - 1 2 3
 
默認(rèn)使用空格分隔每個(gè)對(duì)象,通過指定sep參數(shù)可以使用逗號(hào)分隔
- >>> print(1,2,3, sep=',')
 - 1,2,3
 
對(duì)象默認(rèn)輸出的是標(biāo)準(zhǔn)輸出流,你也可以將內(nèi)容保存到文件中
- >>> print(1,2,3, sep=',', file=open("hello.txt", "w"))
 
isinstance()
可以用 isinstance 函數(shù)判斷某個(gè)對(duì)象是否屬于某個(gè)類的實(shí)例,函數(shù)的定義
- isinstance(object, classinfo)
 
classinfo 既可以是單個(gè)類型對(duì)象,也可以是由多個(gè)類型對(duì)象組成的元組,只要object的類型是元組中任意一個(gè)就返回True,否則返回False
- >>> isinstance(1, (int, str))
 - True
 - >>> isinstance("", (int, str))
 - True
 - >>> isinstance([], dict)
 - False
 
range()
range函數(shù)是個(gè)工廠方法,用于構(gòu)造一個(gè)從[start, stop) (不包含stop)之間的連續(xù)的不可變的整數(shù)序列對(duì)象,這個(gè)序列功能上和列表非常類似,函數(shù)定義:
- range([start,] stop [, step]) -> range object
 
- start 可選參數(shù),序列的起點(diǎn),默認(rèn)是0
 - stop 必選參數(shù),序列的終點(diǎn)(不包含)
 - step 可選參數(shù),序列的步長(zhǎng),默認(rèn)是1,生成的元素規(guī)律是 r[i] = start + step*i
 
生成0~5的列表
- >>>
 - >>> range(5)
 - range(0, 5)
 - >>>
 - >>> list(range(5))
 - [0, 1, 2, 3, 4]
 - >>>
 
默認(rèn)從0開始,生成0到4之間的5個(gè)整數(shù),不包含5,step 默認(rèn)是1,每次都是在前一次加1
如果你想將某個(gè)操作重復(fù)執(zhí)行n遍,就可以使用for循環(huán)配置range函數(shù)實(shí)現(xiàn)
- >>> for i in range(3):
 - ... print("hello python")
 - ...
 - hello python
 - hello python
 - hello python
 
步長(zhǎng)為2
- >>> range(1, 10, 2)
 - range(1, 10, 2)
 - >>> list(range(1, 10, 2))
 - [1, 3, 5, 7, 9]
 
起點(diǎn)從1開始,終點(diǎn)10,步長(zhǎng)為2,每次都在前一個(gè)元素的基礎(chǔ)上加2,構(gòu)成1到10之間的奇數(shù)。
enumerate()
用于枚舉可迭代對(duì)象,同時(shí)還可以得到每次元素的下表索引值,函數(shù)定義:
- enumerate(iterable, start=0)
 
例如:
- >>> for index, value in enumerate("python"):
 - ... print(index, value)
 - ...
 - 0 p
 - 1 y
 - 2 t
 - 3 h
 - 4 o
 - 5 n
 
index 默認(rèn)從0開始,如果顯式指定參數(shù)start,下標(biāo)索引就從start開始
- >>> for index, value in enumerate("python", start=1):
 - ... print(index, value)
 - ...
 - 1 p
 - 2 y
 - 3 t
 - 4 h
 - 5 o
 - 6 n
 
如果不使用enumerate函數(shù),要獲取元素的下標(biāo)索引,則需要更多的代碼:
- def my_enumerate(sequence, start=0):
 - n = start
 - for e in sequence:
 - yield n, e
 - n += 1
 
- >>> for index, value in my_enumerate("python"):
 - print(index, value)
 - 0 p
 - 1 y
 - 2 t
 - 3 h
 - 4 o
 - 5 n
 
len
len 用于獲取容器對(duì)象中的元素個(gè)數(shù),例如判斷列表是否為空可以用 len 函數(shù)
- >>> len([1,2,3])
 - 3
 - >>> len("python")
 - 6
 - >>> if len([]) == 0:
 - pass
 
并不是所有對(duì)象都支持len操作的,例如:
- >>> len(True)
 - Traceback (most recent call last):
 - File "<stdin>", line 1, in <module>
 - TypeError: object of type 'bool' has no len()
 
除了序列對(duì)象和集合對(duì)象,自定義類必須實(shí)現(xiàn)了 __len__ 方法能作用在len函數(shù)上
reversed()
reversed() 反轉(zhuǎn)序列對(duì)象,你可以將字符串進(jìn)行反轉(zhuǎn),將列表進(jìn)行反轉(zhuǎn),將元組反轉(zhuǎn)
- >>> list(reversed([1,2,3]))
 - [3, 2, 1]
 
open()
open 函數(shù)用于構(gòu)造文件對(duì)象,構(gòu)建后可對(duì)其進(jìn)行內(nèi)容的讀寫操作
- open(file, mode='r', encoding=None)
 
讀操作
- # 從當(dāng)前路徑打開文件 test.txt, 默認(rèn)以讀的方式
 - >>>f = open("test.txt")
 - >>>f.read()
 - ...
 
有時(shí)還需要指定編碼格式,否則會(huì)遇到亂碼
- f = open("test.txt", encoding='utf8')
 
寫操作
- >>>f = open("hello.text", 'w', encoding='utf8')
 - >>>f.write("hello python"))
 
文件中存在內(nèi)容時(shí)原來的內(nèi)容將別覆蓋,如果不想被覆蓋,直接將新的內(nèi)容追加到文件末尾,可以使用 a 模式
- f = open("hello.text", 'a', encoding='utf8')
 - f.write("!!!")
 
sorted()
sroted 是對(duì)列表進(jìn)行重新排序,當(dāng)然其他可迭代對(duì)象都支持重新排放,返回一個(gè)新對(duì)象,原對(duì)象保持不變
- >>> sorted([1,4,2,1,0])
 - [0, 1, 1, 2, 4]
 
sorted 還有很多更高級(jí)的用法,可以參考我之前總結(jié)的一篇文章:史上最全關(guān)于sorted函數(shù)的10條總結(jié)
















 
 
 














 
 
 
 