zoukankan      html  css  js  c++  java
  • Python内置函数

    Python内置函数实例:

    python中内置了很多函数,这也大大的提升了编程员的效率;下面介绍一些常用的内置函数的用法

    一、函数的lambda表达形式;
    因为对于内置函数的用法这里可以结合lambda表达式结合进行使用所以这里先简单介绍lambda表达式的用法:

    def func(x):
    	return x**2
    print(func(2))
    这个函数可以用lambda的形式进行表示:
    lambda x(函数的参数):x**2(这里不用加return,lambda自带return)
    

    内置函数:

    1.divmod()

    例:print(divmod(10,3))  #表示10除以3之后的商和余数
    "D:Program Filespython.exe" "E:/py_code/day 3.py"
    (3, 1)
    
    Process finished with exit code 0
    

    2.enumerate()

    l=["a","b","c"]
    for i in enumerate(l)  #取出值和对应的索引
    	print(i)
    
    "D:Program Filespython.exe" "E:/py_code/day 3.py"
    (0, 'a')
    (1, 'b')
    (2, 'c')
    
    Process finished with exit code 0	
    

    3.s=frozenset({1,2,3})

    将可变的集合变成不可变集合

    4.max() #取最大值

    print(max(i for i in range(10)))
    
    "D:Program Filespython.exe" "E:/py_code/day 
    
    
    3.py"
    9
    
    Process finished with exit code 0
    

    5.pow()

    print(pow(3,2))  #表示3的2次方
    print(pow(3,2,2)) #表示3的2次方的结果在对2取余
    

    6.range()

    for i in range(5,0,-1):
        print(i)
    
    "D:Program Filespython.exe" "E:/py_code/day 3.py"
    5
    4
    3
    2
    1
    
    Process finished with exit code 0
    

    7.reversed() #反转

    l=[1,2,3,4,5,]
    print(list(reversed(l)))
    

    8.round() #对浮点型的数据指定小数点的位数

    print(round(3.14125,3))
    
    "D:Program Filespython.exe" "E:/py_code/day 3.py"
    3.141
    
    Process finished with exit code 0
    

    9.map() #映射

    l=[1,2,3,4]
    m=map(lambda x:x**2,l) #这一步这是一个生成器可以加list启动
    print(list(m))
    
    "D:Program Filespython.exe" "E:/py_code/day 3.py"
    [1, 4, 9, 16]
    
    Process finished with exit code 0
    

    10.reduce() #合并

    print(reduce(lambda x,y:x+y,range(100),100))
    

    11.filter #过滤

    names=["12kkk","12jkdkaj","sadj"]
    print(list(filter(lambda name:name.endswith("12"),names)))
    
    "D:Program Filespython.exe" "E:/py_code/day 3.py"
    ['12kkk']
    
    Process finished with exit code 0
    

    12.sorted() # 排序

    l=[1,2,3,4,-1]
    print(sorted(1))
    
    "D:Program Filespython.exe" "E:/py_code/day 3.py"
    [-1, 1, 2, 3, 4]
    
    Process finished with exit code 0
    

    13.bool:根据传入的参数的逻辑值创建一个新的布尔值

    >>> bool() #未传入参数
    False
    >>> bool(0) #数值0、空序列等值为False
    False
    >>> bool(1)
    True
    

    14.int:根据传入的参数创建一个新的整数

    >>> int() #不传入参数时,得到结果0。
    0
    >>> int(3)
    3
    >>> int(3.6)
    3
    

    15.float:根据传入的参数创建一个新的浮点数

    >>> float() #不提供参数的时候,返回0.0
    0.0
    >>> float(3)
    3.0
    >>> float('3')
    3.0
    

    16.str:返回一个对象的字符串表现形式(给用户)

    >>> str()
    ''
    >>> str(None)
    'None'
    >>> str('abc')
    'abc'
    >>> str(123)
    '123'
    

    17.bytearray:根据传入的参数创建一个新的字节数组

    >>> bytearray('中文','utf-8')
    bytearray(b'xe4xb8xadxe6x96x87')
    

    18.bytes:根据传入的参数创建一个新的不可变字节数组

    >>> bytes('中文','utf-8')
    b'xe4xb8xadxe6x96x87'
    

    19.memoryview:根据传入的参数创建一个新的内存查看对象

    
    >>> v = memoryview(b'abcefg')
    >>> v[1]
    98
    >>> v[-1]
    103
    

    20.ord:返回Unicode字符对应的整数

    >>> ord('a')
    97
    

    21.chr:返回整数所对应的Unicode字符

    >>> chr(97) #参数类型为整数
    'a'
    

    22.bin:将整数转换成2进制字符串

    >>> bin(3) 
    '0b11'
    

    23.oct:将整数转化成8进制数字符串

    >>> oct(10)
    '0o12'
    

    24.hex:将整数转换成16进制字符串

    >>> hex(15)
    '0xf'
    

    25.tuple:根据传入的参数创建一个新的元组

    >>> tuple() #不传入参数,创建空元组
    ()
    >>> tuple('121') #传入可迭代对象。使用其元素创建新的元组
    ('1', '2', '1')
    

    26.list:根据传入的参数创建一个新的列表

    >>>list() # 不传入参数,创建空列表
    [] 
    >>> list('abcd') # 传入可迭代对象,使用其元素创建新的列表
    ['a', 'b', 'c', 'd']
    

    27.dict:根据传入的参数创建一个新的字典
    复制代码

    >>> dict() # 不传入任何参数时,返回空字典。
    {}
    >>> dict(a = 1,b = 2) #  可以传入键值对创建字典。
    {'b': 2, 'a': 1}
    >>> dict(zip(['a','b'],[1,2])) # 可以传入映射函数创建字典。
    {'b': 2, 'a': 1}
    >>> dict((('a',1),('b',2))) # 可以传入可迭代对象创建字典。
    {'b': 2, 'a': 1}
    

    28.set:根据传入的参数创建一个新的集合

    >>>set() # 不传入参数,创建空集合
    set()
    >>> a = set(range(10)) # 传入可迭代对象,创建集合
    >>> a
    {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
    

    29frozenset:根据传入的参数创建一个新的不可变集合

    >>> a = frozenset(range(10))
    >>> a
    frozenset({0, 1, 2, 3, 4, 5, 6, 7, 8, 9})
    

    30.enumerate:根据可迭代对象创建枚举对象

    >>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
    >>> list(enumerate(seasons))
    [(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
    >>> list(enumerate(seasons, start=1)) #指定起始值
    [(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
    

    31.range:根据传入的参数创建一个新的range对象

    >>> a = range(10)
    >>> b = range(1,10)
    >>> c = range(1,10,3)
    >>> a,b,c # 分别输出a,b,c
    (range(0, 10), range(1, 10), range(1, 10, 3))
    >>> list(a),list(b),list(c) # 分别输出a,b,c的元素
    ([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 4, 7])
    >>>
    

    32.iter:根据传入的参数创建一个新的可迭代对象

    
    >>> a = iter('abcd') #字符串序列
    >>> a
    <str_iterator object at 0x03FB4FB0>
    >>> next(a)
    'a'
    >>> next(a)
    'b'
    >>> next(a)
    'c'
    >>> next(a)
    'd'
    >>> next(a)
    Traceback (most recent call last):
      File "<pyshell#29>", line 1, in <module>
        next(a)
    StopIteration
    

    33.slice:根据传入的参数创建一个新的切片对象

    
    >>> c1 = slice(5) # 定义c1
    >>> c1
    slice(None, 5, None)
    >>> c2 = slice(2,5) # 定义c2
    >>> c2
    slice(2, 5, None)
    >>> c3 = slice(1,10,3) # 定义c3
    >>> c3
    slice(1, 10, 3)
    
    

    34.super:根据传入的参数创建一个新的子类和父类关系的代理对象

    
    #定义父类A
    >>> class A(object):
        def __init__(self):
            print('A.__init__')
    
    #定义子类B,继承A
    >>> class B(A):
        def __init__(self):
            print('B.__init__')
            super().__init__()
    
    #super调用父类方法
    >>> b = B()
    B.__init__
    A.__init__
    
    

    35.object:创建一个新的object对象

    >>> a = object()
    >>> a.name = 'kim' # 不能设置属性
    Traceback (most recent call last):
      File "<pyshell#9>", line 1, in <module>
        a.name = 'kim'
    AttributeError: 'object' object has no attribute 'name'
    
  • 相关阅读:
    20155209 2016-2017-2 《Java程序设计》第十周学习总结
    2017-2018-1 20155203 《信息安全系统设计基础》第四周学习总结
    2017-2018-1 20155203《信息安全系统设计基础》第一周学习总结
    20155203 实验五《网络编程与安全》
    20155203 2016-2017-2《Java程序设计》课程总结
    20155203 实验四《 Android程序设计》实验报告
    2017-5-10 课堂实践20155203
    20155203 实验三《敏捷开发与XP实践》实验报告
    20155203 2016-2017-2 《Java程序设计》第10周学习总结
    20155203 2016-2017-4 《Java程序设计》第9周学习总结
  • 原文地址:https://www.cnblogs.com/lijian-22huxiaoshan/p/7056130.html
Copyright © 2011-2022 走看看