python -- 内置函数 Built-in Funcitons

python -- 内置函数 Built-in Funcitons

我是官方链接

什么是内置函数

内置函数,Python在启动这些函数就已经生成。

all()

# all()判断可迭代对象中的元素是否都是非0
# 只要有0,就返回False
# 只有全非0,才返回True
# 空,也返回True
a = [1, 2, 3, 4]
b = [0, 2, 3, 4]
c = [-1, 2, -3, 4]

# True
print(all(a))
# False
print(all(b))
# True
print(all(c))

any()

# any()判断可迭代对象中的元素是否有非0
# 只要有非0,就返回True
# 只有全0,才返回False
# 空,也返回False
a = [1, 2, 3, 4]
b = [0]
c = [-1, 2, -3, 4]

# True
print(any(a))
# False
print(any(b))
# True
print(any(c))

ascii()

a = 0
b = ascii(a)

# <class 'str'> 0
print(type(b), b)

bin()

a = 10
b = bin(a)

# <class 'str'> 0b1010
print(type(b), b)

bool()

# 0的bool() = False
# 空列表,字典,元组的bool() = False
a, b, c, d, e = 1, 0, [], {}, ()

# True
print(bool(a))
# False
print(bool(b))
# False
print(bool(c))
# False
print(bool(d))
# False
print(bool(e))

bytearray()

a = bytes('abcde', encoding='utf-8')
b = bytearray('abcde', encoding='utf-8')

# b'abcde'
print(a)
# bytearray(b'abcde')
print(b)
# b'Abcde' b'abcde'
print(a.capitalize(), a)
# 这里的50是ascii表对应的2
b[1] = 50
# bytearray(b'a2cde')
print(b)

callable()

def hello():
    pass


def hello1():
    yield

a = [1, 2, 3]

# False
print(callable(a))
# True
print(callable(hello))
# True
print(callable(hello1))

chr() ord()

# <class 'str'> P
print(type(chr(80)), chr(80))
# <class 'int'> 80
print(type(ord("P")), ord("P"))

compile() j较为底层的函数

code_1 = 'for i in range(5):print(i)'

a = compile(code_1, '', 'exec')

# <code object <module> at 0x0000000000803270, file "", line 1>
print(a)

exec(a)

'''
以下似乎程序的结果:
0
1
2
3
4
'''
code_1 = '2+3*10'

a = compile(code_1, '', 'eval')

# <code object <module> at 0x0000000000803270, file "", line 1>
print(a)

# 32
print(eval(a))

filter()

# 在python3中,filter的结果变成了迭代器
res = filter(lambda n: n > 5, range(10))
for i in res:
    print(i)
'''
以下是程序的结果:
6
7
8
9
'''

map()

res = map(lambda n: n*n, range(5))
for i in res:
    print(i)
'''
以下是程序的结果:
0
1
4
9
16
'''

lambda()的补充

import functools

res = functools.reduce(lambda x, y: x*y, range(1,10))
res_2 = functools.reduce(lambda x, y: x+y, range(10))
print(res)
print(res_2)

'''
以下是程序的结果:
362880
45
'''

sorted()

a = {11: -22, 22: -33, 33: -44, 44: -55}
print(a)
# 把a的key值排序,并且返回list
b = sorted(a)
print(b)
# 把a中key对应的value值排序,并且返回list
c = sorted(a.items(), key=lambda x: x[1])
print(c)

zip()

a = [1, 2, 3, 4, 5]
b = ['a', 'b', 'c', 'd', 'e']

a_1 = [1, 2, 3, 4, 5]
b_1 = ['a', 'b', 'c', ]

c = zip(a, b)
# c_1按照最少数值配对
c_1 = zip(a_1, b_1)

for i in c:
    print(i)
print('--------------------')
for j in c_1:
    print(j)

'''
以下是程序的结果:
(1, 'a')
(2, 'b')
(3, 'c')
(4, 'd')
(5, 'e')
--------------------
(1, 'a')
(2, 'b')
(3, 'c')
'''

写在后面

函数名 功能 适用对象 返回值 备注 特殊
abs() 取绝对值 int/float 正数 abs(0)=0
all() 判断可迭代对象内元素是否有0 可迭代对象 True or False 没有=True;有=False 空=True
any() 判断可迭代对象内任一元素是否有真 可迭代对象 True or False 只要有一个=True;一个真都没有=False 空=False
ascii() 返回包含ascii码的str 返回格式为str
bin() 将int转换成二进制的str int 二进制 返回值是str float不能bin()
bool() 判断真假 True or False 属于class 空=False
bytearray()
bytes()
callable() 查看是否可以调用 True or False 是否可以加()
chr() 把数字对应的Unicode表中的字符显示 str类 ()里需要正整数 与ord()对立
classmethod() 类方法
compile() 把代码进行编译的过程 str str对应的程序效果 exec和eval 可以实现动态导入的效果
complex()
delatter()
dict() 可以生产字典 dict()可以生产一个空字典
dir() 查看对象的使用方法 对象
divmod() 相除,返回商和余数 int/float (商,余)
enumerate()
eval() 把字符串便字典 str 字典格式或int 可以处理简单的数学运算,包含for的无法完成 和exec()结合记忆
exec() 处理较为复杂的运算 str 运算结果 和eval()结合记忆
filter() 过滤 可迭代对象 迭代器
float() 浮点 int float
format() str
frozenset() 不可变集合 可迭代对象 不可变的集合 把原set有的pop,clear取消
getattr()
globals() 返回当前py内,变量:值 dict
hasattr()
hash() 哈希,散列 算法
help() 查看帮助
hex() 转16进制 int str
id() 返回内存地址
input()
int()
isinstance()
issubclass() 是不是子类
iter() 迭代器
len() 长度
list()
locals() 打印局部变量 打印def内的变量 与globals()结合记忆
map() 按照()内的方式处理 可迭代对象 和filter()结合记忆
max() 返回最大值 可迭代对象
memoryview()
min() 返回最小值 可迭代对象
next()
object()
oct() 转8进制 int 8进制
open() 打开文件
ord() 将单个str在Unicode表中对应的数字返回 int类 只能一个str,可以中文 与chr()对立
pow() 返回幂 pow(2, 3) int pow(x, y),x的y次方幂
print()
property()
range()
repr() 用str显示对象
reversed()
round() 保留小数位 float float round(3.1415926, 2)保留2位小数
set()
setattr()
slice() 切片
sorted() 排序
staticmethod()
str()
sum()
super()
tuple()
type()
vars()
zip() 拉链结合 最少元素原则配对
__import__() 通过str来引用功能 str 只知道str就可以import __import__('str')
posted @ 2017-07-19 17:53  gzz041  阅读(347)  评论(0)    收藏  举报