Python之路——内置函数,,内置函数一 1 #


内置函数一

  1 # locals()\globals()\global\nonlocal  2 # print(locals())  3 # print(globals())  4   5 # 迭代器.__next__()  6 # next(Itrator)  7 # 迭代器 = iter(Iterable)  8 # 迭代器 = 可迭代的.__iter__()  9  10 # range 11 # range(10) 12 # range(1,11) 13 # print(‘__next__‘ in dir(range(1,11,2)))   # False 14  15  16 # dir 查看一个变量拥有的方法 17 # print(dir([])) 18 # print(dir(1)) 19  20 # callable 21 # print(callable(print)) 22  23 # help 24 # help(str) 25  26 #某个方法属于某个数据类型的变量,就用.调用 27 # 如果某个方法不依赖于任何数据类型,就直接调用 -- 内置函数 28  29 # import time 30 # # t = __import__(‘time‘) 31 # # print(t.time()) 32  33 # open 34  35 # id 返回一个变量的内存地址 36 # hash 37 # print(hash(132)) 38 # print(hash(‘adasd‘)) 39 # print(hash((‘1‘,‘aaa‘))) 40 # prograss Bar 41 # ************************************************** 42 # print 43 # print(‘womendezuguoshuayuan‘,end=‘‘) # 指定输出的结束符 44 # print(‘womendezuguoshuayuan‘) 45 # print(1,2,3,4,5,sep=‘|‘)    # 1|2|3|4|5 46  47 # f = open(‘file‘,‘w‘,encoding=‘utf-8‘) 48 # print(‘qqqq‘,end=‘‘,file=f) 49 # f.close() 50 # ************************************************** 51 # complex 52 # a = 2 + 3j 53 # print(type(a)) 54 # ************************************************** 55 # import time 56 # for i in range(0,101,2): 57 #     time.sleep(0.1) 58 #     char_num = i//2 59 #     per_str = ‘\r%s%% : %s\n‘ % (i,‘*‘*char_num) if i == 100 else ‘\r%s%% : %s‘ % (i,‘*‘*char_num) 60 #     print(per_str,end=‘‘,flush=True) 61 #     # print(per_str, end=‘‘) 62 # ************************************************** 63 # eval 和 exec执行字符串类型的代码 64 # exec(‘print(123)‘) 65 # eval(‘print(123)‘) 66 # print(eval(‘1+2+3+4‘)) 67 # print(exec(‘1+2+3+4‘))  # None 68 # exec 和eval都可以执行字符串类型的代码 69 # eval有返回值 -- 有结果的简单计算 70 # exec没有返回值 -- 简单流程控制 71 # eval只能用在你明确知道你要执行的代码是什么 72  73 # code = ‘‘‘for i in range(10): 74 #     print(i*‘*‘)‘‘‘ 75 # exec(code) 76 # # eval(code)    # SyntaxError: invalid syntax 77  78 # code1 = ‘for i in range(0,10):print(i)‘ 79 # compile1 = compile(code1,‘‘,‘exec‘) 80 # exec(compile1) 81 # eval(compile1)  # 可以正确执行 82  83 # code2 = ‘1+2+3+4‘ 84 # compile2 = compile(code2,‘‘,‘eval‘) 85 # print(eval(compile2)) 86  87 # code3 = "name = input(‘please input your name:‘)" 88 # compile3 = compile(code3,‘‘,‘single‘) 89 # exec(compile3) 90 # print(name) 91 # ************************************************** 92  93 # float 94 # f = 0.2134548798764845465 95 # print(f) 96  97 # # 二进制、八进制、十六进制 98 # print(bin(10))  # 0b1010 99 # print(oct(10))  # 0o12100 # print(hex(10))  # 0xa101 102 # abs 求绝对值103 # print(abs(-5))104 105 106 # print(divmod(7,2))  # (3, 1)107 # print(divmod(9,5))  # (1, 4)108 109 # round110 # print(round(0.23456789,4))  # 0.2346111 # pow112 # print(pow(2,3)) # 8113 # # x**y%z114 # print(pow(2,5,3))   # 2115 116 # sum117 # ret = sum([1,2,3,5])118 # print(ret)119 # # ret = sum(1,2,3,4)120 # # print(ret)121 # ret = sum([1,2,3,4],15)122 # print(ret)123 124 # min125 # print(min([1,2,3,-4]))126 # print(min(1,2,3,4,-5))127 # print(min(1,2,3,-4,key=abs))

