python学习之路(内置方法)

来源:互联网 发布:智能机器人软件 编辑:程序博客网 时间:2024/05/29 14:50

python学习之路(内置方法)

绝对值 abs

不解释

abs()

判断可迭代的类型值是否所有的都为真 all()

解释:非零就是真

>>> print(all([0,-1,1])) # 0 不为真False>>> >>> print(all([-1,1]))True>>> 

判断只要有一个为真 any()

只要有一个为真,就是true

>>> print(all([-1,1]))True>>> print(any([])) # 空为假False>>> print(any([0]))False>>> print(any([0,1]))True>>> 

将数值变成可打印的字符串的类型 ascii()

>>> print(ascii('我是tt'))'\u6211\u662ftt'>>> print(ascii( [1,2,'我是tt'] ))[1, 2, '\u6211\u662ftt']>>> print(type(ascii( [1,2,'我是tt'] )))<class 'str'>

将数字转化成2进制 bin()

>>> print(bin(1))0b1>>> print(bin(33))0b100001>>> print(bin(100))0b1100100# 0b 是开始,后面是转化的二进制数字

判断真假bool()

>>> print(bool(0)) # 0为假False>>> print(bool(1)) # 非0为真True>>> print(bool([])) #空 假False>>> print(bool([0])) # 非空 为真True

将字符串变成byte类型 bytes()

>>> print(bytes('abcde',encoding='utf-8'))b'abcde'

将字符串变成可修改的byte数组 bytearray()

>>> a = bytearray('abcde',encoding = 'utf-8')>>> a[1] = 200>>> print(a)bytearray(b'a\xc8cde') # 修改之后的结果>>>

判断是不是可以调用 callable()

>>> print(callable( [] )) # 列表不能调用False>>> def a ():...     pass...>>> print(callable(a)) # 函数可以调用True

将数字转化成ascii chr()

>>> chr(98)'b'>>> chr(99)'c'>>> chr(100)'d'

将ascii 转化成 数字 ord()

>>> ord('a')97>>> ord('b')98>>> ord('c')99

类方法 classmethod 后面会着重讲

将代码编译 compile()

一般都是底层用

>>> code = "for i in range(10):print(i)">>> a = compile(code, '', 'exec')>>> print(a)<code object <module> at 0x000001951BB64ED0, file "", line 1>>>> exec(a)0123456789>>>

事实上直接掉用exec(code) 也可以运行

delattr() 这个方法在面向对象中进行讲解

查看这个类型都有什么方法 dir()

比如,查看字典中都有什么方法

>>> a ={}>>> dir(a)['__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']>>>

两数相除 返回商和余数 divmod()

>>> divmod(5,2)(2, 1)>>> divmod(5,3)(1, 2) # 第一个数是商,第二个是余数

enumerate()

a = [1,2,3,4]for index,val in enumerate(a):    print(index,val)

打印:
0 1 前面是位置数,后面是值
1 2
2 3
3 4

eval() 把字符串变成字典

>>> x = 1>>> eval('x+1')2

只能算简单的加减乘除。
复杂的用exec()

filter() map() reduce()

一般会结合lambda使用

a = filter(lambda x:x<5,range(10))a = map(lambda x:x*2 , range(10))

自己可以粘贴运行一下。打印出来看一下区别。
reduce()方法 在python3 以上 需要import functools

a = functools.reduce(lambda x,y:x+y,range(10))print(a) # 45

frozenset() 不可更改集合

不解释了。
自己声明一个,看看有没有更改方法。

globals()

将程序里面所有的变量,用key-value的格式展现。

help() 查看帮助

不解释

转成十六进制 hex()

>>> hex(17)'0x11' # 0x开头>>> hex(120)'0x78'>>> hex(255)'0xff'

len() 长度

不解释

iter() 将可迭代的装成迭代器

>>> a = iter([1,2,3,4])>>> print(a.__next__())1>>> print(a.__next__())2>>> print(a.__next__())3

迭代器的学习:
http://blog.csdn.net/tianrun1110/article/details/78825755

locals() 局部变量

>>> def a ():...     local_1 = 1...     print(locals())... >>> a(){'local_1': 1}

max() 最大值 、min() 最小值

>>> max([1,3,5,32])32>>> min([1,3,5,32])1

next() 就相当于迭代器中的__next__()

oct() 转8进制

>>> oct(11)'0o13' # 0o开头>>> oct(8)'0o10'>>> oct(122)'0o172'

pow() x的y次方

>>> pow(2,1) # 2^12>>> pow(2,2)4>>> pow(3,2)9

repr() 用字符串表示对象

>>> pow(2,1)2>>> pow(2,2)4>>> pow(3,2)9

reversed() 反转

>>> a[1, 2, 3]>>> b = reversed(a)>>> b<list_reverseiterator object at 0x7fdea0b85b00>>>> for i in b:...     print(i)... 321

round() 小数,保留几位

>>> round(1.11222,1)1.1>>> round(1.11222)1>>> round(1.11222,3)1.112

# slice() 切片 没什么用。。。

sorted() 排序,可以把字典排序

字典本来是无序的。

>>> a = {1:6,-5:3,4:5,3:6}>>> print(sorted(a.items()))[(-5, 3), (1, 6), (3, 6), (4, 5)]>>> a{1: 6, -5: 3, 4: 5, 3: 6} # 这可以看出a 本来是无序的

上面是按照key排序,还可以按照value排序

>>> print(sorted(a.items(),key=lambda x:x[1]))[(-5, 3), (4, 5), (1, 6), (3, 6)]

sum() 列表求和

type() 查看数据类型

zip() 将两个列表一一对应

>>> a = [1,2,3,4]>>> b = ['a','b','c','d']>>> for i in zip(a,b):...     print(i)...(1, 'a')(2, 'b')(3, 'c')(4, 'd')