本文将这68个内置函数综合整理为12大类,正在学习Python基础的读者一定不要错过,建议收藏学习!
print(bin(10)) # 0b1010
print(oct(10)) # 0o12
print(hex(10)) # 0xa
print(abs((-2)))
print(divmod(20, 3)) # (6, 2)
print(round(4.52))
print(pow(10, 2, 3)) # 1 如果给了第三个参数,表示最后取余1
print(sum([1, 2, 3, 4, 5]))
print(max(3, 5, 2, 6, 8)) # 8
print(min(3, 5, 2, 6, 8)) # 2
print(list((1, 2, 3, 4, 5))) # [1, 2, 3, 4, 5]
print(tuple([2, 3, 6, 6, 7, 5])) # (2, 3, 6, 6, 7, 5)
res = 'wqnmlgb'
lst = reversed(res)
print(list(lst)) # ['b', 'g', 'l', 'm', 'n', 'q', 'w']
ll = [1, 2, 3, 4, 5, 6, 7]
print(ll[1::2]) # [2, 4, 6]
s = slice(1, 7, 2)
print(ll[s]) # # [2, 4, 6]
s = 'hello world!'
print(format(s, '^20')) # 将hello world!居中
print(format(s, '<20')) # 将hello world!左对齐
print(format(s, '>20')) # 将hello world!右对齐
# 运行结果如下:
'''
hello world!
hello world!
hello world!
'''
print(format(3, 'b')) # 二进制11
print(format(97, 'c')) # 转换成unicode字符:a
print(format(11, 'd')) # 十进制 11
print(format(11, 'o')) # 八进制 13
print(format(11, 'x')) # 十六进制(小写字母:b)
print(format(11, 'X')) # 十六进制(大写字母:B)
print(format(11, 'n')) # 和d一样 11
print(format(11)) # 和d一样 11
print(format(1234134234, 'e')) # 科学计数法
print(format(1234134234, '0.2e'))
print(format(1234134234, '0.2E'))
print(format(1234134234, 'f'))
print(format(1234134234, '0.2f'))
print(format(1234134234, '0.2F'))
print(format(1234134234, '0.10f'))
print(format(1234134234e+3, 'F'))
res = bytes('早上好,tony', encoding='utf8')
print(res) # b'\xe6\x97\xa9\xe4\xb8\x8a\xe5\xa5\xbd\xef\xbc\x8ctony'
res = bytearray('kevin', encoding='utf8')
print(res[0]) # 107
print(res) # bytearray(b'kevin')
res[0] = 107 # 107的位置K赋值给res[0]
print(str(res)) # bytearray(b'kevin')
print(ord('a')) # 字母a在编码中的位置97
print(ord('中')) # 中在编码中的位置20013
print(chr(65)) # 已知码位,求字符是什么:A
print(chr(199999)) #
for i in range(65535):
print(chr(i), end=' ')
print(ascii('@'))
s = '今天\n吃了%s顿\t饭' % 3
print(s)
print(repr(s)) # '今天\n吃了3顿\t饭'
lst = [5, 7, 6, 12, 1, 13, 9, 18, 5]
lst.sort()
print(lst) # [1, 5, 5, 6, 7, 9, 12, 13, 18]
ll = sorted(lst)
print(ll) # [1, 5, 5, 6, 7, 9, 12, 13, 18]
l2 = sorted(lst, reverse=True) # [18, 13, 12, 9, 7, 6, 5, 5, 1]
print(l2)
# 根据字符串长度排序
lst = ['one', 'two', 'three', 'four', 'five', 'six']
def index(s):
return len(s)
ll = sorted(lst, key=index, )
print(ll) # ['one', 'two', 'six', 'four', 'five', 'three']
# enumerate()获取集合的枚举对象
lst = ['one', 'two', 'three', 'four', 'five', 'six']
for k, v in enumerate(lst, 1): # 把索引和元素一起获取,索引默认从0开始,可以更改
print(k)
print(v)
# 运行结果如下:
'''
1
one
2
two
3
three
4
four
5
five
6
six
'''
lst1 = [1, 2, 3, 4, 5]
lst2 = ['醉乡民谣', '驴得水', '放牛班的春天', '美丽人生', '辩护人', '被嫌弃的松子的一生']
lst3 = ['美国', '中国', '法国', '意大利', '韩国', '日本']
print(zip(lst1, lst2, lst3)) #
for e in zip(lst1, lst2, lst3):
print(e)
# 运行结果如下:
'''
(1, '醉乡民谣', '美国')
(2, '驴得水', '中国')
(3, '放牛班的春天', '法国')
(4, '美丽人生', '意大利')
(5, '辩护人', '韩国')
'''
def func(i): # 判断奇数
return i % 2 == 1
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9]
ll = filter(func, lst) # ll是迭代器
print(list(ll)) # [1, 3, 5, 7, 9]
def func(i):
return i
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9]
it = map(func, lst)
print(list(it)) # [1, 2, 3, 4, 5, 6, 7, 8, 9]
def func():
a = 10
print(locals()) # 当前作用域的内容
print(globals()) # 全局作用域的内置
print('today is very more')
func()
for i in range(15, -1, -5):
print(i) # 15 10 5 0
lst = [1, 2, 3, 4]
it = iter(lst)
print(it.__iter__())
print(it.__next__())
print(it.__next__())
print(it.__next__())
s1 = input('请输入a+b:')
# eval()可以动态执行代码,代码必须有返回值
print(eval(s1)) # 请输入a+b:1+2 3
s2 = 'for i in range(5): print(i)'
a = exec(s2)
code1 = 'for i in range(3):print(i)'
com = compile(code1, "", mode='exec')
exec(com)
s = 'alex'
print(hash(s))
lst = [1, 2, 3, 4, 5]
# print(hash(lst)) # 列表是不可哈希的
print(id(s))