内置函数
Posted 人无远虑
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了内置函数相关的知识,希望对你有一定的参考价值。
abs
取绝对值
all
print(all([1,-3,3]))#有不为0的就是真
1 C:\\Users\\Administrator\\PycharmProjects\\untitled3\\venv\\Scripts\\python.exe C:/Users/Administrator/PycharmProjects/2018-2-27/测试.py 2 True 3 4 Process finished with exit code 0
any
print(any([1,-3,0])) #有一个为真就为真
1 C:\\Users\\Administrator\\PycharmProjects\\untitled3\\venv\\Scripts\\python.exe C:/Users/Administrator/PycharmProjects/2018-2-27/测试.py 2 True 3 4 Process finished with exit code 0
ASCII
print(ascii([1,-3,\'大大勾拳\'])) #把内存的数据对象变成一个可打印的字符串
1 C:\\Users\\Administrator\\PycharmProjects\\untitled3\\venv\\Scripts\\python.exe C:/Users/Administrator/PycharmProjects/2018-2-27/测试.py 2 [1, -3, \'\\u5927\\u5927\\u52fe\\u62f3\'] 3 4 Process finished with exit code 0
bin
print(bin(255)) #把数字从十进制转二进制
1 C:\\Users\\Administrator\\PycharmProjects\\untitled3\\venv\\Scripts\\python.exe C:/Users/Administrator/PycharmProjects/2018-2-27/测试.py 2 0b11111111 3 4 Process finished with exit code 0
bool
print(bool([]))#判断真假,有数据就是真,没有数据就是假
1 C:\\Users\\Administrator\\PycharmProjects\\untitled3\\venv\\Scripts\\python.exe C:/Users/Administrator/PycharmProjects/2018-2-27/测试.py 2 False 3 4 Process finished with exit code 0
bytearry
b=bytearray(\'abcde\',encoding=\'utf-8\')#字节数组 可修改的二进制 print(b[1]) b[1]=50 #把二进制变成数组的格式,并且可以进行修改 print(b)
1 C:\\Users\\Administrator\\PycharmProjects\\untitled3\\venv\\Scripts\\python.exe C:/Users/Administrator/PycharmProjects/2018-2-27/测试.py 2 98 3 bytearray(b\'a2cde\') 4 5 Process finished with exit code 0
callable
def as1():pass print(callable([])) #判断是否可以被调用
1 C:\\Users\\Administrator\\PycharmProjects\\untitled3\\venv\\Scripts\\python.exe C:/Users/Administrator/PycharmProjects/2018-2-27/测试.py 2 False 3 4 Process finished with exit code 0
chr
print(chr(97)) #数字对应的asc码,参数必须是数字
1 C:\\Users\\Administrator\\PycharmProjects\\untitled3\\venv\\Scripts\\python.exe C:/Users/Administrator/PycharmProjects/2018-2-27/测试.py 2 a 3 4 Process finished with exit code 0
ord
print(ord(\'d\'))#asc码对应的数字,参数必须是字符
1 C:\\Users\\Administrator\\PycharmProjects\\untitled3\\venv\\Scripts\\python.exe C:/Users/Administrator/PycharmProjects/2018-2-27/测试.py 2 100 3 4 Process finished with exit code 0
complie
code =\'for i in range(10):print(i)\' #把一段字符串变成可以编译的代码 c=compile(code,\'\',\'exec\') exec(c) #直接exec()就可以,跟eval一样用,更简单
1 C:\\Users\\Administrator\\PycharmProjects\\untitled3\\venv\\Scripts\\python.exe C:/Users/Administrator/PycharmProjects/2018-2-27/测试.py 2 0 3 1 4 2 5 3 6 4 7 5 8 6 9 7 10 8 11 9 12 13 Process finished with exit code 0
exec
把一段字符串变成可以编译的代码
code =\'for i in range(10):print(i)\' #把一段字符串变成可以编译的代码 exec(code)
1 C:\\Users\\Administrator\\PycharmProjects\\untitled3\\venv\\Scripts\\python.exe C:/Users/Administrator/PycharmProjects/2018-2-27/测试.py 2 0 3 1 4 2 5 3 6 4 7 5 8 6 9 7 10 8 11 9 12 13 Process finished with exit code 0
eval
code =\'1+2*3\' #把一段字符串变成可以编译的代码,针对整数加减乘除
print(eval(code))
1 C:\\Users\\Administrator\\PycharmProjects\\untitled3\\venv\\Scripts\\python.exe C:/Users/Administrator/PycharmProjects/2018-2-27/测试.py 2 7 3 4 Process finished with exit code 0
divmod
print(divmod(5,1)) #返回商跟余数
1 C:\\Users\\Administrator\\PycharmProjects\\untitled3\\venv\\Scripts\\python.exe C:/Users/Administrator/PycharmProjects/2018-2-27/测试.py 2 (5, 0) 3 4 Process finished with exit code 0
lambda
匿名函数
def sayhi(n): print(n) sayhi(5) cal=lambda n :print(n) #lambda 就是匿名函数,只能处理简单的 cal(5) #或者简单的三元运算 cal= lambda n:3 if n<4 else n print(cal(2)) #需要配合filter使用,filter是过滤出自己想要的
#也可配合map使用
1 C:\\Users\\Administrator\\PycharmProjects\\untitled3\\venv\\Scripts\\python.exe C:/Users/Administrator/PycharmProjects/2018-2-27/测试.py 2 5 3 5 4 3 5 6 Process finished with exit code 0
filter
过滤自己想要的数据,配合lambda使用
res=filter(lambda n:n>5,range(10)) for i in res: print(i)
1 C:\\Users\\Administrator\\PycharmProjects\\untitled3\\venv\\Scripts\\python.exe C:/Users/Administrator/PycharmProjects/2018-2-27/测试.py 2 6 3 7 4 8 5 9 6 7 Process finished with exit code 0
map
把传入的每个值进行处理,再返回一个列表
res=map(lambda n:n*n,range(10)) ###filter是吧过滤出来的合格的打印出来,map是把传入的每一个值进行处理,再返回一个列表 for i in res: print(i)
1 C:\\Users\\Administrator\\PycharmProjects\\untitled3\\venv\\Scripts\\python.exe C:/Users/Administrator/PycharmProjects/2018-2-27/测试.py 2 0 3 1 4 4 5 9 6 16 7 25 8 36 9 49 10 64 11 81 12 13 Process finished with exit code 0
reduce
import functools res=functools.reduce(lambda x,y:x+y,range(10)) #累加,把传入的值,进行处理,然后将结果累加起来 print(res)
1 C:\\Users\\Administrator\\PycharmProjects\\untitled3\\venv\\Scripts\\python.exe C:/Users/Administrator/PycharmProjects/2018-2-27/测试.py 2 45 3 4 Process finished with exit code 0
frozenset
冻结不可变的集合
a= frozenset([1,4,33,212,33,35]) #set是有添加,清楚功能的,forozenset 没有这些功能了
globals
print(globals()) #返回的是当前整个程序里所以的key,valve格式,变量名是key,对应的变量是valve
1 C:\\Users\\Administrator\\PycharmProjects\\untitled3\\venv\\Scripts\\python.exe C:/Users/Administrator/PycharmProjects/2018-2-27/测试.py 2 {\'__name__\': \'__main__\', \'__doc__\': None, \'__package__\': None, \'__loader__\': <_frozen_importlib_external.SourceFileLoader object at 0x0045C490>, \'__spec__\': None, \'__annotations__\': {}, \'__builtins__\': <module \'builtins\' (built-in)>, \'__file__\': \'C:/Users/Administrator/PycharmProjects/2018-2-27/测试.py\', \'__cached__\': None} 3 4 Process finished with exit code 0
hash
中文叫散列 通关一系列算法,把无序的数据转换成有映射关系的有序数据,就是哈希
hex
把一个数字转成16进制
id
返回内存地址
iter
迭代器
max
返回最大值
next
跟迭代器的__next__方法一样
oct
转八进制
print(oct(8))#oct 转八进制
1 C:\\Users\\qiangql\\AppData\\Local\\Programs\\Python\\Python36-32\\python.exe C:/Users/qiangql/PycharmProjects/untitled/内置函数测.py 2 0o10 3 4 Process finished with exit code 0
pow
print(pow(2,8)) #多少次方
1 C:\\Users\\qiangql\\AppData\\Local\\Programs\\Python\\Python36-32\\python.exe C:/Users/qiangql/PycharmProjects/untitled/内置函数测.py 2 256 3 4 Process finished with exit code 0
range
循环
repr
用字符串表示对象
reversed
反转,类似列表的用法
round
print(round(1.3342,3)) #保留小数点后几位,参数可以调
1 C:\\Users\\qiangql\\AppData\\Local\\Programs\\Python\\Python36-32\\python.exe C:/Users/qiangql/PycharmProjects/untitled/内置函数测.py 2 1.334 3 4 Process finished with exit code 0
slice
d=range(10) d=list(d) print(d[slice(2,5)]) #切片
1 C:\\Users\\qiangql\\AppData\\Local\\Programs\\Python\\Python36-32\\python.exe C:/Users/qiangql/PycharmProjects/untitled/内置函数测.py 2 [2, 3, 4] 3 4 Process finished with exit code 0
sorted
把字典排序
a={1:3,2:11,99:3,-2:13} print(sorted(a)) #默认key print(sorted(a.items())) print(sorted(a.items(),key=lambda x:x[1])) #a按照valve排序,valve不能有字符串
1 C:\\Users\\qiangql\\AppData\\Local\\Programs\\Python\\Python36-32\\python.exe C:/Users/qiangql/PycharmProjects/untitled/内置函数测.py 2 [-2, 1, 2, 99] 3 [(-2, 13), (1, 3), (2, 11), (99, 3)] 4 [(1, 3), (99, 3), (2, 11), (-2, 13)] 5 6 Process finished with exit code 0
sum
求和
vars
返回一个对象的所有属性名,没啥用
zip
拉链的意思,组合两个列表,以元组的形式显示。
a=[1,2,3,4,5] #如果a,b数量不一样,按照最少的来 b=[\'a\',\'b\',\'c\',\'d\',\'e\'] for i in zip(a,b): print(i) print(type(i))
1 C:\\Users\\qiangql\\AppData\\Local\\Programs\\Python\\Python36-32\\python.exe C:/Users/qiangql/PycharmProjects/untitled/内置函数测.py 2 (1, \'a\') 3 (2, \'b\') 4 (3, \'c\') 5 (4, \'d\') 6 (5, \'e\') 7 <class \'tuple\'>
__import__
调用一个程序文件,有用,例如
1 C:\\Users\\qiangql\\AppData\\Local\\Programs\\Python\\Python36-32\\python.exe C:/Users/qiangql/PycharmProjects/untitled/内置函数测.py 2 welcome to index page 3 username:qiangql 4 password1234 5 user has passed authentication 6 welcome to home page 7 waibu
直接调用了111的内容。
————————————————————————————————————————————————————————
以上是关于内置函数的主要内容,如果未能解决你的问题,请参考以下文章
C#-WebForm-★内置对象简介★Request-获取请求对象Response相应请求对象Session全局变量(私有)Cookie全局变量(私有)Application全局公共变量Vi(代码片段