一. python内置函数(python3.X)
abs()求数字的绝对值
print(abs(-123)) >>>123
all() 都为真为True 所有元素都为 空 也为True 有一个是假就是False
print(all('')) >>>True ll = ["",None,"xixi"] print(all(ll)) >>>False
any()与上面相反 都为假时为False 有一个 是真就是True
1 print(any('')) 2 >>>False 3 ll = ["",None,"xixi"] 4 print(any(ll)) 5 >>>True
bool() 布尔值的判断
假的有:None , 0 ,"" , [] ,{:}, () ,{} 其余的都是真的。
chr()按照 ASCII 编号 返回对应的字符
1 print(chr(88)) >>>X
ord() 按照 ASCII 的字符 返回对应的编号
1 print(ord("X")) >>>88
bin() 将10进制转成 2 进制
1 print(bin(12)) >>>0b1100 #ob 是2进制的标志
oct() 将10进制转成 8 进制
print(oct(12)) >>>0o14 #0o 代表这是8进制
hex() 将10进制转成 16 进制
print(hex(12)) >>>0xc #0x 表示这是16进制
int() 转成10进制的整数
print(int("111",base=2)) #2进制转10进制 >>>7 print(int("567",base=8)) #8进制转10进制 >>>375 print(int("aa",base=16)) #16进制转10进制 >>>170 print(int(10.123)) #转整数 >>>10
dir() 不带参数时,返回当前范围内的变量,方法和定义的类型列表,带参数时,返回参数属性和方法列表
dir() >>>['__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'aa', 'll'] dir(str) >>>['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__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']
help() 查看某个函数详细信息,及用法
divmod() 分别获取商数和余数 3是商 1是余数
print(divmod(10,3)) >>>(3, 1)
enumerate() 给一个可迭代对象加上序号,默认从0开始
1 li=["hello","world","alex"] 2 for i in enumerate(li,4): 3 print(i) >>> (4, 'hello') (5, 'world') (6, 'alex')
eval() 转成原本的类型
print(eval("12"+"12")) >>>1212 print(eval("12+12")) >>>24
filter()可以对函数做过滤
id() 返回一个内存地址-
len() 返回一个对象的长度
1 l=[1,2,3,4,5,6] 2 print(len(l)) >>>6
frozenset() 冻结列表,让其不允许修改
1 l=[1,2,3,4,5,6] 2 v=frozenset(l) 3 print(v) # v 不会被修改 >>>frozenset({1, 2, 3, 4, 5, 6})
map() 遍历序列,对序列中每个元素进行操作,最终获取新的序列
li = [2,3,4,5,6] print(list(map(lambda x:x*2,li))) >>>[4, 6, 8, 10, 12]
range() 产生一个数字系序列
1 print(range(20)) 2 >>>range(0, 20) 3 print(range(10,20)) 4 >>>range(10, 20)
reversed() 反转
1 l=[48,46,4,15,64,6] 2 v=reversed(l) 3 print(list(v)) 4 >>>[6, 64, 15, 4, 46, 48]
round() 四舍五入
1 print(round(4.6)) 2 >>>5 3 print(round(4.4)) 4 >>>4
sorted() 排序
1 l=[6,46,165,1,616] 2 v=sorted(l) 3 print(v) 4 >>>[1, 6, 46, 165, 616]
sum() 给一组数字求和
print(sum([6,651,651])) >>>1308
type() 查看对象的类型
print(type("hello world")) >>><class 'str'>
vars() 返回对象的变量 跟一个列表或多个字典
def test(): msg='撒旦法阿萨德防撒旦浪费艾丝凡阿斯蒂芬' print(locals()) #打印出上一层的值,如果上一层没有,再往上找 print(vars()) #如果没有参数,跟locals一样,如果有参数,查看某一个方法,显示成字典的方式 test() print(vars(int))
执行结果:
{'msg': '撒旦法阿萨德防撒旦浪费艾丝凡阿斯蒂芬'} {'msg': '撒旦法阿萨德防撒旦浪费艾丝凡阿斯蒂芬'} {'denominator': <attribute 'denominator' of 'int' objects>, '__mod__': <slot wrapper '__mod__' of 'int' objects>, '__radd__': <slot wrapper '__radd__' of 'int' objects>, '__floordiv__': <slot wrapper '__floordiv__' of 'int' objects>, '__doc__': "int(x=0) -> integer int(x, base=10) -> integer Convert a number or string to an integer, or return 0 if no arguments are given. If x is a number, return x.__int__(). For floating point numbers, this truncates towards zero. If x is not a number or if base is given, then x must be a string, bytes, or bytearray instance representing an integer literal in the given base. The literal can be preceded by '+' or '-' and be surrounded by whitespace. The base defaults to 10. Valid bases are 0 and 2-36. Base 0 means to interpret the base from the string as an integer literal. >>> int('0b100', base=0) 4", '__ceil__': <method '__ceil__' of 'int' objects>, '__sizeof__': <method '__sizeof__' of 'int' objects>, '__pos__': <slot wrapper '__pos__' of 'int' objects>, '__gt__': <slot wrapper '__gt__' of 'int' objects>, '__rtruediv__': <slot wrapper '__rtruediv__' of 'int' objects>, '__sub__': <slot wrapper '__sub__' of 'int' objects>, '__rdivmod__': <slot wrapper '__rdivmod__' of 'int' objects>, '__new__': <built-in method __new__ of type object at 0x5D677028>, '__rshift__': <slot wrapper '__rshift__' of 'int' objects>, '__rmod__': <slot wrapper '__rmod__' of 'int' objects>, '__neg__': <slot wrapper '__neg__' of 'int' objects>, '__xor__': <slot wrapper '__xor__' of 'int' objects>, '__rmul__': <slot wrapper '__rmul__' of 'int' objects>, '__repr__': <slot wrapper '__repr__' of 'int' objects>, '__hash__': <slot wrapper '__hash__' of 'int' objects>, 'to_bytes': <method 'to_bytes' of 'int' objects>, 'from_bytes': <method 'from_bytes' of 'int' objects>, 'real': <attribute 'real' of 'int' objects>, '__lt__': <slot wrapper '__lt__' of 'int' objects>, '__invert__': <slot wrapper '__invert__' of 'int' objects>, '__eq__': <slot wrapper '__eq__' of 'int' objects>, '__float__': <slot wrapper '__float__' of 'int' objects>, '__round__': <method '__round__' of 'int' objects>, '__ror__': <slot wrapper '__ror__' of 'int' objects>, '__le__': <slot wrapper '__le__' of 'int' objects>, '__rlshift__': <slot wrapper '__rlshift__' of 'int' objects>, 'bit_length': <method 'bit_length' of 'int' objects>, '__getnewargs__': <method '__getnewargs__' of 'int' objects>, '__index__': <slot wrapper '__index__' of 'int' objects>, '__rsub__': <slot wrapper '__rsub__' of 'int' objects>, '__format__': <method '__format__' of 'int' objects>, '__bool__': <slot wrapper '__bool__' of 'int' objects>, '__or__': <slot wrapper '__or__' of 'int' objects>, '__int__': <slot wrapper '__int__' of 'int' objects>, 'imag': <attribute 'imag' of 'int' objects>, 'conjugate': <method 'conjugate' of 'int' objects>, '__ge__': <slot wrapper '__ge__' of 'int' objects>, '__and__': <slot wrapper '__and__' of 'int' objects>, '__abs__': <slot wrapper '__abs__' of 'int' objects>, '__floor__': <method '__floor__' of 'int' objects>, '__divmod__': <slot wrapper '__divmod__' of 'int' objects>, '__trunc__': <method '__trunc__' of 'int' objects>, '__rrshift__': <slot wrapper '__rrshift__' of 'int' objects>, '__mul__': <slot wrapper '__mul__' of 'int' objects>, '__pow__': <slot wrapper '__pow__' of 'int' objects>, '__str__': <slot wrapper '__str__' of 'int' objects>, '__ne__': <slot wrapper '__ne__' of 'int' objects>, '__getattribute__': <slot wrapper '__getattribute__' of 'int' objects>, '__truediv__': <slot wrapper '__truediv__' of 'int' objects>, '__add__': <slot wrapper '__add__' of 'int' objects>, '__rand__': <slot wrapper '__rand__' of 'int' objects>, '__rfloordiv__': <slot wrapper '__rfloordiv__' of 'int' objects>, '__lshift__': <slot wrapper '__lshift__' of 'int' objects>, '__rxor__': <slot wrapper '__rxor__' of 'int' objects>, 'numerator': <attribute 'numerator' of 'int' objects>, '__rpow__': <slot wrapper '__rpow__' of 'int' objects>}
zip() 可接受任意参数 , 返回一个一 一对应的元组
1 l1 = ["alex", 22, 33, 44, 55] 2 l2 = ["is", 22, 33, 44, 55] 3 l3 = ["good", 22, 33, 44, 55] 4 l4 = ["guy", 22, 33, 44, 55] 5 print( "_".join(list(zip(l1,l2,l3,l4))[0])) >>>alex_is_good_guy
pow(x,y,z) x的y次方 除以 z 取余
1 print(pow(3,3)) 次方 2 >>>27 3 print(pow(3,3,2)) 取余数 4 >>>1
import模块
1. 先创建一个test.py文件
写入内容如下:
1 def say_hi(): 2 print('你好啊林师傅')
2. 再调用这个模块
1 import test #导入一个模块,模块就是一个py文件 2 test.say_hi()
执行结果 >>> 你好啊林师傅
__import__ :导入一个字符串类型模块,就要用__import__
1. 先创建一个test.py文件
写入内容:
1 def say_hi(): 2 print('你好啊林师傅')
2. 再调用这个模块
1 module_name='test' 2 m=__import__(module_name) #有字符串的模块 3 m.say_hi()
执行结果>>> 你好啊林师傅