kezi

pythonr-内置函数

all 
print (all([1,-5,3])) 
print (all([0,-5,3])) 如果有0 就不为真,非0就是为真
打印结果
True    
Flase   


all
print(any([0,0,1]))   #有一个为真,他就为真
print(any([]))        #为空,他就是假
打印结果
True 
Flase 


bin 
print (bin(1))  #转换成2进制
print (bin(2))
print (bin(255))

打印结果
0b1
0b10
0b11111111

bool
print (bool(1))
print(bool(0))
print(bool([]))   #空列表为flase
print(bool([1]))

打印结果
True
False
False
True


bytearray  #可以修改字符串
a=bytes("abcde",encoding="utf-8")   #字符串不可以修改
print(a.capitalize(),a)

b=bytearray("abcde",encoding="utf-8")   #修改字符串
print(b[0])
b[0]=50
print(b)

打印结果
b\'Abcde\' b\'abcde\'
97
bytearray(b\'2bcde\')


def kezi():pass
print(callable(kezi))  可以调用函数
print(callable([]))   空为flase


打印结果
True
False


print (chr(98)) #须要输入数字,返回ASCII
print(ord(\'b\'))须要输入字符,返回数字


打印结果
b
98


dir
#查询出它可以使用的方法有哪些

a={1,2,3}
print(dir(a))

打印结果
[\'__and__\', \'__class__\', \'__contains__\', \'__delattr__\', \'__dir__\', \'__doc__\', \'__eq__\',
\'__format__\', \'__ge__\', \'__getattribute__\', \'__gt__\', \'__hash__\', \'__iand__\', \'__init__\',
\'__ior__\', \'__isub__\', \'__iter__\', \'__ixor__\', \'__le__\', \'__len__\', \'__lt__\', \'__ne__\',
\'__new__\', \'__or__\', \'__rand__\', \'__reduce__\', \'__reduce_ex__\', \'__repr__\', \'__ror__\',
\'__rsub__\', \'__rxor__\', \'__setattr__\', \'__sizeof__\', \'__str__\', \'__sub__\', \'__subclasshook__\',
\'__xor__\', \'add\', \'clear\', \'copy\', \'difference\', \'difference_update\', \'discard\', \'intersection\',
\'intersection_update\', \'isdisjoint\', \'issubset\', \'issuperset\', \'pop\', \'remove\', \'symmetric_difference\',
\'symmetric_difference_update\', \'union\', \'update\']

divmod
返回商和余数
print(divmod(5,3))
打印结果
(1, 2)

eval #简单的数字进行转换
exec #复杂的转换


匿名函数

def kezi(n):
print(n)
kezi(3)

打印结果
3


lambda
(lambda n:print(n))(4) 只能做三元运算,复杂了不行 lambda
kezi2=lambda n:print(n)
kezi2(5)
打印结果
4
5
kezi3=lambda n:3 if n<5 else n
print(kezi3(4))
打印结果
3

filter #过滤 与lambda 配合使用匿名函数
res=filter(lambda n:n>5 ,range(15))
for i in res:
print(i)

打印结果


rs=map(lambda n: n*3,range(10)) 与这个相同rs=[i*2 for i in rang(10)]
for i in rs:
print(i)

打印结果
0
3
6
9
12
15
18
21
24
27

 



import functools
rs=functools.reduce(lambda x,y: x+y,range(5)) #累加
print(rs)

打印结果
10


b=set ([1,2,33,4444,555,34,43,33])
print (b)

c=frozenset([1,2,33,4444,555,34,43,33]) #冻结
print(c)
打印结果

{1, 2, 34, 33, 555, 43, 4444}
frozenset({1, 2, 34, 33, 555, 43, 4444})

print(globals()) 返回整个程序的所有字典的key-value

hash
print (hash("abc"))    #这个值不会变,可以利用这个方法完成字典的查询,如折半算法查询,高效查询
print (hash("abc"))

打印结果
-6788241100703459257
-6788241100703459257

print(hex(200)) 返回16进制结果
打印结果
0xc8

 

def test():
local_var=333
print (locals())
print(globals())
test()
print(globals())
print(globals().get(\'local_var\'))

打印结果
{\'__package__\': None, \'test\': <function test at 0x000001E5FA070400>, \'__cached__\': None, \'__builtins__\': <module \'builtins\' (built-in)>, \'__file__\': \'G:/Users/Administrator/PycharmProjects/untitled/11-20/内置函数.py\', \'__loader__\': <_frozen_importlib_external.SourceFileLoader object at 0x000001E5FA00B780>, \'__spec__\': None, \'__name__\': \'__main__\', \'__doc__\': None}
{\'__package__\': None, \'test\': <function test at 0x000001E5FA070400>, \'__cached__\': None, \'__builtins__\': <module \'builtins\' (built-in)>, \'__file__\': \'G:/Users/Administrator/PycharmProjects/untitled/11-20/内置函数.py\', \'__loader__\': <_frozen_importlib_external.SourceFileLoader object at 0x000001E5FA00B780>, \'__spec__\': None, \'__name__\': \'__main__\', \'__doc__\': None}
{\'__package__\': None, \'test\': <function test at 0x000001E5FA070400>, \'__cached__\': None, \'__builtins__\': <module \'builtins\' (built-in)>, \'__file__\': \'G:/Users/Administrator/PycharmProjects/untitled/11-20/内置函数.py\', \'__loader__\': <_frozen_importlib_external.SourceFileLoader object at 0x000001E5FA00B780>, \'__spec__\': None, \'__name__\': \'__main__\', \'__doc__\': None}
None

print(oct(8))

打印结果
0o3

 

print(pow(3,5))

打印结果
243


print(round(33.333333)保留小数点位

打印结果
33


sorted

b={6:22,8:33,4:44,2:55} 字典是无序的
print(sorted(b)) key排序
print(sorted(b.items(), key=lambda x:x[1])) value排序
print(sorted(b.items()))
print(b)
打印结果
[2, 4, 6, 8]
[(2, 55), (4, 44), (6, 22), (8, 33)]
[(6, 22), (8, 33), (4, 44), (2, 55)]
{8: 33, 2: 55, 4: 44, 6: 22}


zip

a=[1,2,3,4,5]
b=["a","b","c","d","e"]
print(zip(a,b)) #3以上成迭代了
for i in zip(a,b):
print(i)


打印结果
<zip object at 0x00000205CE994AC8>
(1, \'a\')
(2, \'b\')
(3, \'c\')
(4, \'d\')
(5, \'e\')

 

 

分类:

技术点:

相关文章:

  • 2022-01-08
  • 2022-01-08
  • 2021-12-04
  • 2022-01-19
  • 2021-09-12
  • 2021-07-25
  • 2021-10-11
猜你喜欢
  • 2021-09-12
相关资源
相似解决方案