Python内置函数一

Python内置函数

链接:

思维导图链接:

https://www.processon.com/view/link/5b72a704e4b053a09c33f341

 不好记得几个

hash()

lis= ("风到这里","就是黏","林俊杰","江南")
print(hash(lis))  #为了存储, 计算后得到一个数字   hash值尽量的不要

dir()

print(dir(str))


字符串的所有方法
['__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', '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']

 

bin()  oct()    hex()

1 print(bin(5))  # 5的二进制\
2 print(oct(12)) # 12的八进制
3 print(hex(20)) #  20的十六进制
4 
5 
6 结果
7 0b101
8 0o14
9 0x14

 

abs()   divmod()   round()   pow()

 1 print(abs(-8))   # 绝对值
 2 print(divmod(10,3))  # 返回商和余数
 3 print(round(4.51561))  # 四舍五入  后面不写 默认取整   后面写几 就保留几位小数,然后四舍五入
 4 print(round(4.5156,3))  # 后面写几 就保留几位小数,然后四舍五入
 5 print(pow(2,3)) # == 2**3   求次幂
 6 print(pow(2,3,3)) #   第三个参数  计算余数
 7 
 8 
 9 结果
10 8
11 (3, 1)
12 5
13 4.516
14 8

 

sum()   min()   max()

1 print(sum([1,2,3,4,5,6,7,8]))  #求和
2 print(min(1,2,3,4,5,6))  #求最小值 
3 print(max(1,2,3,4,5,6,7,8))  # 求最大值

 

reversed()

lis = ["马云",'捏你','还玩','你饿了','我饿了']
ll = reversed(lis)
print(list(ll))


结果

['我饿了', '你饿了', '还玩', '捏你', '马云']

 

slice()

lis = ["马云",'捏你','还玩','你饿了','我饿了']
s = slice(1,3,2) # == lis[1,3,2]
print(lis[s])

结果
['捏你']

 

memoryview()

s = "就是看了你一眼就已确定了永远"
print(memoryview(s.encode("utf-8"))) # 还不如id()


<memory at 0x0000015DCBA55F48>

 

ord()  chr()

1 print(ord("a"))  # 查看字母a的编码位置 97
2 print(ord('A')) #
3 print(chr(65))  #  查看65的位置编码是什么
4 
5 结果
6 97
7 65
8 A

 

转义字符 \

print("周杰伦说:\"昆凌特别难看\"") # \" 转义. 不让"作为字符串的开头或者结尾
print("哈哈\\\呵呵") # \\ 表示的是一个\    \ 转义. \n换行 \t制表符 \", \', \\
print(repr("周杰伦说:\n\t昆凌特别难看\t"))  # 对象的规范字符串表示形式???????


结果
 周杰伦说:"昆凌特别难看"
哈哈\\呵呵
'周杰伦说:\n\t昆凌特别难看\t'

 

formart()

s = 'bfhbf'
print(format(s ,'^20'))  # 居中
print(format(s,">20"))  # 右对齐
print(format(s,"<20"))  # 左对齐

解果

bfhbf        
               bfhbf
bfhbf               

 

 1 print(format(3,'b')) # 二进制
 2 print(format(10,'o'))  # 八进制
 3 print(format(19,'d'))  # 十进制  本身
 4 print(format(15,'x'))  # 十六进制  小写字母
 5 print(format(15,'X'))  #  十六进制  大写字母
 6 print(format(19,'n')) #  自己 和 d 一样
 7 
 8 结果
 9 11
10 12
11 19
12 f
13 F
14 19

 

 1 print(format(12345679,'e')) # 科学计数法. 默认保留6位⼩数
 2 print(format(123465789,'0.2e'))  # 科学计数法. 保留2位小数(小写)
 3 print(format(12345679,'0.2E'))  # 科学计数法. 保留2位⼩数(⼤写)
 4 print(format(1.2345679,'f'))  # ⼩数点计数法. 保留6位⼩数
 5 print(format(1.2345679,'0.2e'))  # ⼩数点计数法. 保留2位⼩数
 6 print(format(1.2345679,'0.10e')) # ⼩数点计数法. 保留10位⼩数
 7 print(format(12345679e10000,'F'))  # ⼩数点计数法. INF 无穷大
 8 
 9 结果
10 1.234568e+07
11 1.23e+08
12 1.23E+07
13 1.234568
14 1.23e+00
15 1.2345679000e+00
16 INF

 

frozenset()

1 s = frozenset({"战狼2", "我不是药神", "西虹市首富", "捉妖记"})
2 print(hash(s)) # 可哈希, 不可变

 

all()  与  and   一样         any  与  or 一样

print(all([1,0,0,True]))

结果
False

print(any([1,0,0,True]))

结果
True

 

zip 压缩  与对应的列表长度分开放在一起

lst1 = ["甜蜜蜜", "往事只能回味", "难忘今宵", "分红的回忆", "十年"]
lst2 = ["邓丽君", "韩宝仪", "李谷一", "王宝强", "陈奕迅"]
lst3 = ["2000","3000","5","120","50000"]
a = zip(lst1, lst2,lst3) # 水桶效应
print("__iter__" in dir(a)) # 可迭代的
for el in a:
    print(el)

结果
('甜蜜蜜', '邓丽君', '2000')
('往事只能回味', '韩宝仪', '3000')
('难忘今宵', '李谷一', '5')
('分红的回忆', '王宝强', '120')
('十年', '陈奕迅', '50000')

 

exec()执行代码         eval() 有返回值

 1 s = "5+6+4"
 2 a = eval(s)  #有返回值   动态执行一个代码片段
 3 print(a)
 4 
 5 a = "{'name':'汪峰', 'age':'48', 'wife':{'name':'国际章','age':'38'}}"   # 像字典一样的  字符串
 6 s = eval(a)   # 返回字典
 7 print(s)
 8 
 9 s = 'a = 10'
10 exec(s)  #  执行代码
11 print(a)  # pycharm报错不一定是错的

 

compile()

参数说明:
#  1. resource 要执⾏的代码, 动态代码⽚段
#  2. ⽂件名, 代码存放的⽂件名, 当传⼊了第⼀个参数的时候, 这个参数给空就可以了
#  3. 模式, 取值有3个,
#  1. exec: ⼀般放⼀些流程语句的时候
#  2. eval: resource只存放⼀个求值表达式.
#  3. single: resource存放的代码有交互的时候. mode应为single

 

s = "5+9"
c = compile(s, "", "eval")
ret = eval(c)
print(ret)

s = 'for i in range(10): print(i)'
a = compile(s,'','exec')
exec(a)

s = 'a = input("请输入你的名字:")'
c = compile(s,"",'single')  # 编译
exec(c)
print(a)

 

posted @ 2018-08-14 17:54  heshun  阅读(203)  评论(0)    收藏  举报