作用域相关 locals()、globals()# print(locals())# 返回本地作用域中的所有名字# print(globals()) #返回全局作用域中的所有名字# 迭代器.__next__()#next(迭代器)# 迭代器 = iter(可迭代的)# 迭代器 = 可迭代的.__iter__()# print('__next__' in dir(range(1,11,2)))# dir 查看一个变量拥有的方法# print(dir([]))#调用相关callable#变量 查看 callable()是否是个函数# print(callable(print))# a = 1# print(callable(a))# help 查看方法及其用法# help(str)#模块相关#import time# t = __import__('time')# print(t.time())#方法属于数据类型的变量,就用调用#如果某个方法不依赖于任何数据类型,就直接调用 -- 内置函数 和 自定义函数# 文件操作相关open()# f = open('file.py')# print(f.writable())# print(f.readable())# 内存相关 id()hash()# print(hash(123))# print(hash('dsf'))# print(hash(('1','sf')))# print(hash([]))# 对于相同可以hash数据的hash值在一次程序的执行过程中总是不变的# 输入输出print()、input()# f = open('file','w')# print('aaa',file=f)# f.close()# import time# for i in range(0, 101, 2):# time.sleep(0.1)# char_num = i//2# per_str = '\r%s%% : %s\n' % (i, '*' * char_num) if i == 100 else '\r%s%% : %s' % (i,'*'*char_num)# print(per_str,end='',flush=True)# print(123)#字符串类型代码的执行 exec() eval()#exec eval都可以执行 字符串类型的代码#eval只能用在你明确知道你要执行的代码是什么# exec('print(123)')# eval('print(123)')# print(eval('1+2+3'))# print(exec('1+2+3'))#没有返回值# code = '''for i in range(10):# print(i*'*')'''# exec(code)# 进制转换# print(hex(10))# print(bin(10))# print(oct(10))#运算# print(abs(-5))# 绝对值# print(divmod(7,2))# div除法,mod除余,除余# print(round(3.1415926,2))# print(pow(2,3)) #幂运算# print(pow(3,2,1))# print(pow(3,2,4))#幂运算之后再取余# print(sum([1,2,3,4]))#10# print(sum([1,2,3,4],10))#20# print(min([1,2,3,4]))#1# print(min(1,2,3,-4,key=abs))# print(max(1,2,3,-4,key=abs))#以绝对值来找最大值
print(round(3.1344646,2))#3.13 精确值
#数据类型:int bool#数据结构:tuple set list dict#reversed() 保留原列表,返回一个反向的迭代器l = [1,2,3,4,5]# l.reverse()# print(l)#[5, 4, 3, 2, 1]# l2 = reversed(l)# print(l2)#<list_reverseiterator object at 0x0000026BC23D6978>#slice()切片# l = [1,2,3,4,5,35,343,545]# sl = slice(1,5,2)# print(l[sl])#[2, 4]#format()# print(format('test', '<20'))# print(format('test', '>40'))# print(format('test', '^40'))# test# test# test#bytes() 转换成bytes类型# print(bytes('你好',encoding='GBK').decode('GBK')) #unicode转换成GBK的bytes# print(bytes('你好',encoding='utf-8'))#unicode转换成utf-8的bytes# 你好# b'\xe4\xbd\xa0\xe5\xa5\xbd'# 网络编程 只能传二进制# 照片和视频也是以二进制存储# html网页爬取到的也是编码#bytearray()# b = bytearray('你好',encoding='utf-8')# print(b)# print(b[0])#bytearray(b'\xe4\xbd\xa0\xe5\xa5\xbd')# 228#ord# print(ord('a'))# print(ord('1'))# # 97# # 49# print(chr(97))#a# print(ord('你'))#20320#ASCII# print(ascii(97))# print(ascii('你'))# 97# '\u4f60'# repr# name = 'egg'# print('你好,%r'%name)# print(repr('1'))# print(repr(1))# # 你好,'egg'# '1'# 1#len 字符长度#enumerate 枚举#all# print(all(['a','',123]))# print(all(['a','']))# print(all(['a',123]))# #any# print(any(['',True]))# False# False# True# True# #zip# l =[1,2,3]# l2 = ['a','b','c']# l3 = ['&','*',[1,2]]# for i in zip(l,l2,l3):# print(i)# (1, 'a', '&')# (2, 'b', '*')# (3, 'c', [1, 2])#filter(函数名,可迭代对象)# def is_odd(x):# return x % 2 == 1## def is_str(s):# return type(s) == str# ret = filter(is_odd, [1, 6, 7, 12, 17])# ret1 = filter(is_str,[1, 'df', 7, 'dfds', 17])# print(ret)# for i in ret:# print(i)# print(ret1)# for i in ret1:# print(i)# <filter object at 0x000001D74D087208># 1# 7# 17# <filter object at 0x000001D74D087278># df# dfds#sqrt#from math import sqrt# def func(num):# res = sqrt(num)# return res % 1 == 0# ret = filter(func,range(1,101))# for i in ret:# print(i)#map map(函数名,可迭代对象)ret = map(abs,[1,-4,6,-8])print(ret)for i in ret: print(i)# <map object at 0x00000292E0EB20B8># 1# 4# 6# 8
# filter 执行了filter之后的结果集合 <= 执行之前的个数 #filter只管筛选,不会改变原来的值# map 执行前后元素个数不变 # 值可能发生改变
l = [' ',[1,2],'hello world']new_l = sorted(l,key=len)print(new_l)# [[1, 2], ' ', 'hello world']
来源:https://www.cnblogs.com/rssblogs/p/10943472.html