zoukankan      html  css  js  c++  java
  • python 基础 内置函数

    内置参数

    print(all([5,-1,5]))  # 非0都是真 true
    print(all([0,-1,5]))  # false
    
    print(any([1,0,5]))  # 有一个数据为真,就为真
    print(any([]))       # false
    
    # 把数字转换成二进制
    print(bin(1))
    '''
    >>> bin(1)
    '0b1'
    >>> bin(2)
    '0b10'
    >>> bin(16)
    '0b10000'
    >>> bin(255)
    '0b11111111'
    >>>
    '''
    
    
    '''
    # 判断真假
    >>> bool(1)
    True
    >>> bool(0)
    False
    >>> bool(5)
    True
    >>> bool([])
    False
    >>> bool({})
    False
    >>> bool({1})
    True
    >>> bool([241])
    True
    '''
    
    '''
    a = bytes("abcd",encoding="utf8")
    print(a.capitalize(),a)
    
    b = bytearray("abcd",encoding="utf8")
    print(b[1])  # 打印ascii
    b[1]= 50
    print(b)
    '''
    
    
    # 判断一个事情可否调用 可调用true 不可调用false
    print(callable([]))
    False
    
    
    def abc1():pass
    print(callable(abc1) )
    
    True
    
    '''
    # ascii数字对应字符串转换
    >>>
    >>>
    >>> chr(97)
    'a'
    >>> chr(98)
    'b'
    >>> chr(90)
    'Z'
    >>> chr(99)
    'c'
    >>>
    
    # 反过来 必须输入ascii字符 转换成数字
    >>> ord('a')
    97
    >>> ord('b')
    98
    >>> ord('c')
    99
    >>> ord('1')
    49
    >>>
    '''
    
    
    '''
    # 查看 可以用什么方法
    >>> a = []
    >>>
    >>> dir(a)
    ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__
    , '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__
    , '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__'
     '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__
    educe__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__
    , '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear'
     'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sor
    ']
    >>>
    
    '''
    
    '''
    可以把 字符串转换成原来的数据类型  例如:原来是 list ,dict
    eval()
    
    '''
    
    '''
    # exec函数主要用于执行语句块
    
    >>> exec('a=1+3*2*2')
    >>> exec
    <built-in function exec>
    >>> a
    13
    >>>
    
    '''
    def abc1(n):
        print(n)
    
    abc1(3)
    
    # 传参数
    (lambda c:print(c))(110)
    
    abc = lambda c:print(c)
    abc(5)
    abc = lambda c:10 if c<5 else c
    print(abc(3))
    
    print("===========================================")
    
    # filter
    # 打印>6的
    res = filter(lambda n:n>6,range(10))
    for i in res:
        print(i)
    
    
    print("===========================================")
    
    # map
    # 把里面的集合每个数据 拿出来给前面的函数处理 然后用list方式打印出来
    res = map(lambda n:n*2,range(10))
    for i in res:
        print(i)
    
    0
    2
    4
    6
    8
    10
    12
    14
    16
    18
    
    
    
    print("===========================================")
    # 累加 reduce
    import functools
    res = functools.reduce(lambda x,y:x+y,range(1,10))
    print(res)
    
    # 累乘
    res = functools.reduce(lambda x,y:x*y,range(1,10))
    print(res)
    
    print("===========================================")
    
    # 判断变量存在否
    # print(globals())
    
    '''
    >>>
    >>> hash(1)
    1
    >>> hash(2)
    2
    >>> hash("ming")
    2265504022069637367
    >>>
    >>> hash("mike")
    -5868197253725756830
    >>>
    
    '''
    
    # 把一个数 转换成16进制
    '''
    >>>
    >>> hash(1)
    1
    >>> hash(2)
    2
    >>> hash("ming")
    2265504022069637367
    >>>
    >>> hash("mike")
    -5868197253725756830
    >>>
    '''
    
    # 返回多少次幂  例如 pow(x,y) x的y次方
    '''
    >>>
    >>> pow(3,3)
    27
    >>> pow(5,2)
    25
    >>> pow(8,2)
    64
    >>>
    
    '''
    
    # 排序 从小到大
    a = {6:2,8:0,1:4,-5:6,99:11,4:22}
    #print(a)
    print(sorted(a))
    [-5, 1, 4, 6, 8, 99]
    
    print(sorted(a.items()))    # key排序
    [(-5, 6), (1, 4), (4, 22), (6, 2), (8, 0), (99, 11)]
    
    print(sorted(a.items(),key=lambda x:x[1]))    # 按value排序,x代表一个元素
    
    [(8, 0), (6, 2), (1, 4), (-5, 6), (99, 11), (4, 22)]
    
    print("===========================================")
    
    # 把两个列表对应起来 合并
    
    d = [1,2,3,4,5,6]
    e = ['a','b','c','d','e','f']
    
    for i in zip(d,e):
        print(i)
    
    (1, 'a')
    (2, 'b')
    (3, 'c')
    (4, 'd')
    (5, 'e')
    (6, 'f')
    
    print("===========================================")
    
    __import__('生成器')
  • 相关阅读:
    openstack 网络架构 nova-network + neutron
    编程算法
    16周(项目四 动态数组)
    iptables惹的祸
    【剑指offer】和为定值的连续正数序列
    root用户改动普通用户文件
    Android学习四、Android中的Adapter
    初探swift语言的学习笔记四(类对象,函数)
    crtmpserver 基本流程分析
    八大排序算法总结
  • 原文地址:https://www.cnblogs.com/mingerlcm/p/8001367.html
Copyright © 2011-2022 走看看