标签:
# abs() :取绝对值 >>> res = abs(-10) >>> print(res) 10
# 0,None,"",{},[],() 布尔值都为空 # all(),any() 判断值的真假 # all() 所有为真才为真 p = all([0,1,2,3]) # 0为False,所有p 是False print(p) # any() 任意一个为真即为真 p = any((0,"",[],1)) print(p)
# 进制转换 # bin() 十进制转换成二进制 >>> a = 10 >>> print(bin(a)) 0b1010 # int() 十进制转换成十进制 >>> a = 10 >>> print(int(a)) 10 # oct() 十进制转换成八进制 >>> a = 10 >>> print(oct(a)) 0o12 # hex() 十进制转换成十六进制 >>> a = 18 >>> print(hex(a)) 0x12
# bool()真假值判断 >>> a = 0 >>> bool(a) False >>> a = 1 >>> bool(a) True
# float() 浮点数 # round(float, digits)接受一个浮点数 flt 并对其四舍五入,ndig位小数。 ret = 10 / 3 ret = float(ret) ret = round(ret, 2) #保留两位小数 print(ret) 3.33
# [重要] 字符串转换成字节,用bytes() name = "谢鹏程" n = bytes(name, encoding="utf-8") print(n) n = bytes(name, encoding="gbk") print(n)
# 查询数字对应的ASCII码表内的值 >>> a = chr(65) >>> print(a) A # 查询字符对应的ASCII码表的位置 >>> a = ord("A") >>> print(a) 65 可以用来生成随机字符串(随机密码),示例:
# 随机字符串生成 import random li = [] for i in range(16): # 33 <= i < 127 temp = random.randrange(33, 127) c = chr(temp) li.append(c) res = "".join(li) print(res) # 随机大写字母和数字 li = [] for i in range(16): r = random.randrange(0, 3) if r == 1 or r == 2: temp = random.randrange(0, 10) li.append(str(temp)) else: # 33 <= i < 127 temp = random.randrange(65, 91) c = chr(temp) li.append(c) res = "".join(li) print(res)
# dir() 快速查看一个对象提供了哪些功能 res = dir(dict) print(res) #help()详细功能展示 res = help(dict) print(res)
# [重要] divmod() 做除法,用元组的形式返回商和余数; >>> a = 10 >>> b = 3 >>> divmod(a, b) (3, 1) 可用于内容的分页展示:(共97条记录,每页展示10条,共需要多少页),示例: total = 97 per = 10 temp_tuple = divmod(total, per) if temp_tuple[1] == 0: ret = temp_tuple[0] else: ret = temp_tuple[0] + 1 print(ret)
# enumerate(迭代器) 枚举,用列表的形式返回索引和值 a = [11,22,33,44,[1,2,3,4,]] ret = enumerate(a) print(list(ret)) [(0, 11), (1, 22), (2, 33), (3, 44), (4, [1, 2, 3, 4])] #或者 a = [11,22,33,44,[1,2,3,4,]] for index, value in enumerate(a): print(index, value)
0 11 1 22 2 33 3 44 4 [1, 2, 3, 4]
# eval 只执行表达式,并获取结果,有返回值 s = "1+2*3" res = eval(s) print(res) 7 # 将字符串编译成python代码 #compile() string = "print(123)" a = compile(string, "<string>", "exec") print(type(a)) <class ‘code‘> # 执行python代码,接收代码或者字符串,字符串时先编译成代码再执行,默认包含compile() #exec() # 没有返回值 string = "print(123)" exec(string) print(exec(string)) # 没有返回值,默认是None 123 None
# [重要] #filter(def, 可迭代对象) 做筛选; # map(def, 可迭代对象) 做操作. li = [1,2,3,4,5,6,7,8,9,10,1,2,3] # def f1(args): # if args > 6: # return True # ret = filter(f1, li) #返回的是一个列表 ret = filter(lambda a: a > 6, li) print(list(ret)) #map() li = [1,2,3,4,5,6,7,8,9,10,1,2,3] ret = map(lambda a: a + 100, li) print(list(ret))
# id() 获取对象的内存地址 a = [1,2,3,4,] print(id(a))
4315410184
# [重要]判断对象是否是某个类的实例,返回True or False s = "xie" res = isinstance(s, str) # 判断变量s是否为字符串 print(res) True
# len() 返回长度 a = "Pesen" print(len(a)) 5
# globals() 返回当前范围的全局变量,字典形式;locals() 返回当前范围所有的局部变量 NAME = "Pesen" def f1(): sec_name = "Xie" print(locals()) print(globals()) f1() {‘sec_name‘: ‘Xie‘} {‘__builtins__‘: <module ‘builtins‘ (built-in)>, ‘__name__‘: ‘__main__‘, ‘__package__‘: None, ‘f1‘: <function f1 at 0x10127b8c8>, ‘__spec__‘: None, ‘__file__‘: ‘/Users/xiepengcheng/PycharmProjects/LearnPython/day4/temp.py‘, ‘__cached__‘: None, ‘NAME‘: ‘Pesen‘, ‘__loader__‘: <_frozen_importlib_external.SourceFileLoader object at 0x1010a5c88>, ‘__doc__‘: None}
# min() 、max()、sun()取最小值,最大值,求和 li = [1,2,3,4,5,6,7,3,1,5] print(min(li), max(li), sum(li)) 1 7 37
# pow(x, y, z) 两个参数是计算x的y次方;三个参数是计算 x**y % z a = pow(2, 4) print(a) 16 a = pow(2, 4, 2) print(a) 0
# zip() 接受任意多个(包括0个和1个)序列作为参数,返回一个tuple列表 x = [1, 2, 3] y = [4, 5, 6] z = [7, 8, 9] xyz = zip(x, y, z) print(list(xyz)) [(1, 4, 7), (2, 5, 8), (3, 6, 9)]
# python3.5 按照字符来遍历 >>> for i in "谢鹏程": ... print(i) 谢 鹏 程 # python2.7 #按照字节 >>> a = "谢鹏程" >>> print a[:3], a[3:6], a[6:9] 谢 鹏 程
标签:
原文地址:http://www.cnblogs.com/pesen/p/5544389.html