• python基本类型


    数值类型

    整型

    int_val = 1145143
    print(int_val)
    
    • 1
    • 2

    python中的整型是大数类型。

    一些其他函数

    val = 30
    vlen = val.bit_length() # 转换为二进制的长度
    v_8 = oct(val)
    print(v_8) # 将十进制转为八进制
    v_16 = hex(val) # 将十进制转为十六进制
    v_2 = bin(val) # 将十进制转为二进制
    
    # 将字符串类型的base数字字符串转为十进制数字
    val = int("100",2);
    print(val) # 100(2) ==> 4(10)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    # 延申:整型占用的内存空间
    import sys
    print(sys.getsizeof(0)) # 整数0占用24字节
    print(sys.getsizeof(2 ** 30-1)) # 1 到 2^30 - 1 占用28字节
    print(sys.getsizeof(2 ** 30)) # 2^30 占用32字节
    print(sys.getsizeof(1.)) # 浮点数占固定字节 24字节
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    整数的内存占用大小会随着数值的增加而增加。而浮点数的内存占用大小是固定的。

    浮点型

    字符串类型

    s1 = 'This is a string' # 单引号字符串
    s2 = "This also is a string" # 双引号字符串
    s3 = """  
    There are 
    multi-row 
    string""" # 多行字符串
    s4 = r'\this\has\special\\' # 原始字符串,反斜杠 \ 不会被转义,而是当作普通字符处理
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在双引号字符串中使用单引号不用转义。

    s1 = '\'\''
    s2 = "''"
    print(s2)
    
    • 1
    • 2
    • 3
    s1 = 'This is a string' # 单引号字符串
    s2 = "This also is a string" # 双引号字符串
    s3 = """  
    There are 
    multi-row 
    string""" # 多行字符串
    s4 = r'\this\has\special\\' # 原始字符串,反斜杠 \ 不会被转义,而是当作普通字符处理
    print('.'.join([s1,s2,s3,s4])) # 字符串拼接
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    image-20230909233724902

    字符串常用操作

    • 字符串合并

    image-20230909233934111

    • 字符串重复

    image-20230909234044973

    • 字符串大小写转换

    image-20230909234147837

    image-20230909234222810

    • 字符串查找

    str.find(sub[, start[, end]]): 返回字符串中子串 sub 第一次出现的索引,如果没有找到则返回 -1。可以指定可选参数 startend 来指定查找的起始和结束位置。

    image-20230909234512196

    • 字符串计数

    image-20230909234556530

    • 字符串替代

    str.replace(old, new[, count]): 将字符串中的 old 子串替换为 new 字符串。可选参数 count 指定替换的次数(默认为全部替换)。

    image-20230909234717009

    • 字符串长度

    image-20230909234951069

    字符串切片和索引

    s1 = "Yan Harper"
    print(s1[0], s1[1], s1[-1])
    print(s1[0:3])
    print(s1[-6:]) 
    print(s1[::-1]) # 翻转
    
    • 1
    • 2
    • 3
    • 4
    • 5

    字符串格式化

    %格式化(printf风格) 【版本2.x就有】

    image-20230909235538534

    format格式化【版本3.x常用】

    image-20230909235822736

    f字符串【3.6+】

    image-20230910000129169

    %格式自定义名称(字典形式)

    image-20230910000633285

    format格式化自定义顺序

    image-20230910000824476

    这些不仅仅可以用于打印,还可以用于字符串本身。

    image-20230910000930934

    关于字符串格式化的输入输出还有很多形式。

    print(" There are %d %s, you have to pay %.2f yuan" % (3, "apples", 4.5))
    print("There are {:d} {:s}, you have to pay {:.2f} yuan".format(5, "apples", 23.))
    count = 1
    amount = 3.14
    item = "apples"
    print(f"There are {count:d} {item:s}, you have to pay {amount:.2f}")
    print("There are %(num1)d %(str1)s, you have to pay %(num2).2f"%{"num1" : 3, "str1" : "apples", "num2": 3.1})
    
    print("There are {1} {2}, you have to pay {0:.2f}".format(3.4, 2, "apples"))
    s1 = "%d %.2f" % (3, 45.3434)
    print(s1)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    布尔型

    image-20230916230049376

    print(False)
    print(True)
    print(True * 34)
    
    • 1
    • 2
    • 3

    布尔类型是整数类型的子类

    print(isinstance(True, int))
    
    • 1

    image-20230916230135151

    None类型

    None作为未设定的默认值,用来判断是否被赋值。

    image-20230916230308393

    def func(p = None):
        if p is None:
            return "None"
        else:
            return "YES"
    print(func(34))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 相关阅读:
    SSM之spring注解式缓存redis
    day57
    解Bug之路-应用999线升高
    PHP:类的属性
    Linux学习之vim跳转到特定行数
    【今日文章】:1.关于 $attr $lisenner $slot 用法的思考 2. 关于父子组件传值的思考 3.关于前端日志系统搭建的思考
    问题 K: 哈夫曼树--查找值最小的两个叶节点
    大模型引领未来:探索其在多个领域的深度应用与无限可能【第三章、医疗领域:大模型助力医疗健康创新】
    P1006 [NOIP2008 提高组] 传纸条,棋盘型dp,路径dp
    R语言贝叶斯MCMC:GLM逻辑回归、Rstan线性回归、Metropolis Hastings与Gibbs采样算法实例...
  • 原文地址:https://blog.csdn.net/qq_63432403/article/details/132784139