zoukankan      html  css  js  c++  java
  • 第四篇:python基本数据类型的常用方法(函数)与内建函数

    python内置了很多内置函数、类方法属性及各种模块。当我们想要当我们想要了解某种类型有哪些属性方法以及每种方法该怎么使用时,我们鼓励使用dir()函数、help()函数或网上搜索来研究各类的方法。dir() 函数不带参数时,返回当前范围内的变量、方法和定义的类型列表;带参数时,返回参数的属性、方法列表。如果参数包含方法__dir__(),该方法将被调用。如果参数不包含__dir__(),该方法将最大限度地收集参数信息。help()函数帮助我们了解模块、类型、对象、方法、属性的详细信息。

    print(dir()) #不带参数,获得当前模块的属性列表
    
    '''
    运行结果:
        ['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'num', 'srt1']
        
        注意:有一些属性或模块是python自动加载进去的。
    '''
    print(dir(list)) #若带参数,则返回参数的属性与方法
    print(help(print)) #查看print()函数的文档信息
    print(help('keywords')) #查看python的关键字

     number

    python官方详细文档:https://docs.python.org/3.6/library/stdtypes.html#numeric-types-int-float-complex

    字符串的常用函数

    字符串相关的官方文档:https://docs.python.org/3.6/library/stdtypes.html#text-sequence-type-str

    列表的常用函数

    列表相关的官方文档:https://docs.python.org/3.6/library/stdtypes.html#sequence-types-list-tuple-range

        添加 ——》append() , insert()

    fruits = ['苹果','梨子','香蕉','橙子','葡萄']
    fruits.append('李子') #末尾追加
    print(fruits)
    fruits.insert(2,'榴莲') #指定的下标位置插入
    print(fruits)
    fruits.insert(9,'荔枝') #当指定的下标大于列表的长度时,效果与append()一样
    print(fruits)

        删除 ——》pop() , remove()

    fruits = ['苹果','梨子','香蕉','橙子','葡萄']
    fruits.pop(1) #通过索引删除元素
    print(fruits)
    fruits.remove('葡萄') #知道元素的时候,可以使用remove()
    print(fruits)
    
    #通过del语句删除
    del fruits[2]
    print(fruits)

         更多列表的方法,使用print(dir(list))来查找

    集合的常用方法

        创建:set()、frozense()

    #1、创建,将会自动去重,其元素为不可变数据类型,即数字、字符串、元组
    test01 ={"zhangsan","lisi","wangwu","lisi",666,("hello","world",),True}
    #或者
    test02 =set({"zhangsan","lisi","wangwu","lisi",666,("hello","world",),True})
    
    #2、不可变集合的创建 -->frozenset()
    test =frozenset({"zhangsan","lisi","wangwu","lisi",666,("hello","world",),True})
    示例

        增:   add()、update()

    #更新单个值 --->add
    names ={"zhangsan","lisi","wangwu"}
    names.add("james") #其参数必须为hashable类型
    print(names)
    
    #更新多个值 -->update
    names ={"zhangsan","lisi","wangwu"}
    names.update({"alex","james"})#其参数必须为集合
    print(names)
    示例

        删除:pop()、remove()、discard()

    #随机删除 -->pop
    names ={"zhangsan","lisi","wangwu","alex","james"}
    names.pop()
    print(names)
    
    #指定删除,若要删除的元素不存在,则报错 -->remove
    names ={"zhangsan","lisi","wangwu","alex","james"}
    names.remove("lisi")
    print(names)
    
    #指定删除,若要删除的元素不存在,无视该方法 -->discard
    names ={"zhangsan","lisi","wangwu","alex","james"}
    names.discard("hello")
    print(names
    示例

       关系运算:交集 & 、并集 | 、差集 - 、交差补集 ^ 、 issubset() 、isupperset()

    english_c ={"ZhangSan","LiSi","James","Alex"}
    math_c ={"WangWu","LiuDeHua","James","Alex"}
    
    #1、交集-->  in a and in b
    #统计既报了英语班又报了数学班的同学
    print(english_c & math_c)
    print(english_c.intersection(math_c))
    #输出为:{'Alex', 'James'}
    
    #2、并集--> in a or in b
    #统计报名了两个班的所有同学
    print(english_c | math_c)
    print(english_c.union(math_c))
    #输出为:{'James', 'ZhangSan', 'LiuDeHua', 'LiSi', 'Alex', 'WangWu'}
    
    #3、差集--> in a not in b
    #统计只报名英语班的同学
    print(english_c - math_c)
    print(english_c.difference(math_c)) 
    #输出为:{'LiSi', 'ZhangSan'}
    
    4、交差补集 
    #统计只报名一个班的同学
    print(english_c ^ math_c)
    #输出为:{'LiuDeHua', 'ZhangSan', 'WangWu', 'LiSi'}
    
    #5、issubset-->n 是否为 m 的子集
    #   issuperset --> n 是否为 m 的父集
    n ={1,2,4,6,8,10}
    m ={2,4,6}
    l ={1,2,11}
    
    print(n >= m)
    #print(n.issuperset(m)) #n 是否为 m的父集
    #print(n.issuperset(l))
    print(m <=n)    
    #print(m.issubset(n))    #m 是否为 n的子集
    示例

    字典的常用方法

    字典相关的官方文档:https://docs.python.org/3.6/library/stdtypes.html#mapping-types-dict

    字典的增、删、改、查

    fruits = {'苹果':2,'梨子':5,'香蕉':6,'橙子':8,'葡萄':9}
    
    #增:只要添加的键在字典不存在,就会新添加这个元素。
    fruits['榴莲'] = 10
    print(fruits)
    
    #若键键在字典存在,就会修改这个元素。
    fruits['苹果'] = 15
    print(fruits)
    
    #
    del fruits['榴莲'] # del语句可以删除整个字典和字典的某个元素
    print(fruits)
    fruits.clear()  #清空字典所有元素,变成空字典
    print(fruits)

    更多字典的方法,使用print(dir(dict))来查找

    python的内置函数

    python的内置函数主要来自“__builtins__”模块,__builtins__   模块是python的内置模块,启动python便会自动导入,所以可以使用print(dir(__builtins__))查看‘“__builtins__”模块所有内置函数。具体使用方法参考:1.中文文档。    2.python官方文档 

  • 相关阅读:
    Jira汉化
    maven settings.xml配置用户信息
    Ubuntu 源码方式安装Subversion
    JS表格分页(封装版)
    CSS选择器之基本选择器总结
    有关Web常用字体的研究?
    关于解决不同浏览器之间的兼容性问题
    八一八浏览器内核
    DIV CSS float浮动
    JS对象
  • 原文地址:https://www.cnblogs.com/us-wjz/p/10887815.html
Copyright © 2011-2022 走看看