31.
bytearray()方法返回一個新字節(jié)數(shù)組。這個數(shù)組里的元素是可變的,并且每個元素的值范圍: 0 <= x < 256。
實例:
>>> bytearray()
bytearray(b'')
>>> bytearray([1,2,3])
bytearray(b'\x01\x02\x03')
>>> bytearray('runoob','utf-8')
bytearray(b'runoob')
32.
filter()函數(shù)用于過濾序列,過濾掉不符合條件的元素,返回由符合條件元素組成的新列表。
該接收兩個參數(shù),第一個為函數(shù),第二個為序列,序列的每個元素作為參數(shù)傳遞給函數(shù)進(jìn)行判,然后返回 True 或 False,最后將返回 True 的元素放到新列表中。
實例:
>>> def odd(n):
...? ? return n % 2 ==1
...
>>> list=filter(odd,[1,2,3,4,5,6,7,8,11,45])
>>> next(list)
1
>>> next(list)
3
>>> next(list)
5
>>> next(list)
7
>>> next(list)
11
>>> next(list)
45
33.issubclass()方法用于判斷參數(shù) class 是否是類型參數(shù) classinfo 的子類。
實例:
>>> class A:
...? ? pass
...
>>> class B(A):
...? ? pass
...
>>> print(issubclass(B,A))
True
34.pow()方法返回 xy(x的y次方) 的值。
實例:
>>> import math
>>> print("math.pow(100,2):",math.pow(100,2))
math.pow(100,2): 10000.0
>>> print("pow(100,2):",pow(100,2))
pow(100,2): 10000
>>> print("math.pow(100,-2):",math.pow(100,-2))
math.pow(100,-2): 0.0001
>>> print("math.pow(2,4):",math.pow(2,4))
math.pow(2,4): 16.0
>>> print("math.pow(2,0):",math.pow(2,0))
math.pow(2,0): 1.0
35.super()函數(shù)用于調(diào)用下一個父類(超類)并返回該父類實例的方法。
super 是用來解決多重繼承問題的,直接用類名調(diào)用父類方法在使用單繼承的時候沒問題,但是如果使用多繼承,會涉及到查找順序(MRO)、重復(fù)調(diào)用(鉆石繼承)等種種問題。
MRO 就是類的方法解析順序表, 其實也就是繼承父類方法時的順序表。
實例:
>>> class FooParent(object):
...? ? def __init__(self):
...? ? ? ? ? ? self.parent='I am the parent.'
...? ? ? ? ? ? print('Parent')
...? ? def bar(self,message):
...? ? ? ? ? ? print("%s from Parent" % message)
...
>>> class FooChild(FooParent):
...? ? def __init__(self):
...? ? ? ? ? ? super(FooChild,self).__init__()
...? ? ? ? ? ? print('Child')
...? ? def bar(self,message):
...? ? ? ? ? ? super(FooChild,self).bar(message)
...? ? ? ? ? ? print('Child bar fuction')
...? ? ? ? ? ? print(self.parent)
...
>>> if __name__=="__main__":
...? ? foochild=FooChild()
...? ? foochild.bar("HelloWorld")
...
Parent
Child
HelloWorld from Parent
Child bar fuction
I am the parent.
36.bytes 函數(shù)返回一個新的 bytes 對象,該對象是一個 0 <= x < 256 區(qū)間內(nèi)的整數(shù)不可變序列。它是 bytearray 的不可變版本。
實例:
>>> a=bytes([1,2,3,4])
>>> a
b'\x01\x02\x03\x04'
>>> type(a)
>>> a=bytes("hello","ascii")
>>> a
b'hello'
>>> type(a)
<class 'bytes'>
37.float()函數(shù)用于將整數(shù)和字符串轉(zhuǎn)換成浮點數(shù)。
>>> float(1)
1.0
>>> float(111)
111.0
>>> float(-111)
-111.0
>>> float(-111.33)
-111.33
>>> float('-66.66')
-66.66
38.iter()函數(shù)用來生成迭代器。
實例:
>>> lst=[1,2,3]
>>> for i in iter(lst):
...? ? print(i)
...
1
2
3
39.print()方法用于打印輸出,最常見的一個函數(shù)。
>>> print(1)
1
>>> print("hello python")
hello python
>>> a=1
>>> b='asa'
>>> print(a,b)
1 asa
40.tuple 函數(shù)將列表轉(zhuǎn)換為元組。
>>> list=['baidu','taobao','tengxun']
>>> tuple=tuple(list)
>>> tuple
('baidu', 'taobao', 'tengxun')