我們可以很容易的通過Python解釋器獲取幫助。如果想知道一個對象(object)更多的信息,那么可以調(diào)用help(object)!另外還有一些有用的方法,dir(object)會顯示該對象的大部分相關(guān)屬性名,還有object._?doc?_會顯示其相對應(yīng)的文檔字符串。下面對其進(jìn)行逐一介紹。
創(chuàng)新互聯(lián)建站主營云陽網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,成都app開發(fā),云陽h5小程序定制開發(fā)搭建,云陽網(wǎng)站營銷推廣歡迎云陽等地區(qū)企業(yè)咨詢
1、 help()
help函數(shù)是Python的一個內(nèi)置函數(shù)。?
函數(shù)原型:help([object])。?
可以幫助我們了解該對象的更多信息。?
If?no argument is given, the interactive help system starts on the interpreter console.
help()
Welcome to Python 2.7! ?This is the online help utility.
If this is your first time using Python, you should definitely check out
the tutorial on the Internet at .
Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules. ?To quit this help utility andreturn to the interpreter, just type "quit".
To get a list of available modules, keywords, or topics, type "modules","keywords", or "topics". ?Each module also comes with a one-line summary
of what it does; to list the modules whose summaries contain a given word
such as "spam", type "modules spam".
help int ?# 由于篇幅問題,此處只顯示部分內(nèi)容,下同Help on class int in module __builtin__:class int(object)
| ?int(x=0) - int or long
| ?int(x, base=10) - int or long
| ?
.....help
12345678910111213141516171819202122232425262728
If?the argument is a string, then the string is looked up as the name of a?module,?function,?class,?method,?keyword, or?documentation topic, and a help page is printed on the console. If the argument is any other kind of object, a help page on the object is generated.
help(abs) ?# 查看abs函數(shù)Help on built-in function abs in module __builtin__:
abs(...)
abs(number) - number
Return the absolute value of the argument. help(math) # 查看math模塊,此處只顯示部分內(nèi)容Help on built-in module math:
NAME
math
FILE
(built-in)
DESCRIPTION
This module is always available. ?It provides access to the
mathematical functions defined by the C standard.
FUNCTIONS
acos(...)
acos(x)
Return the arc cosine (measured in radians) of x.
..... 12345678910111213141516171819202122232425262728293031
2、dir()
dir函數(shù)是Python的一個內(nèi)置函數(shù)。?
函數(shù)原型:dir([object])?
可以幫助我們獲取該對象的大部分相關(guān)屬性。?
Without arguments, return the list of names in the current local scope.
dir() ?# 沒有參數(shù)['__builtins__', '__doc__', '__name__', '__package__'] import math ?# 引入一個包和一個變量,再次dir() a=3 dir()
['__builtins__', '__doc__', '__name__', '__package__', 'a', 'math'] 12345678910
With an argument, attempt to return a list of valid attributes for that object.
import math dir(math) ?# math模塊作為參數(shù)['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc'] 12345
The default dir() mechanism behaves differently with different types of objects, as it attempts to produce the most relevant, rather than complete, information:?
? If the object is a module object, the list contains the names of the module’s attributes.
import math dir(math) ?# math模塊作為參數(shù)['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc'] 12345
? If the object is a type or class object, the list contains the names of its attributes, and recursively of the attributes of its bases.
dir(float) ?# 類型['__abs__', '__add__', '__class__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__eq__', '__float__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getformat__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__int__', '__le__', '__long__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__nonzero__', '__pos__', '__pow__', '__radd__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rmod__', '__rmul__', '__rpow__', '__rsub__', '__rtruediv__', '__setattr__', '__setformat__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', 'as_integer_ratio', 'conjugate', 'fromhex', 'hex', 'imag', 'is_integer', 'real'] dir(3.4)
['__abs__', '__add__', '__class__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__eq__', '__float__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getformat__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__int__', '__le__', '__long__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__nonzero__', '__pos__', '__pow__', '__radd__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rmod__', '__rmul__', '__rpow__', '__rsub__', '__rtruediv__', '__setattr__', '__setformat__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', 'as_integer_ratio', 'conjugate', 'fromhex', 'hex', 'imag', 'is_integer', 'real'] class A:
x=3
y=4 class B(A):
z=5 dir(B) ?# 類['__doc__', '__module__', 'x', 'y', 'z'] 123456789101112131415161718
? Otherwise, the list contains the object’s attributes’ names, the names of its class’s attributes, and recursively of the attributes of its class’s base classes.
3、_?doc_
在Python中有一個奇妙的特性,文檔字符串,又稱為DocStrings。?
用它可以為我們的模塊、類、函數(shù)等添加說明性的文字,使程序易讀易懂,更重要的是可以通過Python自帶的標(biāo)準(zhǔn)方法將這些描述性文字信息輸出。?
上面提到的自帶的標(biāo)準(zhǔn)方法就是_?doc?_。前后各兩個下劃線。?
注:當(dāng)不是函數(shù)、方法、模塊等調(diào)用doc時,而是具體對象調(diào)用時,會顯示此對象從屬的類型的構(gòu)造函數(shù)的文檔字符串。
import math math.__doc__ ? # 模塊'This module is always available. ?It provides access to the\nmathematical functions defined by the C standard.' abs.__doc__ ? # 內(nèi)置函數(shù)'abs(number) - number\n\nReturn the absolute value of the argument.' def addxy(x,y):
'''the sum of x and y'''
return x+y addxy.__doc__ ?# 自定義函數(shù)'the sum of x and y' a=[1,2,4] a.count.__doc__ ?# 方法'L.count(value) - integer -- return number of occurrences of value' b=3 b.__doc__ ? # 具體的對象"int(x=0) - int or long\nint(x, base=10) - int or long\n\nConvert a number or string to an integer, or return 0 if no arguments\nare given. ?If x is floating point, the conversion truncates towards zero.\nIf x is outside the integer range, the function returns a long instead.\n\nIf x is not a number or if base is given, then x must be a string or\nUnicode object representing an integer literal in the given base. ?The\nliteral can be preceded by '+' or '-' and be surrounded by whitespace.\nThe base defaults to 10. ?Valid bases are 0 and 2-36. ?Base 0 means to\ninterpret the base from the string as an integer literal.\n int('0b100', base=0)\n4" 12345678910111213141516171819
其實我們可以通過一定的手段來查看這些文檔字符串,比如使用Pycharm,在對應(yīng)的模塊、函數(shù)、方法等上鼠標(biāo)“右擊”-Go to-Declaration。例如:查看內(nèi)置函數(shù)abs的文檔字符串?
我們再舉一個具體的對象的例子,例如,上面具體的整型對象b的doc顯示的就是其所從屬的int類型的文檔字符串:?
參考文獻(xiàn):?
1、Python幫助文檔
這是python的字符串格式化函數(shù)。格式為 '{0:[填充字符][對齊方式^][寬度]}'.format('[字符']
在本例中,填充字符為下劃線 _ ,對齊方式為 ^ (居中對齊),寬度為 11,字符為 'hello'。因此結(jié)果為: ___hello___
左右各三個下劃線,再加上hello,總共11個字符。
python的常用內(nèi)置函數(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ù)或模塊用途的詳細(xì)說明
help('math')查看math模塊的用處
a=[1,2,3,4]
help(a)查看列表list幫助信息
4.dir()獲得當(dāng)前模塊的屬性列表
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ù)用于獲取對象的內(nèi)存地址
a=12
id(a)
1550569552
8.enumerate() 函數(shù)用于將一個可遍歷的數(shù)據(jù)對象(如列表、元組或字符串)組合為一個索引序列,同時列出數(shù)據(jù)和數(shù)據(jù)下標(biāo),一般用在 for 循環(huán)當(dāng)中。
a=["tom","marry","leblan"]
list(enumerate(a))
[(0, 'tom'), (1, 'marry'), (2, 'leblan')]
9. oct() 函數(shù)將一個整數(shù)轉(zhuǎn)換成8進(jìn)制字符串
oct(15)
'0o17'
oct(10)
'0o12'
10. bin() 返回一個整數(shù) int 或者長整數(shù) long int 的二進(jìn)制表示
bin(10)
'0b1010'
bin(15)
'0b1111'
11.eval() 函數(shù)用來執(zhí)行一個字符串表達(dá)式,并返回表達(dá)式的值
eval('2+2')
4
12.int() 函數(shù)用于將一個字符串會數(shù)字轉(zhuǎn)換為整型
int(3)
3
int(3.6)
3
int(3.9)
3
int(4.0)
4
13.open() 函數(shù)用于打開一個文件,創(chuàng)建一個file對象,相關(guān)的方法才可以調(diào)用它進(jìn)行讀寫
f=open('test.txt')
14.str() 函數(shù)將對象轉(zhuǎn)化為適于人閱讀的形式
str(3)
'3'
15. bool() 函數(shù)用于將給定參數(shù)轉(zhuǎn)換為布爾類型,如果沒有參數(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() 方法對系列進(jìn)行求和計算
sum([1,2,3],5)
11
sum([1,2,3])
6
18. super() 函數(shù)用于調(diào)用下一個父類(超類)并返回該父類實例的方法。super 是用來解決多重繼承問題的,直接用類名調(diào)用父類方法
class ? User(object):
? def__init__(self):
class Persons(User):
? ? ? ? super(Persons,self).__init__()
19. float() 函數(shù)用于將整數(shù)和字符串轉(zhuǎn)換成浮點數(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ù)將列表轉(zhuǎn)換為元組
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() 方法用于將元組轉(zhuǎn)換為列表
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)建一個復(fù)數(shù)或者轉(zhuǎn)化一個字符串或數(shù)為復(fù)數(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)建一個無序不重復(fù)元素集,可進(jìn)行關(guān)系測試,刪除重復(fù)數(shù)據(jù),還可以計算交集、差集、并集等。
a= set("tom")
b = set("marrt")
a,b
({'t', 'm', 'o'}, {'m', 't', 'a', 'r'})
ab#交集
{'t', 'm'}
a|b#并集
{'t', 'm', 'r', 'o', 'a'}
a-b#差集
{'o'}
網(wǎng)站題目:gt函數(shù)python python agg函數(shù)
網(wǎng)頁路徑:http://chinadenli.net/article6/dogegog.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、企業(yè)網(wǎng)站制作、用戶體驗、網(wǎng)站內(nèi)鏈、品牌網(wǎng)站設(shè)計、定制開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)