1、dir(random) #输出模块/变量可以调用的方法,返回的类型是list
import random s='456' print(dir(random)) #模块可以用的方法 print(" ============== ") print(dir(s)) #变量可以调用的方法 /Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7 /Users/dongyf/Documents/python/besttest_study/cnz/ycc/a.py ['BPF', 'LOG4', 'NV_MAGICCONST', 'RECIP_BPF', 'Random', 'SG_MAGICCONST', 'SystemRandom', 'TWOPI', '_BuiltinMethodType', '_MethodType', '_Sequence', '_Set', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_acos', '_bisect', '_ceil', '_cos', '_e', '_exp', '_inst', '_itertools', '_log', '_os', '_pi', '_random', '_sha512', '_sin', '_sqrt', '_test', '_test_generator', '_urandom', '_warn', 'betavariate', 'choice', 'choices', 'expovariate', 'gammavariate', 'gauss', 'getrandbits', 'getstate', 'lognormvariate', 'normalvariate', 'paretovariate', 'randint', 'random', 'randrange', 'sample', 'seed', 'setstate', 'shuffle', 'triangular', 'uniform', 'vonmisesvariate', 'weibullvariate'] ============== ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill'] Process finished with exit code 0
2、sorted() 排序,排序后变成list,正序排列
l=[1,2,3,7,4,5] #list t=(1,2,8,3,5,6) #tuple s={1,3,2,4} #set s2="12423" #str d={"a":1,"c":2,"b":3} #字典 print(sorted(l)) print(sorted(t)) print(sorted(s)) print(sorted(s2)) print(sorted(d)) /Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7 /Users/dongyf/Documents/python/besttest_study/cnz/ycc/a.py [1, 2, 3, 4, 5, 7] [1, 2, 3, 5, 6, 8] [1, 2, 3, 4] ['1', '2', '2', '3', '4'] ['a', 'b', 'c']
3、reversed反转,返回的是类,迭代器
备注:只能用于字符串,元组,列表,不能用于集合,字典类型
l = [1, 2, 3, 7, 4, 5] # list t = (1, 2, 8, 3, 5, 6) # tuple s2 = "12423" # str a = list(reversed(l)) b = tuple(reversed(t)) c = ''.join(reversed(s2)) print(a) print(b) print(c) /Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7 /Users/dongyf/Documents/python/besttest_study/cnz/ycc/a.py [5, 4, 7, 3, 2, 1] (6, 5, 3, 8, 2, 1) 32421
迭代器
s2 = "12423" # str for i in reversed(s2): #返回的迭代器循环进行输出 print(i) /Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7 /Users/dongyf/Documents/python/besttest_study/cnz/ycc/a.py 3 2 4 2 1
4、all([1,2,3,4]) 判断可迭代的对象里面的值是否都是真,非0即真
备注:该方法仅用于列表和元组
print(all([1,2,3,4])) #列表 print(all((1,2,0,3))) #元组 print(all([])) #空列表 print(all(())) #空元组 /Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7 /Users/dongyf/Documents/python/besttest_study/cnz/ycc/a.py True False True True
5、any([0,1,2]) 判断可迭代的对象里面的值是否有一个为真,非空即真
备注:可迭代的对象可以是list,元组,字符串,集合,字典任意一种
print(any([0,1,2])) #对象不为空 True print(any([])) #对象为空 False print(any((1,2))) #对象不为空 True print(any(())) #对象为空 False print(any({1,2})) #对象不为空 True print(any({})) #对象为空 False
6、bool('s') 把一个对象转换成布尔类型,非空即真
print(bool([0,1,2])) #对象不为空 True print(bool([])) #对象为空 False print(bool((1,2))) #对象不为空 True print(bool(())) #对象为空 False print(bool('s')) #对象不为空 True print(bool('')) #对象为空 False
7、chr(32) 打印数字的ascii码,可以传十六进制和十进制,十六进制加上0x
print(chr(42)) #十进制 print(chr(0x28)) #0x 代表十六进制 D:studypython estvenvScriptspython.exe D:/study/python/test/dd.py * (
8、ord(‘a’)打印字符串对于的ascii码,且输出的十进制
print(ord("*")) #输出*号对应得ASCII码 print(ord("a")) #输出a对应得ASCII码
9、bin(10) 十进制转换为二进制
print(bin(10)) #输出10对应得二进制 print(bin(2)) #输出2对应得二进制