Python教程之类型转换详解

2022-08-17 17:55:06
目录
隐式类型转换显式类型转换

Python 定义了类型转换函数以将一种数据类型直接转换为另一种数据类型,这在日常和竞争性编程中很有用。本文旨在提供有关某些转换函数的信息。

Python中有两种类型转换:

    隐式类型转换显式类型转换

    让我们详细讨论它们。

    隐式类型转换

    在>

    例子:

    x = 10
    
    print("x is of type:",type(x))
    
    y = 10.6
    print("y is of type:",type(y))
    
    z = x + y
    
    print(z)
    print("z is of type:",type(z))
    

    输出:

    x is of type: <class 'int'>
    y is of type: <class 'float'>
    20.6
    z is of type: <class 'float'>

    正如我们所见,“z”的数据类型自动更改为“float”类型,而一个变量 x 是整数类型,而另一个变量 y 是浮点类型。浮点值没有被转换为整数的原因是由于类型提升允许通过将数据转换为更广泛的数据类型来执行操作而不会丢失任何信息。这是python中隐式类型转换的一个简单案例。

    显式类型转换

    在>

    1. int(a, base)

    此函数将任何数据类型转换为整数。如果数据类型是字符串, 'Base' 指定字符串的基数。

    2. float()

    该函数用于将任何数据类型转换为浮点数 。

    # 使用 int()、float() 演示类型转换的 Python 代码
    
    # 初始化字符串
    s = "10010"
    
    # 打印字符串转换为 int base 2
    c = int(s,2)
    print ("After converting to integer base 2 : ", end="")
    print (c)
    
    # 打印字符串转换为浮点数
    e = float(s)
    print ("After converting to float : ", end="")
    print (e)

    输出:

    After converting to integer base 2 : 18
    After converting to float : 10010.0

    3. ord() : 该函数用于将字符转换为整数。

    4. hex(): 这个函数是将整数转换为十六进制字符串。

    5. oct() : 这个函数是将整数转换为八进制字符串。

    # 使用 ord()、hex()、oct() 演示类型转换的 Python 代码
    
    # 初始化整数
    s = '4'
    
    # 打印字符转换为整数
    c = ord(s)
    print ("After converting character to integer : ",end="")
    print (c)
    
    # 打印整数转换为十六进制字符串
    c = hex(56)
    print ("After converting 56 to hexadecimal string : ",end="")
    print (c)
    
    # 打印整数转换为八进制字符串
    c = oct(56)
    print ("After converting 56 to octal string : ",end="")
    print (c)

    输出:

    After converting character to integer : 52
    After converting 56 to hexadecimal string : 0x38
    After converting 56 to octal string : 0o70

    6. tuple() : 该函数用于转换为元组。

    7. set() : 该函数返回转换为 set 后的类型。

    8. list(): 该函数用于将任何数据类型转换为列表类型。

    # 使用 tuple()、set()、list() 演示类型转换的 Python 代码
    
    # 初始化字符串
    s = 'geeks'
    
    # 打印字符串转换为元组
    c = tuple(s)
    print ("After converting string to tuple : ",end="")
    print (c)
    
    # 打印字符串转换为设置
    c = set(s)
    print ("After converting string to set : ",end="")
    print (c)
    
    # 打印字符串转换为列表
    c = list(s)
    print ("After converting string to list : ",end="")
    print (c)

    输出:

    After converting string to tuple : ('g', 'e', 'e', 'k', 's')
    After converting string to set : {'k', 'e', 's', 'g'}
    After converting string to list : ['g', 'e', 'e', 'k', 's']

    9. dict() : 该函数用于将顺序为 (key,value) 的元组转换为字典。

    10. str() : 用于将整数转换为字符串。

    11. complex(real,imag) : 此函数将实数转换为复数(real,imag)。

    # 使用 dict()、complex()、str() 演示类型转换的 Python 代码
    
    # 初始化整数
    a = 1
    b = 2
    
    # 初始化元组
    tup = (('a', 1) ,('f', 2), ('g', 3))
    
    # 打印整数转换为复数
    c = complex(1,2)
    print ("After converting integer to complex number : ",end="")
    print (c)
    
    # 打印整数转换为字符串
    c = str(a)
    print ("After converting integer to string : ",end="")
    print (c)
    
    # 打印元组转换为表达式字典
    c = dict(tup)
    print ("After converting tuple to dictionary : ",end="")
    print (c)

    输出:

    After converting integer to complex number : (1+2j)
    After converting integer to string : 1
    After converting tuple to dictionary : {'a': 1, 'f': 2, 'g': 3}

    12. chr(number): 该函数将数字转换为对应的ASCII字符。

    # 将 ASCII 值转换为字符
    a = chr(76)
    b = chr(77)
    
    print(a)
    print(b)

    输出:

    LM 
    _

    到此这篇关于Python教程之类型转换详解的文章就介绍到这了,更多相关Python类型转换内容请搜索易采站长站以前的文章或继续浏览下面的相关文章希望大家以后多多支持易采站长站!