美文网首页python内置函数
python3 内置函数04

python3 内置函数04

作者: 小白快加油 | 来源:发表于2018-09-17 21:49 被阅读0次

1 bytearray() 方法返回一个新字节数组。这个数组里的元素是可变的,并且每个元素的值范围: 0 <= x < 256。

>>>bytearray()
bytearray(b'')
>>> bytearray([1,2,3])
bytearray(b'\x01\x02\x03')
>>> bytearray('runoob', 'utf-8')
bytearray(b'runoob')

2 filter() 函数用于过滤序列,过滤掉不符合条件的元素,返回一个迭代器对象,如果要转换为列表,可以使用 list() 来转换。

该接收两个参数,第一个为函数,第二个为序列,序列的每个元素作为参数传递给函数进行判,然后返回 True 或 False,最后将返回 True 的元素放到新列表中。

语法:
filter(function, iterable)

def is_odd(n):
    return n % 2 == 1
 
tmplist = filter(is_odd, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
newlist = list(tmplist)
print(newlist)
# [1, 3, 5, 7, 9]

3 issubclass() 方法用于判断参数 class 是否是类型参数 classinfo 的子类。

class A:
    pass
class B(A):
    pass
    
print(issubclass(B,A))    # 返回 True

4 pow() 方法返回x的y次方的值。

import math
math.pow( x, y )

>>> import math
>>> math.pow(5, 2)
25.0

5 super() 函数是用于调用父类(超类)的一个方法。

super 是用来解决多重继承问题的,直接用类名调用父类方法在使用单继承的时候没问题,但是如果使用多继承,会涉及到查找顺序(MRO)、重复调用(钻石继承)等种种问题。

MRO 就是类的方法解析顺序表, 其实也就是继承父类方法时的顺序表。

class A:
    pass
class B(A):
    def add(self, x):
        super().add(x)
class FooParent(object):
    def __init__(self):
        self.parent = 'I\'m the parent.'
        print ('Parent')
    
    def bar(self,message):
        print ("%s from Parent" % message)
 
class FooChild(FooParent):
    def __init__(self):
# super(FooChild,self) 首先找到 FooChild 的父类(就是类 FooParent),
#然后把类B的对象 FooChild 转换为类 FooParent 的对象
        super(FooChild,self).__init__()    
        print ('Child')
        
    def bar(self,message):
        super(FooChild, self).bar(message)
        print ('Child bar fuction')
        print (self.parent)
 
if __name__ == '__main__':
    fooChild = FooChild()
    fooChild.bar('HelloWorld')

#Parent
#Child
#HelloWorld from Parent
#Child bar fuction
#I'm the parent.

6 bytes 函数返回一个新的 bytes 对象,该对象是一个 0 <= x < 256 区间内的整数不可变序列。它是 bytearray 的不可变版本。

>>>a = bytes([1,2,3,4])
>>> a
b'\x01\x02\x03\x04'
>>> type(a)
<class 'bytes'>
>>>
>>> a = bytes('hello','ascii')
>>>
>>> a
b'hello'
>>> type(a)
<class 'bytes'>

7 float() 函数用于将整数和字符串转换成浮点数。

8 iter() 函数用来生成迭代器。

>>>lst = [1, 2, 3]
>>> for i in iter(lst):
...     print(i)
... 
1
2
3

9 print() 方法用于打印输出,最常见的一个函数。

>>> print("www","runoob","com",sep=".")  # 设置间隔符
www.runoob.com

10 tuple 函数将列表转换为元组。

>>>list1= ['Google', 'Taobao', 'Runoob', 'Baidu']
>>> tuple1=tuple(list1)
>>> tuple1
('Google', 'Taobao', 'Runoob', 'Baidu')

相关文章

  • python3 内置函数04

    1 bytearray() 方法返回一个新字节数组。这个数组里的元素是可变的,并且每个元素的值范围: 0 <= x...

  • Python内建函数

    在python2中一下函数为内置函数,在python3中,一下部分函数已从内置函数中移除,变为内置类 1、map(...

  • 【小白福利、快速入门Python】之内置函数

    欢迎大家关注公众号【哈希大数据】python的内置函数――基础介绍总结篇68个内置函数python3内置了68个常...

  • Python 69个内置函数分8类总结

    内置函数 Python3解释器中内置了69个常用函数,属于底层的函数,它们到处可用。有些对大家来说比较熟悉,比如a...

  • Python学习

    第六天 Python常用内置函数Python3解释器中内置了69个常用函数,属于底层的函数,它们到处可用。有些对大...

  • 02-Python3的基础语法

    Python3中常用的快捷键 prin()函数 含义:是python3中的内置函数作用:在控制台输出()里面的所有...

  • python3内置函数(1)

    python3常用内置函数: 1.abs()函数 abs函数是返回数字的绝对值 基本语法: abs(x) x...

  • Python3内置函数(一)

    Python3内置函数(一) 1.abs abs()函数返回数字的绝对值 语法: abs(x) 参数 x -- 数...

  • python3创建多线程的几种方法

    python3创建多线程主要有下面两种方法:函数、类 1.函数创建多线程 python3中,提供了一个内置模块th...

  • Python 2 与 python 3的区别

    python3已经将print置为内置函数,因此输出用print() round()四舍五入函数: python3...

网友评论

    本文标题:python3 内置函数04

    本文链接:https://www.haomeiwen.com/subject/bxidnftx.html