1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
| #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/10/9 15:59 # @Author : Ropon # @File : 14_01.py
#内置函数
#作用域 # locals() # globals()
#迭代器相关 # range() # next() #调用内部封装__next()__ # iter() #调用内部封装__iter()__
#字符串类型代码执行 #eval() # print(eval("2" + "2")) # print(eval("2 + 2"))
#执行字符串类型的代码 #exec() # exec(""" # for i in range(5): # print(i) # """)
#将字符串类型代码变异,代码对象能通过exec执行或通过eval进行求值 # code1 = "for i in range(10): print(i)" # c1 = compile(code1, "", mode="exec") # exec(c1)
# code2 = "1+2+3" # c2 = compile(code2, "", mode="eval") # a = eval(c2) # print(a)
# code3 = "name = input('请输入您的名字:')" # c3 = compile(code3, "", mode="single") # exec(c3) # print(name)
#总结:有返回值字符串形式代码使用eval,没有返回值的字符串形式代码用exec,很少使用compile
#输入输出有关 #input() #print()
#内存相关 # hash() #获取int,str,bool,tuple对象的哈希值 # id() #获取对象的内存地址
#文件操作相关 #open()
#模块相关 # __import__() #用于动态加载类和函数
#帮助 # help()
#调用相关 # callable() #检查对象是否可调用,返回True,对象有可能调用失败,但如果返回False,那调用肯定不成功
#查看对象内置属性 # dir() #查看对象内置属性,方法;访问的是对象中__dir__()方法
#基础数据类型相关 #数字相关 # bool() #将给的数据转换为bool值,不给值返回False # int() #将给的数据转换为int值,不给值返回0 # float() #将给的数据转换为float值,不给值返回0.0 # complex()#创建一个复数,第一个参数为实部,第二个参数为虚部;或者第一个参数直接用字符串来描述复数 #complex(1, 1) #complex("1+1j") # a = complex("1-2j") # print(a.real) # print(a.imag)
#进制转换 # bin() #将给的值转换成二进制 # oct() #将给的值转换成八进制 # hex() #将给的值转换成十进制 # a = 15 # print(bin(a)) # print(oct(a)) # print(hex(a))
#数学运算 # abs() #返回绝对值 # divmod(x, y) #表示x/3 返回商和余数 # round() #四舍五入 # pow(x, y, [z]) #[]表示可选,求x的y次幂,如果有第三个参数,求完幂后再对第三个数取余 # sum() #求和 参数可迭代对象,如列表、元组、集合 # min() #求最小值 # max() #同理求最大值 # a = 7 # print(abs(a)) # print(divmod(a, 3)) # print(round(4.3)) # print(pow(2, 4, 3)) # print(sum((3, 2))) # print(sum([1, 2, 3])) # print(min(1, 2, -1)) # print(min([4, 5, -1])) # print(max([4, 5, -1]))
#数据结构相关 #列表和元祖 # list() #将可迭代对象转换为列表 # tuple() #将可迭代对象转换为元组 # reversed() #将一个序列翻转,返回翻转序列的迭代器 # slice() #列表切片 # ret = reversed([1, 3, 5, 7, 9]) # print(ret.__next__()) # a = [11, 22, 33, 44] # s = slice(1, 3) #11,33 # print(a[s])
#字符串相关 # str() #将数据转换为字符串 # format() #与具体数据有关,用于计算各种小数,精算等 #字符串 # print(format('test', '<20')) #左对齐 # print(format('test', '>20')) #右对齐 # print(format('test', '^20')) #居中
#数值 # print(format(3, 'b')) #转换为二进制 # print(format(97, 'c')) #转换Unicode编码 # print(format(11, 'd')) #转换为十进制 # print(format(11, 'n')) #转换为十进制 # print(format(121)) #以十进制输出 # print(format(11, 'o')) #转换为八进制 # print(format(11, 'x')) #转换为十六进制(小写字母) # print(format(11, 'X')) #转换为十六进制(大写字母)
#浮点数 # print(format(123456789, 'e')) #科学计数法,默认保留6位小数 # print(format(123456789, '0.2e')) #科学计数法,保留2位小数(小写) # print(format(123456789, '0.2E')) #科学计数法,保留2位小数(大写) # print(format(123456789, 'f')) #小数点计数法,默认保留6位小数 # print(format(123456789, '0.2f')) #小数点计数法,保留2位小数 # print(format(123456789, '0.10f')) #小数点计数法,保留10位小数 # print(format(123456789, 'F')) #小数点计数法,默认保留6位小数
# bytes() #把字符串转换为bytes类型 # s = "测试" # bs = s.encode("UTF-8") # print(bs) # s1 = bs.decode("UTF-8") # print(s1) # bs = bytes(s, encoding="UTF-8") # print(bs)
# bytearray() #返回一个新字节数组,这个数组元素是可变的,而且每个元素的取值范围是[0, 256] # ret = bytearray('ropon', encoding='UTF-8') # for i in range(len(ret)): # print(ret[i])
# memoryview() #查看bytes在内存中的情况 # s = memoryview("再次测试".encode("UTF-8")) # a = '5' # s1 = memoryview(a.encode("UTF-8")) # print(s) # print(s1)
# ord() #返回字符编码 # chr() #返回对应编码的字符 # ascii() #在ASCII编码中就返回该值,不在就返回Unicode # print(ord('a')) # print(ord('中')) # print(chr(98)) # print(ascii('a')) # print(ascii('我们'))
# repr() #返回一个对象的string形式 # print(repr('哈喽,\n \t 我是Ropon')) # print('哈喽,\n \t 我是Ropon')
#数据集合 # dict() #创建一个字典 # set() #创建一个集合 # frozenset() #创建一个冻结的集合,冻结的集合不能进行添加或者删除
# 其他相关 # len() #返回对象中元素的个数 # sorted() #对可迭代的对象进行排序,配合lambda # enumerate() #获取集合的枚举对象 # a = {'cpu', 'mem', 'os', 'os_size', 'data_size'} # ret =enumerate(a) # print(ret.__next__()) # print(ret.__next__())
# lst = ["cpu", "mem", "os"] # for index, el in enumerate(lst): # print(str(index)+"==>"+el) #all() #可迭代对象中全部为True,结果才问True #any() #可迭代对象中有一个为True,结果就是True #zip() #将可迭代对象作为参数,将对象中对应的元素打包成一个个元组 # l1 = [1, 2, 3, ] # l2 = ['a', 'b', 'c', 5] # l3 = ('*', '**', (1, 2 ,3)) # for i in zip(l1, l2, l3): # print(i) #filter() #过滤,配合lambda #map() #根据提供的函数对指定序列做映射,配合lambda
|