zoukankan      html  css  js  c++  java
  • python-内置方法、公共方法

    abs():取绝对值
    chr():print(chr(97)) #值a,返回ascii码的对应表
    ord():print(ord('a')) #值97,与chr相反
    dir():查看里面都有什么方法
    如:a = {}
    print(dir(a))
    eval() #把字符串变成字典

    内置函数

    注意:len在操作字典数据时,返回的是键值对个数。

    # del用法总结
    # 可以配合列表使用
    my_list = [1, 2, 3]
    del my_list[1]
    # 可以配合字典
    my_dict = {"name": "小红", "age": 22, "no": "009"}
    del my_dict["age"]
    # 可以提前销毁一个对象
    num = 10
    # 在程序没有结束的时候 程序员提前杀死了这个对象 提前释放内存
    del num
    print(num)  # NameError: name 'num' is not defined
    # 监听当程序结束后 python会执行一个方法 del num 告知系统回收内存
    # 统计字符串中,各个字符的个数
    # 比如:"hello world" 字符串统计的结果为: h:1 e:1 l:3 o:2 d:1 r:1 w:1
    # 01 准备一个字符串
    my_str = "hello world"
    # 字符串的有一个count
    # 循环 -> for循环
    my_list = []
    # 循环
    for c in my_str:
        # 去除空格 且 这个字符列表没有保存过
        if c != " " and c not in my_list:
            # 计算每个字符出现的次数
            count = my_str.count(c)
            print("%s:%d" % (c, count))
            # 保存下这个字符
            my_list.append(c)
    # 01 准备一个字符串
    my_str = "hello world"
    new_list = my_str.split()  # 去空格变成列表
    new_str = "".join(new_list)  # 把列表的元素组成一个字符串
    print(new_str)  # helloworld
    my_list = []
    for c in new_str:
        # 这个字符列表没有保存过
        if c not in my_list:
            # 计算每个字符出现的次数
            count = my_str.count(c)
            print("%s:%d" % (c, count))
            # 保存下这个字符
            my_list.append(c)

    总结:

    开区间闭区间

    import random
    # 随机数
    random.randint(x, y)  # [x, y]
    # 范围
    range(x, y)  # [x, y)
    # 切片 -> 字符串中
    "a"[x:y:步长]  # [x, y)

    数据类型 是可变 还是不可变
    不可变的:int float bool str 元组
    可变的:列表 字典

    公共方法--------------

    运算符

    # 运算符 +
    # 列表
    my_list1 = [1, 2]
    my_list2 = [3, 5]
    ret1 = my_list2 + my_list1  # 注意顺序
    print(ret1)  # [3, 5, 1, 2]
    # 字符串
    name = "小明"
    print("我叫%s" % name)  # 占位符
    ret = "我叫" + name  # 我叫小明
    ret1 = "我叫%s" % name
    print(ret1)
    # 类型不统一 使用str()转类型
    name = "小明"
    age = 18
    ret2 = "我叫" + name + "年龄" + str(age)
    print(ret2)  # 我叫小明年龄18
    # 元组
    ret3 = (1, 4) + (6, 9)
    print(ret3)  # (1, 4, 6, 9)
    # 运算符 *
    # 字符串
    my_str = "=="*20
    print(my_str)  # ========================================
    # 列表
    my_list = ["hello", "world"]*5
    print(my_list)  # ['hello', 'world', 'hello', 'world', 'hello', 'world', 'hello', 'world', 'hello', 'world']
    # 元组
    my_tuple = (11,)*5
    print(my_tuple)  # (11, 11, 11, 11, 11)
    # in
    # 字符串
    my_name = "world"
    if "l" in my_name:
        print("存在")
    # 元组
    my_tuple = (1, 3, 5)
    if 1 in my_tuple:
        print("存在")
    # 字典
    my_dict = {"name": "小红", "age": 22, "no": "009"}
    # 使用in 在字典中其实是判断的是key存不存在
    if "name" in my_dict:
        print("存在")
    # 就是想查看值到底有没有
    if "小红" in my_dict.values():
        print("存在")

  • 相关阅读:
    Top 10 Product Manager Skills To Boost Your Resume In 2021
    大数据知识梳理
    B端产品如何设计权限系统?
    华三盒式交换机MAC、ARP、Route性能表项参数查询
    中了传说中的挖矿病毒
    SqlServer 2019 事务日志传送
    docker中生成的pdf中文是方框的解决方案
    The Live Editor is unable to run in the current system configuration
    2021 面试题大纲
    五分钟搞定Docker安装ElasticSearch
  • 原文地址:https://www.cnblogs.com/peiya/p/11994147.html
Copyright © 2011-2022 走看看