1.abs() 取绝对值
2.all() 括号内有一个元素为假时返回False,都时真时返回True
print(all([1,2,3,''])) all() 括号内只能传入一个参数
运行结果:False
print(all('')) 注意此时运行结果为True
运行结果:True
3.any() 括号内有过一个为真则返回True
4.
(1)bin() 把十进制改为二进制
(2)hex() 十进制改为16进制
(3)oct() 十进制改为八进制
5.bool() 返回布尔值 ( 除 空字符串、0、None 其余布尔值都为True)
6.bytes() 把字符串转换为字节的形式,需要传入编码规则
name = '你好'
print(bytes(name,encoding = 'utf-8'))
print(bytes(name,encoding = 'utf-8').decode('utf-8')) 进行解码 (用什么编码,就应该用什么解码)
运行结果:
b'xe4xbdxa0xe5xa5xbd'
你好
7. (1)chr() 传入数字,返回该数字在ASCII表中对应的字母
print(chr(97))
运行结果:a
(2)ord() 传入字母,返回字母在ASCII表中对应的数字
8.dir() 打印某个对象都有那些方法
9.divmod(x,y) 返回 x/y 的 值及其余数
10.eval()
(1) 提取字符串中的数据结构,转换为字典
dic = {'name':'alex'} dic_str = str(dic) print(dic_str) d1 = eval(dic_str) print(d1) print(d1['name'])
运行结果:
{'name': 'alex'} 第一个时字符串 ‘{‘name’:‘alex’}’
{'name': 'alex'} 第二个是字典,可根据键进行索引
alex
(2)运行字符串中的数学运算
express = '1- 2 * (3-3) -2'
a = eval(express)
print(a)
运行结果:
-1
11.hash() 可hash的数据类型即不可变数据类型,不可hash的即为可变数据类型
12.help() 打印对象的使用方法
13.isinstance() 判断数据类型
print(isinstance(1,int))
print(isinstance('1',str))
运行结果:
True
True
14.globals()打印全局变量
name = 'hahahahah'
print(globals())
运行结果:{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x000001F7C4D93F98>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'F:/python/应用pycharm/python练习1/lianxi.py', '__cached__': None, 'name': 'hahahahah'}
15.locals() 打印局部变量
16.zip()括号内可传入两个(或两个以上)序列,一一对应生成一个内存地址
print(list(zip(('a','b','c'),(1,2,3))))
print((zip(('a','b','c'),(1,2,3))))
p = {'name':'alex','age':'18','gender':'none'}
print(list(zip(p.keys(),p.values()))) zip()括号内传入的应该是两个序列类型(元组、列表、字符串),
运行结果:
[('a', 1), ('b', 2), ('c', 3)]
<zip object at 0x0000025C28FEAE08> 返回一个内存地址,通过 list() 转换成列表,可通过for循环打印
[('name', 'alex'), ('age', '18'), ('gender', 'none')]
17.max() 、 min() 函数处理的是可迭代对象,相当于一个for循环取出每个元素进行比较,注意:不同类型之间不能比较
每个元素间进行比较,是从每个元素的第一个位置依次比较,如果这一个位置分出大小,后面的都不需要再比较了,直接得出这两个元素的大小关系
(2)dic = {‘age1’:‘18’,‘age2’:10} max(dic()) 返回字典中最大的key
(3)max(dic.values()) 返回字典中最大的值,但不知道其对应的key
(4)max(zip(dic.values(),dic.keys())) 可以返回最大的值和对应的key
(5)max(people,key = lambda dic:dic[‘age’]) 此段代码相当于 ret = [] for item in people: ret.append(item['age']) print(ret) max(ret)
people = [
{'name':'alex','age':100},
{'name':'wupeiqi','age':10000},
{'name':'yuanhao','age':9000},
{'name':'lihaifeng','age':18}]
print(max(people,key = lambda dic:dic['age'])) 此段代码的运行,通过key= lambda 将lambda定义的方法传递给 max 。 实际是max函数对people进行for循环,将每一个取出来的元素(即字典)按照lambda规定方法进行处理
运行结果:{'name': 'wupeiqi', 'age': 10000} 即每次传入一个字典,通过 lambda 得到此字典‘age’键对应的值,再进行比较
18.pow(x,y) 返回x的y次方
pow(x,y,z) x的y次方对z取余
19.reversed() 将字典反转,返回的是内存地址,可通过list进行提取
l = [1,2,3,4]
a = reversed(l)
print(a)
print(list(a))
运行结果:
<list_reverseiterator object at 0x0000024FA360B0B8>
[4, 3, 2, 1]
20.round() 对传入的值四舍五入
21.slice() 切片
l = 'hello'
s1 = slice(3,5)
s2 = slice(1,4,2) 设置步长为2
print(l[s1]) slice的用法
print(l[s2])
运行结果:
lo
el
22.sorted() 对传入的对象进行排序,不同类型不能排序 ,用法与max类似
people = [
{'name':'alex','age':100},
{'name':'wupeiqi','age':10000},
{'name':'yuanhao','age':9000},
{'name':'lihaifeng','age':18}]
print(sorted(people,key = lambda dic:dic['age'])) 对people按age大小排序
运行结果:
[{'name': 'lihaifeng', 'age': 18}, {'name': 'alex', 'age': 100}, {'name': 'yuanhao', 'age': 9000}, {'name': 'wupeiqi', 'age': 10000}]
将字典中元素按值的大小进行排序
name_dict = {'abyuanhao':11900,
'alex':1200,
'wupqiqi':300}
print(sorted(name_dict,key = lambda key :name_dict[key])) 与max类似,sorted对name_dict进行for循环(对字典的for循环是对其key进行循环),所以 lambda key:。。。中的key就是字典每个键值对的键
运行结果:['wupqiqi', 'alex', 'abyuanhao'] 进行for循环就是遍历每个元素,按照key= lambda定义的规则对每个元素进行处理,将返回值进行排序
输出对应的键值对
name_dict = {'abyuanhao':11900,
'alex':1200,
'wupqiqi':300}
print(sorted(zip(name_dict.values(),name_dict.keys())))
运行结果:[(300, 'wupqiqi'), (1200, 'alex'), (11900, 'abyuanhao')]
23.type() 判断对象的数据类型
24.vars() 当括号内没有参数的时候,作用与locals相;,当传入参数的时候将对象的所有方法按列表的形式打印出来
print(vars(int)) 运行结果:{'__repr__': <slot wrapper '__repr__' of 'int' objects>, '__hash__': <slot wrapper '__hash__' of 'int' objects>, '__str__': <slot wrapper '__str__' of 'int' objects>, '__getattribute__': <slot wrapper '__getattribute__' of 'int' objects>, '__lt__': <slot wrapper '__lt__' of 'int' objects>, '__le__': <slot wrapper '__le__' of 'int' objects>, '__eq__': <slot wrapper '__eq__' of 'int' objects>, '__ne__': <slot wrapper '__ne__' of 'int' objects>, '__gt__': <slot wrapper '__gt__' of 'int' objects>, '__ge__': <slot wrapper '__ge__' of 'int' objects>, '__add__': <slot wrapper '__add__' of 'int' objects>, '__radd__': <slot wrapper '__radd__' of 'int' objects>, '__sub__': <slot wrapper '__sub__' of 'int' objects>, '__rsub__': <slot wrapper '__rsub__' of 'int' objects>, '__mul__': <slot wrapper '__mul__' of 'int' objects>, '__rmul__': <slot wrapper '__rmul__' of 'int' objects>, '__mod__': <slot wrapper '__mod__' of 'int' objects>, '__rmod__': <slot wrapper '__rmod__' of 'int' objects>, '__divmod__': <slot wrapper '__divmod__' of 'int' objects>, '__rdivmod__': <slot wrapper '__rdivmod__' of 'int' objects>, '__pow__': <slot wrapper '__pow__' of 'int' objects>, '__rpow__': <slot wrapper '__rpow__' of 'int' objects>, '__neg__': <slot wrapper '__neg__' of 'int' objects>, '__pos__': <slot wrapper '__pos__' of 'int' objects>, '__abs__': <slot wrapper '__abs__' of 'int' objects>, '__bool__': <slot wrapper '__bool__' of 'int' objects>, '__invert__': <slot wrapper '__invert__' of 'int' objects>, '__lshift__': <slot wrapper '__lshift__' of 'int' objects>, '__rlshift__': <slot wrapper '__rlshift__' of 'int' objects>, '__rshift__': <slot wrapper '__rshift__' of 'int' objects>, '__rrshift__': <slot wrapper '__rrshift__' of 'int' objects>, '__and__': <slot wrapper '__and__' of 'int' objects>, '__rand__': <slot wrapper '__rand__' of 'int' objects>, '__xor__': <slot wrapper '__xor__' of 'int' objects>, '__rxor__': <slot wrapper '__rxor__' of 'int' objects>, '__or__': <slot wrapper '__or__' of 'int' objects>, '__ror__': <slot wrapper '__ror__' of 'int' objects>, '__int__': <slot wrapper '__int__' of 'int' objects>, '__float__': <slot wrapper '__float__' of 'int' objects>, '__floordiv__': <slot wrapper '__floordiv__' of 'int' objects>, '__rfloordiv__': <slot wrapper '__rfloordiv__' of 'int' objects>, '__truediv__': <slot wrapper '__truediv__' of 'int' objects>, '__rtruediv__': <slot wrapper '__rtruediv__' of 'int' objects>, '__index__': <slot wrapper '__index__' of 'int' objects>, '__new__': <built-in method __new__ of type object at 0x000000006A0B99A0>, 'conjugate': <method 'conjugate' of 'int' objects>, 'bit_length': <method 'bit_length' of 'int' objects>, 'to_bytes': <method 'to_bytes' of 'int' objects>, 'from_bytes': <method 'from_bytes' of 'int' objects>, '__trunc__': <method '__trunc__' of 'int' objects>, '__floor__': <method '__floor__' of 'int' objects>, '__ceil__': <method '__ceil__' of 'int' objects>, '__round__': <method '__round__' of 'int' objects>, '__getnewargs__': <method '__getnewargs__' of 'int' objects>, '__format__': <method '__format__' of 'int' objects>, '__sizeof__': <method '__sizeof__' of 'int' objects>, 'real': <attribute 'real' of 'int' objects>, 'imag': <attribute 'imag' of 'int' objects>, 'numerator': <attribute 'numerator' of 'int' objects>, 'denominator': <attribute 'denominator' 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"}
25. import 导入模块 ,并不能以字符串的形式导入模块
__import__ () 可以以字符串的形式导入模块