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

    内置函数

    1 abs 绝对值函数

    res = abs(-9.9867)
    print(res)

    执行

    [root@node10 python]# python3 test.py
    9.9867

    2 round  四舍六入五凑偶

    • n.5 n为偶数则舍去 n.5 n为奇数,则进一
    • 奇进偶不进
    res = round(3.5)
    print(res)
    res = round(12.5)
    print(res)

    执行

    [root@node10 python]# python3 test.py
    4
    12

    3 sum 计算一个序列得和

    listvar = [1,2,34,34,43,32,5]
    total = 0
    for i in listvar:
            total += i
    print(total)
    
    res = sum(listvar)
    print(res)

    执行

    [root@node10 python]# python3 test.py
    151
    151

     4 max获取一个序列里边的最大值

    listvar = [1,2,34,34,43,32,5]
    res = max(listvar)
    print(res)

    执行

    [root@node10 python]# python3 test.py
    43

    5min获取一个序列里边的最小值

    listvar = [1,2,34,34,43,32,5]
    res = min(listvar)
    print(res)

    执行

    [root@node10 python]# python3 test.py
    1

    6 sort找出最大值和最小值

    listvar = [1,2,34,34,43,32,5]
    listvar.sort()
    print(listvar)
    res_min = listvar[0]
    res_max = listvar[-1]
    print(res_min)
    print(res_max)

    执行

    [root@node10 python]# python3 test.py
    [1, 2, 5, 32, 34, 34, 43]
    1
    43

    找出岁数最小的元组

    listvar = [("张三",45),("李四",38),("王五",18),("田七",120)]
    def func(n):
        #("张三",45)
        return n[-1]
    res = min(listvar,key=func)
    print(res)

    执行

    [root@node10 python]# python3 test.py
    ('王五', 18)

    执行过程

    ("王五",18 扔到func当中  return 18
    首先把("张三",45) 扔到func当中  return 45
    ("李四",38) 扔到func当中  return 38
    ("田七",120)  扔到func当中  return 120
    按照右侧实际年龄进行排序,找出最小的那个数,直接把元组返回.

    7 pow    计算某个数值的x次方

    res = pow(2,3)
    print (res) res
    = pow(4,20)
    print(res)
    '''pow(可以接受第三个可选参数 , 第三个参数是用来取余的)''' res= pow(2,3,3) print(res)

    执行

    [root@node10 python]# python3 test.py
    8
    1099511627776
    2

    8 range产生指定范围数据的可迭代对象

    res = range(10)
    print(res)
    for i in range(10):
        print(i)
    for i in range(1,8):
        print(i)
    for i in range(1,16,3):
        print(i)
    
    for i in range(10,0,-1):
        print(i)

    执行

    [root@node10 python]# python3 test.py
    range(0, 10)
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    1
    2
    3
    4
    5
    6
    7
    1
    4
    7
    10
    13
    10
    9
    8
    7
    6
    5
    4
    3
    2
    1

    9 bin将10进制数据转化为二进制

    res = bin(15)
    print(res)

    执行

    [root@node10 python]# python3 test.py
    0b1111

    10 oct将10进制数据转化为八进制

    res = oct(8)
    print(res)

    执行

    [root@node10 python]# python3 test.py
    0o10

     11 hex将10进制数据转化为16进制

    res = hex(16)
    print(res)

    执行

    [root@node10 python]# python3 test.py
    0x10

    12 chr将ASCII编码转换为字符

    res  = chr(97)
    print(res)

    执行

    [root@node10 python]# python3 test.py
    a

    13 ord将字符转换为ASCII编码

    res = ord("A")
    print(res)

    执行

    [root@node10 python]# python3 test.py
    65

    14 eval 将字符串当作python代码执行

    res = "print(123)"
    print(res)
    eval(res)
    res = "print('sdfsdf')"
    print(res)
    eval(res)
    res = "print("sdfsdf")"
    print(res)
    eval(res)

    执行

    [root@node10 python]# python3 test.py
    print(123)
    123
    print('sdfsdf')
    sdfsdf
    print("sdfsdf")
    sdfsdf

    15 exec将字符串当作python代码执行(功能更强大)

    res = """
    for i in range(10):
            print(i)
    """
    # 要注意小心sql注入 delete from 数据 where id = 1 exec或者eval两个函数慎用
    exec(res)

    执行

    [root@node10 python]# python3 test.py
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9

    16 epr 不转义字符输出字符串

    listvar = [1,2,3]
    res = repr(listvar)
    print(res,type(res))
    strvar = "123"
    print(repr(strvar))
    strvar = "111
    	
    "
    print(repr(strvar))

    执行

    [root@node10 python]# python3 test.py
    [1, 2, 3] <class 'str'>
    '123'
    '111
    	
    '

    17 hash生成哈希值

    字典的键 和 集合当中的值 需要使用哈希算法
    哈希算法在存储上是无序的散列,经过计算之后会产生具有固定长度的唯一值

    通过字符串和内存地址加在一起计算出来的 通过hash计算出来

    res1 = "今天天气好"
    res2 = "今天天气好"
    res3 = "132"
    print(hash(res1))
    print(hash(res2))
    print(hash(res3))

    执行

    [root@node10 python]# python3 test.py
    -8085451974024421996
    -8085451974024421996
    -5108487044744349521

    可哈希数据:Number(int bool complex float) str () [不可变数据]
    不可哈希数据:dict list set [可变数据]

  • 相关阅读:
    关于我 — About Me
    《这样说就对了》读书笔记
    LOMA280保险原理读书笔记
    .NET单元测试的艺术-3.测试代码
    .NET单元测试的艺术-2.核心技术
    .NET单元测试的艺术-1.入门
    《人人都该买保险》读书笔记
    借助 Lucene.Net 构建站内搜索引擎(下)
    借助 Lucene.Net 构建站内搜索引擎(上)
    自己动手写一个简单的MVC框架(第二版)
  • 原文地址:https://www.cnblogs.com/zyxnhr/p/12287858.html
Copyright © 2011-2022 走看看