Python入門及進階筆記 Python 內(nèi)置函數(shù)小結(jié)
內(nèi)置函數(shù)
常用函數(shù)
1.數(shù)學(xué)相關(guān)
•abs(x)
abs()返回一個數(shù)字的絕對值。如果給出復(fù)數(shù),返回值就是該復(fù)數(shù)的模。
>>>print abs(-100)
100
>>>print abs(1+2j)
2.2360679775
•divmod(x,y)
divmod(x,y)函數(shù)完成除法運算,返回商和余數(shù)。
>>> divmod(10,3)
(3, 1)
>>> divmod(9,3) (3, 0)
•pow(x,y[,z])
pow()函數(shù)返回以x為底,y為指數(shù)的冪。如果給出z值,該函數(shù)就計算x的y次冪值被z取模的值。
>>> print pow(2,4)
16
>>> print pow(2,4,2)
0
>>> print pow(2.4,3)
13.824
•round(x[,n])
round()函數(shù)返回浮點數(shù)x的四舍五入值,如給出n值,則代表舍入到小數(shù)點后的位數(shù)。
>>> round(3.333)
3.0
>>> round(3)
3.0
>>> round(5.9)
6.0
•min(x[,y,z...])
min()函數(shù)返回給定參數(shù)的最小值,參數(shù)可以為序列。
>>> min(1,2,3,4)
1
>>> min((1,2,3),(2,3,4))
(1, 2, 3)
•max(x[,y,z...])
max()函數(shù)返回給定參數(shù)的最大值,參數(shù)可以為序列。
>>> max(1,2,3,4)
4
>>> max((1,2,3),(2,3,4))
(2, 3, 4)
2.序列相關(guān)
•len(object) -> integer
len()函數(shù)返回字符串和序列的長度。
>>> len("aa")
2
>>> len([1,2])
2
•range([lower,]stop[,step])
range()函數(shù)可按參數(shù)生成連續(xù)的有序整數(shù)列表。
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(1,10)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(1,10,2)
[1, 3, 5, 7, 9]
•xrange([lower,]stop[,step])
xrange()函數(shù)與range()類似,但xrnage()并不創(chuàng)建列表,而是返回一個xrange對象,它的行為
與列表相似,但是只在需要時才計算列表值,當(dāng)列表很大時,這個特性能為我們節(jié)省內(nèi)存。
>>> a=xrange(10)
>>> print a[0]
0
>>> print a[1]
1
>>> print a[2]
2
3.對象及類型
•callable(object)
callable()函數(shù)用于測試對象是否可調(diào)用,如果可以則返回1(真);否則返回0(假)??烧{(diào)用對象包括函數(shù)、方法、代碼對象、類和已經(jīng)定義了 調(diào)用 方法的類實例。
>>> a="123"
>>> print callable(a)
False
>>> print callable(chr)
True
•cmp(x,y)
cmp()函數(shù)比較x和y兩個對象,并根據(jù)比較結(jié)果返回一個整數(shù),如果x<y,則返回-1;如果x>y,則返回1,如果x==y則返回0。
>>>a=1
>>>b=2
>>>c=2
>>> print cmp(a,b)
-1
>>> print cmp(b,a)
1
>>> print cmp(b,c)
0
•isinstance(object,class-or-type-or-tuple) -> bool
測試對象類型
>>> a='isinstance test'
>>> b=1234
>>> isinstance(a,str)
True
>>>isinstance(a,int)
False
>>> isinstance(b,str)
False
>>> isinstance(b,int) True
•type(obj)
type()函數(shù)可返回對象的數(shù)據(jù)類型。
>>> type(a)
<type 'list'>
>>> type(copy)
<type 'module'>
>>> type(1)
<type 'int'>
內(nèi)置類型轉(zhuǎn)換函數(shù)
1.字符及字符串
•chr(i)
chr()函數(shù)返回ASCII碼對應(yīng)的字符串。
>>> print chr(65)
A
>>> print chr(66)
B
>>> print chr(65)+chr(66)
AB
•ord(x)
ord()函數(shù)返回一個字符串參數(shù)的ASCII碼或Unicode值。
>>> ord("a")
97
>>> ord(u"a")
97
•str(obj)
str()函數(shù)把對象轉(zhuǎn)換成可打印字符串。
>>> str("4")
'4'
>>> str(4)
'4'
>>> str(3+2j)
'(3+2j)'
2.進制轉(zhuǎn)換
•int(x[,base])
int()函數(shù)把數(shù)字和字符串轉(zhuǎn)換成一個整數(shù),base為可選的基數(shù)。
>>> int(3.3)
3
>>> int(3L)
3
>>> int("13")
13
>>> int("14",15)
19
•long(x[,base])
long()函數(shù)把數(shù)字和字符串轉(zhuǎn)換成長整數(shù),base為可選的基數(shù)。
>>> long("123")
123L
>>> long(11)
11L
•float(x)
float()函數(shù)把一個數(shù)字或字符串轉(zhuǎn)換成浮點數(shù)。
>>> float("12")
12.0
>>> float(12L)
12.0
>>> float(12.2)
12.199999999999999
•hex(x)
hex()函數(shù)可把整數(shù)轉(zhuǎn)換成十六進制數(shù)。
>>> hex(16)
'0x10'
>>> hex(123)
'0x7b'
•oct(x)
oct()函數(shù)可把給出的整數(shù)轉(zhuǎn)換成八進制數(shù)。
>>> oct(8)
'010'
>>> oct(123)
'0173'
•complex(real[,imaginary])
complex()函數(shù)可把字符串或數(shù)字轉(zhuǎn)換為復(fù)數(shù)。
>>> complex("2+1j")
(2+1j)
>>> complex("2")
(2+0j)
>>> complex(2,1)
(2+1j)
>>> complex(2L,1)
(2+1j)
3.數(shù)據(jù)結(jié)構(gòu)
•tuple(x)
tuple()函數(shù)把序列對象轉(zhuǎn)換成tuple。
>>> tuple("hello world")
('h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd')
>>> tuple([1,2,3,4])
(1, 2, 3, 4)
•list(x)
list()函數(shù)可將序列對象轉(zhuǎn)換成列表。如:
>>> list("hello world")
['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
>>> list((1,2,3,4))
[1, 2, 3, 4]
序列處理函數(shù)
常用函數(shù)中的len()、max()和min()同樣可用于序列。
•filter(function,list)
調(diào)用filter()時,它會把一個函數(shù)應(yīng)用于序列中的每個項,并返回該函數(shù)返回真值時的所有項,從而過濾掉返回假值的所有項。
>>> def nobad(s):
... return s.find("bad") == -1
...
>>> s = ["bad","good","bade","we"]
>>> filter(nobad,s)
['good', 'we']
•map(function,list[,list])
map()函數(shù)把一個函數(shù)應(yīng)用于序列中所有項,并返回一個列表。
>>> import string
>>> s=["python","zope","linux"]
>>> map(string.capitalize,s)
['Python', 'Zope', 'Linux']
map()還可同時應(yīng)用于多個列表。如:
>>> import operator
>>> s=[1,2,3]; t=[3,2,1]
>>> map(operator.mul,s,t) # s[i]*t[j]
[3, 4, 3]
如果傳遞一個None值,而不是一個函數(shù),則map()會把每個序列中的相應(yīng)元素合并起來,并返回該元組。如:
>>> a=[1,2];b=[3,4];c=[5,6]
>>> map(None,a,b,c)
[(1, 3, 5), (2, 4, 6)]
•reduce(function,seq[,init])
reduce()函數(shù)獲得序列中前兩個項,并把它傳遞給提供的函數(shù),獲得結(jié)果后再取序列中的下一項,連同結(jié)果再傳遞給函數(shù),以此類推,直到處理完所有項為止。
[code]
>>> import operator
>>> reduce(operator.mul,[2,3,4,5]) # ((2*3)*4)*5
120
>>> reduce(operator.mul,[2,3,4,5],1) # (((1*2)*3)*4)*5
120
>>> reduce(operator.mul,[2,3,4,5],2) # (((2*2)*3)*4)*5
240
[code]
wklken
Email: wklken@yeah.net
- python進階教程之函數(shù)參數(shù)的多種傳遞方法
- python進階教程之函數(shù)對象(函數(shù)也是對象)
- python進階教程之循環(huán)相關(guān)函數(shù)range、enumerate、zip
- Python進階之遞歸函數(shù)的用法及其示例
- Python進階-函數(shù)默認參數(shù)(詳解)
- python中的內(nèi)置函數(shù)max()和min()及mas()函數(shù)的高級用法
- python中l(wèi)ist列表的高級函數(shù)
- Python sorted函數(shù)詳解(高級篇)
- Python中的高級函數(shù)map/reduce使用實例
- python高級特性和高階函數(shù)及使用詳解
- Python高級特性與幾種函數(shù)的講解
- Python基礎(chǔ)之函數(shù)基本用法與進階詳解
相關(guān)文章
Python3正則表達式之:(?(id/name)yes-pattern|no-pattern)條件性匹配
(?(id/name)yes-pattern|no-pattern)的作用是對于給出的id或者name,先嘗試去匹配 yes-pattern部分的內(nèi)容,如果id或name條件不滿足,則去匹配no-pattern部分的內(nèi)容2021-10-10Python如何提取csv數(shù)據(jù)并篩選指定條件數(shù)據(jù)詳解
在學(xué)習(xí)python過程中常遇到一種情況,要讀取.csv文件的數(shù)據(jù),然后取出其中某個字段,下面這篇文章主要給大家介紹了關(guān)于Python如何提取csv數(shù)據(jù)并篩選指定條件數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下2022-08-08Python?SQLAlchemy與數(shù)據(jù)庫交互操作完整指南
SQLAlchemy 是一個強大的 Python 庫,用于數(shù)據(jù)庫操作,無論是簡單的數(shù)據(jù)存儲還是復(fù)雜的數(shù)據(jù)管理,SQLAlchemy 都提供了多種方法來處理數(shù)據(jù)庫,本文將全面介紹 SQLAlchemy的基本用法以及各種操作的示例代碼2024-01-01django的模型類管理器——數(shù)據(jù)庫操作的封裝詳解
這篇文章主要介紹了django的模型類管理器——數(shù)據(jù)庫操作的封裝詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04python 實現(xiàn)存儲數(shù)據(jù)到txt和pdf文檔及亂碼問題的解決
這篇文章主要介紹了python 實現(xiàn)存儲數(shù)據(jù)到txt和pdf文檔及亂碼問題的解決,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03pycharm的debug調(diào)試以及異常,Python中錯誤的處理過程
這篇文章主要介紹了pycharm的debug調(diào)試以及異常,Python中錯誤的處理過程,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01