zoukankan      html  css  js  c++  java
  • python04——内嵌函数

    1.内置函数简介

    内嵌函数——系统自带的函数 

    https://docs.python.org/3/library/functions.html

    2.数据运算函数

    abs(-10)————求绝对值   

    round(2.567,2)————四舍五入求近似值的###保留2位小数

    pow(3,4)————求幂  ##3的4次方

    divmod(7,3)————求余和商###输出(2,1)商2余1

    max(x,y,z)     max([x,y,z])————求最大值

    min()————求最小值              

    sum([1,2,3])  sum((1,2),3)  sum([1,2,3],4)————求和      

    eval()————执行一个字符串表达式,并返回表达式的值

    a,b,c=1,2,3
    eval('a+b')
    ####或者
    eval('a+b+c',{'c':3,'a':1,'b':2})

    3.类型转换函数

    int()    字符串或数字转换为整型

    float()   将整数和字符串转换成浮点数

    str()    转为字符串

    ord()      返回对应字符的ascii码

    chr()    数字转字符    

    bool()    转为布尔型

    bin()    转为2进制

    hex()      转为16进制

    oct()    转为8进制

    list()    元组转列表

    tuple()   将列表转换为元组

    dict()    创建一个字典

    bytes()   转为字节数组

    4.序列操作函数

    all()     判断给定元组或列表中所有元素是否全部TRUE,含0、空、FALSE就会返回FALSE(空元组、空列表返回值为True)

    any()    判断给定元组或列表中所有元素是否全部FALSE,全部为0、空、FALSE才返回FALSE,否则返回true

    sorted()    排序操作,sort()是应用在list上的方法(对已有列表进行排序),sorted()可以对一切可迭代对象(输出排序后的新列表)

            默认升序,如果想要降序:sorted([1,2,3],reverse=True)

    li=[1,2,3,6,7,4,]
    li.sort()
    print(li)    ###li会被更改
    
    newlist=sorted(li,reverse=True)
    print(li)  ###li不会被更改
    print(newlist)

    reverse()

    range(start,end,步长)    创建一个整数的列表,一般用于for循环中    

    zip()    将所有参数打包成一个元组,返回元组组成的列表;如果参数元素个数不一致,返回列表长度与最短的相同,用*可将元组解压为列表

    t=zip([1,2,3],['a','b','c','d'])
    print(list(t))

    enumerate()    将列表、元素、字符串组合为一个索引序列,同时列出数据和数据下标,一般用在for循环

    seasons=['','','','']
    print(list(enumerate(seasons)))

    list(enumerate(seasons,5)) ####开始的下标为5
    seasons=['','','','']
    for index,item in enumerate(seasons,5):  ##把下标存储在index中,
        print(index,item)
    ###对字典
    dic={}
    dic['name']='yh'
    dic['hob']='run'
    dic['pro']='teach'
    for i,j in enumerate(dic):
        print(j)

    5.set集合

    set——无序且不重复的元素结合,不支持索引和切片

    set的创建方式:

    set1={1,2}
    ###另一种
    list1=[1,1,2,3]
    set1=set(list1)   ###可以把列表去重  

    集合操作函数:

    add()——添加

    set1.add('love')

    clear()——清空

    set1.clear()

    difference()——取差集,set1中存在,set2中不存在的

    set1={1,2,3}
    set2={2,3,4}
    set1.difference(set2)

    intersection()——取交集

    set1.intersection(set2)
    set1&set2也可以

    union()——取并集

    set1.union(set2)
    set1|set2也可以

    pop()——从集合中拿数据,同时删除

    set1.pop()   ##随机拿走一个

    discard()——移除指定数据

    set1.discard(3)

    update()——合并在一起,重复项合并

    set1.updata(set2)

    巩固:

    1.求三组连续自然数的和:求出1到10、20到30和35到45的三个和

    def sum1(a,b):
        re=0
        for i in range(a,b+1):
            re+=i           
        return re
    
    print(sum1(1,10))
    print(sum1(20,30))
    print(sum1(35,45))

    2.100个和尚吃100个馒头,大和尚一人吃3个馒头,小和尚三人吃1个谩头。请问大小和尚各多少人?

    def person():
        for a in range(1,100):
            if 3*a+(100-a)/3==100:
               # print('大和尚:',a)
                #print('小和尚:',100-a)
                return (a,100-a)
    
    aaa=person()
    print('大和尚{}人 小和尚{}人'.format(aaa[0],aaa[1]))

    3.指定一个列表,列表里含有唯一一个只出现过一次的数字。写程序找出这个“独一无二”的数字

    li=[1,2,3,4,5,6,1,2,3,4,5,7,8,9,0,10,0,5,6,5,7,6,8,7,9,9,7,5,6]
    set1=set(li)
    for i in set1:
        li.remove(i)
        pass
    set2=set(li)
    for i in set1:
        if i not in set2:
            print(i)
  • 相关阅读:
    MySQL(六)锁机制
    MySQL(七)性能优化
    Jenkins+SVN+Maven+shell 自动化部署实践
    vue中使用echarts图表自适应窗口的几种方案
    想加入或者创建一个技术交流群。偏向前端,各位大佬有推荐吗?
    Goland 2019下载和安装(带破解补丁和汉化包)
    Elementui 表单验证 点击保存定位到验证失败处
    Scheduled 参数配置
    SpringBoot @Scheduled定时任务
    centos 7 安装 SVN服务
  • 原文地址:https://www.cnblogs.com/yilia-er/p/14085096.html
Copyright © 2011-2022 走看看