转自:https://blog.csdn.net/oaa608868/article/details/53506188
内置函数(BIF,built-in functions)是Python内置对象类型之一,不需要额外导入任何模块即可直接使用,这些内置对象都封装在内置模块builtins之中,用C语言实现并且进行了大量优化,具有非常快的运行速度,推荐优先使用。
函数 | 返回结果 |
---|---|
abs(-2) | 2 |
abs(0) | 0 |
函数 | 返回结果 |
---|---|
divmod(10,3) | (3,1) |
divmod(5.5,2) | (2.0,1.5) |
divmod(5,-2) | (-3,1) |
函数 | 返回结果 |
---|---|
max(-1,0) | 0 |
max(‘1234’) | ‘4’ |
max(-1,-2,key = abs) | -2 |
函数 | 返回结果 |
---|---|
min(-1,0) | -1 |
min(‘1234’) | ‘1’ |
min(-1,-2,key = abs) | -1 |
函数 | 返回结果 |
---|---|
pow(2,3) | 8 |
pow(2,3,5) | 3(即2 ** 3 % 5) |
函数 | 返回结果 |
---|---|
round(1.1314926,5) | 1.13149 |
round(123.45,-2) | 100.0(即在十位四舍五入) |
函数 | 返回结果 |
---|---|
sum((1,2,3,4),-10)) | 0(可迭代,但元素必须是数值型) |
函数 | 返回结果 |
---|---|
bool() | False |
bool(0) | False |
bool(1) | True(参数不为数值0和空序列,值为真) |
bool(2) | True |
函数 | 返回结果 |
---|---|
int() | 0 |
int(3) | 3 |
int(3.6) | 3 |
函数 | 返回结果 |
---|---|
float() | 0.0 |
float(3) | 3.0 |
float(‘3’) | 3.0 |
函数 | 返回结果 |
---|---|
complex() | 0j |
complex(‘1+2j’) | (1+2j) |
complex(1,2) | (1+2j) |
示例:str(None)
‘None’
示例:bytearray('中文','utf-8')
bytearray(b’\xe4\xb8\xad\xe6\x96\x87’)
示例:bytes('中文','utf-8')
b’\xe4\xb8\xad\xe6\x96\x87’
示例:v = memoryview(b'abcefg')'
v[1]
98
v[-1]
103
示例:ord('a')
97
示例:chr(97)
‘a’
示例:bin(3)
‘0b11’
示例:oct(10)
‘0o12’
示例:hex(15)
‘0xf’
示例:tuple('121')
(‘1’, ‘2’, ‘1’)
示例:list('abcd')
[‘a’, ‘b’, ‘c’, ‘d’]
示例:dict(a = 1,b = 2)
{‘b’: 2, ‘a’: 1}
示例:set(range(10))
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
示例:frozenset(range(10))
frozenset({0, 1, 2, 3, 4, 5, 6, 7, 8, 9})
示例:seasons = ['Spring', 'Summer', 'Fall', 'Winter']
list(enumerate(seasons))
[(0, ‘Spring’), (1, ‘Summer’), (2, ‘Fall’), (3, ‘Winter’)]
list(enumerate(seasons, start=1))
#指定起始值
[(1, ‘Spring’), (2, ‘Summer’), (3, ‘Fall’), (4, ‘Winter’)]
a = range(10)
b = range(1,10)
c = range(1,10,3)
a,b,c
(range(0, 10), range(1, 10), range(1, 10, 3))
list(a),list(b),list(c)
([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 4, 7])
a = iter('abcd')
a
next(a)
‘a’
next(a)
‘b’
next(a)
‘c’
next(a)
‘d’
next(a)
Traceback (most recent call last):
File “
next(a)
StopIteration
slice(5)
slice(None, 5, None)
slice(2,5)
slice(2, 5, None)
slice(1,10,3)
slice(1, 10, 3)
定义父类A
class A(object):
def __init__(self):
print('A.__init__')
定义子类B,继承A
class B(A):
def __init__(self):
print('B.__init__')
super().__init__()
super调用父类方法
b = B()
B.init
A.init
a = object()
a.name = 'kim'
Traceback (most recent call last):
File “
a.name = ‘kim’
AttributeError: ‘object’ object has no attribute ‘name’
函数 | 返回结果 |
---|---|
print(1,2,3) | 1 2 3 |
print(1,2,3,sep = ‘+’) | 1+2+3 |
print(1,2,3,sep = ‘+’,end = ‘=?’) | 1+2+3=? |
s = input('please input your name:')
please input your name:Ain
s
‘Ain’
#t为文本读写,b为二进制读写
a = open('test.txt','rt')
a.read()
‘some text’
a.close()
code1 = 'for i in range(0,10): print (i)'
compile1 = compile(code1,'','exec')
exec (compile1)
0
1
2
3
4
5
6
7
8
9
示例:eval('1+2+3+4')
10
示例:exec('1+2')
3
a = 'some text'
str(a)
‘some text’
repr(a)
“‘some text’”
>>> class C:
def __init__(self):
self._name = ''
@property
def name(self):
"""i'm the 'name' property."""
return self._name
@name.setter
def name(self,value):
if value is None:
raise RuntimeError('name can not be None')
else:
self._name = value
>>> c = C()
>>> c.name # 访问属性
''
>>> c.name = None # 设置属性时进行验证
Traceback (most recent call last):
File "", line 1, in
c.name = None
File "", line 11, in name
raise RuntimeError('name can not be None')
RuntimeError: name can not be None
>>> c.name = 'Kim' # 设置属性
>>> c.name # 访问属性
'Kim'
>>> del c.name # 删除属性,不提供deleter则不能删除
Traceback (most recent call last):
File "", line 1, in
del c.name
AttributeError: can't delete attribute
>>> c.name
'Kim'
>>> class C:
@classmethod
def f(cls,arg1):
print(cls)
print(arg1)
>>> C.f('类对象调用类方法')
类对象调用类方法
>>> c = C()
>>> c.f('类实例对象调用类方法')
类实例对象调用类方法
# 使用装饰器定义静态方法
>>> class Student(object):
def __init__(self,name):
self.name = name
@staticmethod
def sayHello(lang):
print(lang)
if lang == 'en':
print('Welcome!')
else:
print('你好!')
>>> Student.sayHello('en') #类调用,'en'传给了lang参数
en
Welcome!
>>> b = Student('Kim')
>>> b.sayHello('zh') #类实例对象调用,'zh'传给了lang参数
zh
你好