• 【Python基础入门5】关于数据类型


    目录

    一、字符串类型

    二、整数类型

    附:十进制转任意(二、八、十六)进制

    三、浮点类型

    四、布尔类型

    五、类型转换

    1、str()函数

    2、int()函数

    3、float()函数


    一、字符串类型

    字符串也被称为“不可变的字符序列”,其可以使用 单引号双引号三引号 """ """ 来定义,来定义:

    注意:三引号 及 """ """ 定义的字符串可分布在连续多行

    1. a = 'Welcome to Python' # 单引号
    2. b = "Welcome to Python" # 双引号
    3. c = '''Welcome to
    4. Python'''
    5. d = """Welcome to
    6. Python"""
    7. print(a)
    8. print(b)
    9. print(c)
    10. print(d)
    11. >> Welcome to Python
    12. >> Welcome to Python
    13. >> Welcome to
    14. >> Python
    15. >> Welcome to
    16. >> Python

    二、整数类型

    我们常见的整数类型一般都是十进制的,但它其实还有常用的二进制、八进制、十六进制的表达方式,其中:

    二进制 —>以0b开头,用bin()进行转换

    1. print(bin(10))
    2. >> 0b1010

    八进制 —>以0o开头,用oct()进行转换

    1. print(oct(100))
    2. >> 0o144

    十六进制 —>以0x开头,用hex()进行转换

    1. print(hex(1000))
    2. >> 0x3e8

    注意: 以上述方法进行类型转换之后,我们的转换后的整数……会因此变为字符串类型

    1. print(bin(10), type(bin(10)))
    2. print(oct(100), type(oct(100)))
    3. print(hex(1000), type(hex(1000)))
    4. >> 0b1010 <class 'str'>
    5. >> 0o144 <class 'str'>
    6. >> 0x3e8 <class 'str'>

    当然,我们也可以从 二、八、十六进制转回十进制:

    1. a = '1010'
    2. b = '144'
    3. c = '3e8'
    4. print(int(a, 2)) # 二转十
    5. print(int(b, 8)) # 八转十
    6. print(int(c, 16)) # 十六转十
    7. >> 10
    8. >> 100
    9. >> 1000

    注意: 此时我们无需加上开头的0b、0o或0x,但转换前他们必须为字符串类型

    附:十进制转任意(二、八、十六)进制

    1. from pythonds.basic import Stack
    2. def convert(decNumber, base):
    3. num = "0123456789ABCDEF"
    4. remstack = Stack()
    5. while decNumber > 0:
    6. rem = decNumber % base
    7. remstack.push(rem)
    8. decNumber //= base
    9. binString = ""
    10. while not remstack.isEmpty():
    11. binString += num[remstack.pop()]
    12. return binString
    13. n, m = map(int, input().split())
    14. if m == 2:
    15. print(f"0b{convert(n, m)}")
    16. elif m == 8:
    17. print(f"0o{convert(n, m)}")
    18. else:
    19. print(f"0x{convert(n, m)}")

    三、浮点类型

    浮点数由整数部分和小数部分组成,同时其具有存储的不精确性(例如下面的1.1 + 2.2)

    1. a = 3.1415
    2. print(a, type(a))
    3. print(1.1 + 2.2)
    4. >> 3.1415 <class 'float'>
    5. >> 3.3000000000000003

    解决方法: 用模块decimal

    1. from decimal import Decimal
    2. print(Decimal('1.1') + Decimal('2.2'))
    3. >> 3.3

    四、布尔类型

    用来表示真或假的值,True为真,False为假。布尔值可以转化为整数:True —> 1,False —> 0

    1. a = True
    2. b = False
    3. print(a, type(a))
    4. print(b, type(b))
    5. print(a + 1)
    6. print(b + 1)
    7. >> True <class 'bool'>
    8. >> False <class 'bool'>
    9. >> 2
    10. >> 1

    五、类型转换

    Why do we need 类型转换?答:最基本的是,为了将不同数据类型的数据拼接起来。

    例如:

    1. name = 'cheems'
    2. age = 16
    3. print('My dog ' + name + ' is ' + age + ' years old')
    4. print('My dog ' + name + ' is ' + str(age) + ' years old')

    第一个输出必然报错:TypeError: can only concatenate str (not "int") to str

    问题就出在age为int变量,因此利用str()函数转化后 —> 输出二,可以正常输出

    1、str()函数

    将其他类型转为字符串

    1. a = 16
    2. b = True
    3. c = 3.14
    4. print(str(a), type(str(a)))
    5. print(str(b), type(str(b)))
    6. print(str(c), type(str(c)))
    7. >> 16 <class 'str'>
    8. >> True <class 'str'>
    9. >> 3.14 <class 'str'>

     同时,我们也可以用引号进行转换(无论是单引号双引号三引号 """ """

    1. print('123', type('123'))
    2. print("True", type("True"))
    3. print('''3.14''', type('''3.14'''))
    4. print("""123""", type("""123"""))
    5. >> 123 <class 'str'>
    6. >> True <class 'str'>
    7. >> 3.14 <class 'str'>
    8. >> 123 <class 'str'>

    2、int()函数

    注意:(1)文字类和小数类字符串均无法转化成整数;(2)浮点数转为整数时,抹零取整(不进行四舍五入)。

    1. a = '16'
    2. b = True
    3. c = 3.14
    4. d = '3.14'
    5. e = 'Python'
    6. print(int(a), type(int(a)))
    7. print(int(b), type(int(b)))
    8. print(int(c), type(int(c)))
    9. print(int(d), type(int(d)))
    10. print(int(e), type(int(e)))
    11. >> 16 <class 'int'>
    12. >> 1 <class 'int'>
    13. >> 3 <class 'int'>
    14. >> ValueError: invalid literal for int() with base 10: '3.14'
    15. >> ValueError: invalid literal for int() with base 10: 'Python'

    3、float()函数

    注意:(1)文字类字符串均无法转化成浮点数;(2)整数转为浮点数时,末尾添上.0。

    1. a = '16'
    2. b = True
    3. c = 114514
    4. d = '3.14'
    5. e = 'Python'
    6. print(float(a), type(float(a)))
    7. print(float(b), type(float(b)))
    8. print(float(c), type(float(c)))
    9. print(float(d), type(float(d)))
    10. print(float(e), type(float(e)))
    11. >> 16.0 <class 'float'>
    12. >> 1.0 <class 'float'>
    13. >> 114514.0 <class 'float'>
    14. >> 3.14 <class 'float'>
    15. >> ValueError: could not convert string to float: 'Python'

  • 相关阅读:
    pytest测试报告邮件发送格式调整(基于Allure的测试报告)
    ARM32开发——GPIO输入
    持续集成/持续部署(3)Jenkins(2)
    Android 应用退出方式
    贝叶斯分位数回归、lasso和自适应lasso贝叶斯分位数回归分析免疫球蛋白、前列腺癌数据...
    javaH5醉美南湾湖网站设计计算机毕业设计MyBatis+系统+LW文档+源码+调试部署
    创建一个给定形状的数组,并用给定的值填充numpy.full()
    创建无序列表
    deque(双端数组)——STL
    Python忽略NoData计算多张遥感影像的像元平均值:whitebox库
  • 原文地址:https://blog.csdn.net/yewanyuan/article/details/126566505