zoukankan      html  css  js  c++  java
  • Python小白学习之路(十六)—【内置函数一】

    将68个内置函数按照其功能分为了10类,分别是:

    • 数学运算(7个)
      • abs()    divmod()    max()    min()    pow()    round()    sum()
    • 类型转换(24个)
      • bool()    int()    float()    complex()    str()    bytearray()
      • bytes()   memoryview()   ord()   chr()   bin()   oct()   hex()
      • tuple()   list()   dict()   set()   frozenset(  ) enumerate()
      • range()   iter()   slice()   supper()   object()
    • 序列操作(8个)
      • all()      any()   filter()   map()   next()   reversed()   sorted()   zip()
    • 对象操作(7个)
      • help() dir() id() hash() type() len() ascii() format() vars()
    • 反射操作(8个)
      • _import__() isinstance() issubclass() hasattr() getattr() setattr() delattr() callable()
    • 变量操作(2个)
      • globals() locals()
    • 交互操作(2个)
      • print() input()
    • 文件操作(1个)
      • open()
    • 编译执行(4个)
      • compile() eval() exec() repr()
    • 装饰器(3个)
      • property() classmethod() staticmethod()


    一、数学运算类(7个)

    abs()

    • Return the absolute value of the argument.取绝对值
    print(abs(-1))
    #执行结果
    1

    divmod()

    • divmod(x, y) -> Return the tuple (x//y, x%y).返回商和余数
    print(divmod(5, 2))
    #执行结果
    (2, 1)

    max()

    • 返回最大值
    • 处理的对象为可迭代对象
    • (相当于一个for循环取出每一个元素进行比较)
    • 不同类型之间是不能进行比较的
    • 每个元素进行比较时,是从每个元素的第一个位置开始依次比较
    • (如果一个位置分出大小,后面的位置可以不用进行比较便可以得出比较结果)
    #用法一:
    num_1 = [1,38,2,0,4,7]
    print(max(num_1))
    
    #用法二:传入对象为字典,默认比较的是字典的key
    age_dic={'age1':14, 'age2':3, 'age3':45, 'age1':18} 
    print(max(age_dic)) 
    print(max(age_dic.values())) # 比较的是字典的values
    #执行结果
    age4
    45

    用法三:
    任务:
    将给定名单中年龄最大的找出来

    people_list = [
    {'name':'xhg','age':18},
    {'name':'aaa','age':10},
    {'name':'bbb','age':30},
    {'name':'ccc','age':14},
    ]
    print(max(people_list, key = lambda dic:dic['age']))
    #执行结果
    {'name': 'bbb', 'age': 30}
    '''
    #程序分析
    #这段代码的理解 max(people_list, key = lambda dic:dic['age'],实际上进行了这样一个操作
    ret = []
    for item in people_list:
    ret.append(item['age'])
    print(max(ret))
    '''

    min()

    • 返回可迭代对象的元素中的最小值或者所有参数的最小值
    • 具体用法同max()函数


    pow()

    • Equivalent to x**y (with two arguments) or x**y % z (with three arguments)
    • 两个参数,做幂运算
    • 三个参数,先做幂运算,再取余
    print(pow(2,3))
    #执行结果
    8
    print(pow(2,3,3))
    #执行结果
    2

    round()

    • 对浮点数进行四舍五入求值
    • 第二个参数为可选参数,如果不填默认取整,参数代表保留的位数
    print(round(3.1415926))
    print(round(3.1415926, 3))
    #执行结果
    3
    3.142

    sum()

    • 对可迭代对象中的每个元素求和
    • 可迭代对象中的元素类型是数值
    print(sum([1, 2, 3, 4, 5]))
    #执行结果
    15

    二、类型转换(24个)

    bool() 

    • 根据传入的参数的逻辑值创建一个新的布尔值
    • None '' () {} [] 0 ==>False
    • 其余 ==>True
    print(bool(None))
    print(bool('xhg'))
    #执行结果
    False
    True

    int()

    • 根据传入的参数创建一个整数
    print(int('123')) #将字符串转化为整形
    print(int(123.9)) #取整
    
    #执行结果
    123
    123

    float()

    根据传入的参数创建一个新的浮点数
    
    print(float('123.78'))    #将字符串转化为浮点型
    print(float(123))    #将整形转换成浮点型
    
    #执行结果
    123.78
    123.0

    complex()

    • 根据传入参数创建一个新的复数
    • 有两个参数:
    • 第一个参数是实部;第二个参数是虚部
    • 不填参数,默认输出为 0j
    • 只填一个参数 a,则输出 a + 0j
    print(complex())
    print(complex(1))
    print(complex(1, 2))
    
    #执行结果
    0j
    (1+0j)
    (1+2j)

    str()

    • 将目标参数转化为字符串
    print(str(123))
    print(type(str(123)))
    print(str([1, 2, 3, 4, 'xhg']))
    print(type(str([1, 2, 3, 4, 'xhg'])))
    
    #执行结果
    123
    <class 'str'>
    [1, 2, 3, 4, 'xhg']
    <class 'str'>

    bytearray()

    • 根据传入的参数创建一个新的字节数组
    • 可传入的参数:
      • 字符串(使用指定编码方式编码);bytearray()然后使用str.encode()将字符串转换为字节。
      • 如果它是一个整数,那么数组将具有这个大小,并将用null字节初始化。
      • 如果它是符合缓冲区接口的对象,则将使用对象的只读缓冲区来初始化字节数组。
      • 如果它是可迭代的,那么它必须是range 0 < = x < 256的整数的迭代,它被用作数组的初始内容
    a = bytearray('小伙郭', encoding = 'utf -8' ) #字符串需要指定编码方式
    print(a, len(a))
    b = bytearray('小伙郭', encoding = 'gbk' )
    print(b, len(b))
    c = bytearray([1,2,3,255]) #如果元素大于255,将会报错
    print(c, len(c))
    
    #执行结果
    bytearray(b'xe5xb0x8fxe4xbcx99xe9x83xad') 9
    bytearray(b'xd0xa1xbbxefxb9xf9') 6
    bytearray(b'x01x02x03xff') 4

    bytes()

    • 根据传入的参数创建一个新的不可变字节数组
    • 具体用法及传入的参数同函数bytearray()

    ord()

    chr()

    • 返回Unicode字符对应的整数
    • 返回整数所对应的Unicode字符
    print(ord('d'))
    print(chr(100))
    #执行结果
    100
    d

    bin()整数-->2进制字符串
    oct()整数-->8进制数字符串
    hex()整数-->16进制字符串

    print(bin(5))
    print(oct(10))
    print(hex(15))
    
    #执行结果
    0b101
    0o12
    0xf

    tuple()

    • 根据传入的参数创建一个新的元组
    • 传入的参数为空或者是可迭代对象
    print(tuple())
    print(tuple('123456'))
    print(tuple([1, 2, 'a', 'b']))
    
    #执行结果
    ()
    ('1', '2', '3', '4', '5', '6')
    (1, 2, 'a', 'b')

    list()

    • 根据传入的参数创建一个新的列表
    • 传入的参数为空或者是可迭代对象
    print(list())
    print(list('123456'))
    print(list([1, 2, 'a', 'b']))
    
    #执行结果
    []
    ['1', '2', '3', '4', '5', '6']
    [1, 2, 'a', 'b']


    dict()

    • 根据传入的参数创建一个新的字典
    • 可以由下列四种方式传入参数:
      • 不传入参数,建立空字典
      • 通过映射函数创建字典
      • 通过键值对来创建字典
      • 通过可迭代对象来创建字典

     

    print(dict())
    print(dict(zip(['key1', 'key2'],[1, 2])))
    print(dict(key1 = 'a', key2 = 'b'))
    print(dict([['key1',1], ['key2','b']]))
    
    #执行结果
    {}
    {'key1': 1, 'key2': 2}
    {'key1': 'a', 'key2': 'b'}
    {'key1': 1, 'key2': 'b'}

    set()

    • 根据传入的参数创建一个新的集合
    • set() -> new empty set object
    • set(iterable) -> new set object
    print(set())
    print(set('123456'))
    print(set([1,2,3,'a','b']))
    
    #执行结果
    set()
    {'3', '1', '6', '2', '5', '4'}
    {1, 2, 3, 'b', 'a'}

    frozenset()

    • 根据传入的参数创建一个新的不可变集合
    print(frozenset())
    print(frozenset('123456'))
    
    frozenset()
    frozenset({'5', '1', '4', '3', '6', '2'})

    range()

    • 根据传入的参数创建一个新的range对象
    • python3里面,需要通过for循环来创建range()每一个元素
    • range(a,b)-->左闭右开
    • range(stop) -> range object
    • range(start, stop[, step]) -> range object
    for item in range(5):
    print(item)
    for item in range(10, 15):
    print(item)
    for item in range(0, 100, 20):
    print(item)
    
    #执行结果
    0 1 2 3 4 
    10 11 12 13 14
    0 20 40 60 80

    写在后面:

    内置函数总结好多哦

    今天眼睛看电脑时间太长了   累

    明天继续总结吧

    今天也就总结了一半的样子

    最近发现之前的知识有些忘记了

    需要返回去再看看了

    开题报告写的不是很好  今天老师要求重新写

    好好加油吧

    明天就是周末啦   可以粗去吃好吃的  放松放松

    更重要的  双11   买买买

  • 相关阅读:
    设计模式
    三——第二部分——第二篇论文 计划建设SQL Server镜像
    非正确使用浮点数据由项目产生BUG讨论的问题
    重写ViewPager实施单一交有关切换到这个问题,并没有缓存
    1159 Palindrome(最小插入回文串)
    CSDN-markdown编者LaTex数学公式
    找工作总结
    开展:随笔记录 OSGI的jar增加了一些小问题和注意事项
    js jquery版本号 金额千分之一转换功能(非规范,高效率)
    dom4j.jar有什么作用?
  • 原文地址:https://www.cnblogs.com/guoruxin/p/9937491.html
Copyright © 2011-2022 走看看