内置函数二

 1 # sorted 2 # l1 = [1,2,3,5,-2,-4,-6] 3 # l2 = sorted(l1,key=abs) 4 # l3 = sorted(l1) 5  6 # print(l1) 7 # print(l2) 8 # print(l3) 9 # l = [[1,2],[3,4,5,6],(7,),‘123‘]10 # l.sort(key=len,reverse=True)11 # print(l)12 #13 # print(sorted(l,key=len))14 15 # filter16 # def is_odd(x):17 #     return x%2 ==118 # # print(is_odd(1))19 # a = filter(is_odd,[1,2,3,4,6,7,34,63,23])20 # for i in a:21 #     print(i)22 23 # def is_not_empty(s):24 #     return s and len(s.strip()) >025 # a = filter(is_not_empty,[‘test‘, None, ‘‘, ‘str‘, ‘  ‘, ‘END‘])26 # print(list(a))27 28 # def is_sqr(x):29 #     return pow(x,0.5) % 1 == 030 # flt = filter(is_sqr,range(1,101))31 # print(list(flt))32 33 # L =  [1,2,3,4,5,6,7,8]34 # def pow2(x):35 #     return x*x36 # a = map(pow2,L)37 # print(list(a))38 39 40 41 # l= [1,2,3,4,5]42 # l.reverse()# 将l反序43 # print(l)44 45 # l2 = reversed(l)  # 返回一个能反序的迭代器46 # print(list(l2))47 48 # zip49 # l = [1,2,3,4]50 # l2 = [‘a‘,‘b‘,‘c‘]51 # for i in zip(l,l2):52 #     print(i)53 54 # format55 # print(format(3.1415936))56 # print(str(3.1415936))57 58 # 字符串可以提供的参数,指定对齐方式,<是左对齐, >是右对齐,^是居中对齐59 # print(format(‘test‘,‘<20‘))60 # print(format(‘test‘,‘>20‘))61 # print(format(‘test‘,‘^20‘))62 63 # 整形数值可以提供的参数有 ‘b‘ ‘c‘ ‘d‘ ‘o‘ ‘x‘ ‘X‘ ‘n‘ None64 # print(format(3,‘b‘))    # 转换成二进制65 # print(format(97,‘c‘))   # 转换成uinicode字符66 # print(format(11,‘d‘))   # 转换成十进制67 # print(format(11,‘o‘))   # 转换成八进制68 # print(format(11,‘x‘))   # 转换成16进制,字母用小写表示69 # print(format(11,‘X‘))   # 转换成16进制,字母用大写表示70 # print(format(11,‘n‘))   # 和d一样71 # print(format(11))        # 默认和d一样72 73 # #浮点数可以提供的参数有 ‘e‘ ‘E‘ ‘f‘ ‘F‘ ‘g‘ ‘G‘ ‘n‘ ‘%‘ None74 # print(format(314149267,‘e‘)) #科学计数法,默认保留6位小数75 # print(format(314149267,‘0.8e‘)) #科学计数法,指定保留8位小数76 # print(format(314159267,‘0.2E‘)) # 科学计数法,指定保留2位小数,采用大写E表示77 # print(format(314159267,‘f‘))    # 小数点表示法,默认保留6位小数78 # print(format(3.14159267000,‘f‘))    # 小数点表示法,默认保留6位小数79 # print(format(3.14159267000,‘0.8f‘))    # 小数点表示法,指定保留8位小数80 # print(format(3.14159267000,‘0.10f‘))    # 小数点表示法,指定保留10位小数81 # print(format(3.14e+10000,‘F‘))  # 小数点表示法,无穷大换成大写字母82 83 #g的格式化比较特殊,假设p为格式中指定的保留小数位数,先尝试采用科学计数法格式化,得到幂指数exp,84 # 如果-4<= exp <p,则采用小数计数法,并保留p-1-exp位小数,否则按小数计数法计数,并按p-1保留小数位数85 # print(format(314156.9267,‘0.3g‘))   # 3.14e+0586 # print(format(3.141569267,‘0.3g‘))

Python之路——内置函数

评论关闭