Python中的类型转换

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

null

Python中有两种类型的类型转换:

  1. 隐式类型转换
  2. 显式类型转换

让我们详细讨论一下。

隐式类型转换

在Python中数据类型的隐式类型转换中,Python解释器会自动将一种数据类型转换为另一种数据类型,而无需任何用户参与。要更清楚地了解该主题,请参见以下示例。

例子:

Python3

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


输出:

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

正如我们所看到的,“x”的类型从“整数”类型自动更改为“浮点”类型。这是python中隐式类型转换的一个简单例子。

显式类型转换

在Python中的显式类型转换中,数据类型由用户根据需要手动更改。下面解释了各种形式的显式类型转换:

1.int(a, (基础) :此函数用于转换 将任何数据类型转换为整数 “’Base’指定 字符串所在的基 如果数据类型是字符串。 2.浮动 :此函数用于转换 将任何数据类型转换为 浮点运算 数字

Python3

# Python code to demonstrate Type conversion
# using int(), float()
# initializing string
s = "10010"
# printing string converting to int base 2
c = int (s, 2 )
print ( "After converting to integer base 2 : " , end = "")
print (c)
# printing string converting to float
e = float (s)
print ( "After converting to float : " , end = "")
print (e)


输出:

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

3.ord(): 此函数用于转换 将字符转换为整数。 4.十六进制() 此函数用于转换 整数到十六进制字符串 . 5.十月(): 此函数用于转换 整数到八进制字符串 .

Python3

# Python code to demonstrate Type conversion
# using  ord(), hex(), oct()
# initializing integer
s = '4'
# printing character converting to integer
c = ord (s)
print ( "After converting character to integer : " ,end = "")
print (c)
# printing integer converting to hexadecimal string
c = hex ( 56 )
print ( "After converting 56 to hexadecimal string : " ,end = "")
print (c)
# printing integer converting to octal string
c = oct ( 56 )
print ( "After converting 56 to octal string : " ,end = "")
print (c)


输出:

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

6.tuple(): 此函数用于 转换为元组 . 7.set(): 此函数返回 转换为set后键入 . 8.列表(): 此函数用于转换 将任何数据类型转换为列表类型 .

Python3

# Python code to demonstrate Type conversion
# using  tuple(), set(), list()
# initializing string
s = 'geeks'
# printing string converting to tuple
c = tuple (s)
print ( "After converting string to tuple : " ,end = "")
print (c)
# printing string converting to set
c = set (s)
print ( "After converting string to set : " ,end = "")
print (c)
# printing string converting to list
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(): 此函数用于 将有序元组(键、值)转换为字典 . 10.str(): 曾经 将整数转换为字符串。 11.复杂(真实,图像): 这个函数 将实数转换为复数。

Python3

# Python code to demonstrate Type conversion
# using  dict(), complex(), str()
# initializing integers
a = 1
b = 2
# initializing tuple
tup = (( 'a' , 1 ) ,( 'f' , 2 ), ( 'g' , 3 ))
# printing integer converting to complex number
c = complex ( 1 , 2 )
print ( "After converting integer to complex number : " ,end = "")
print (c)
# printing integer converting to string
c = str (a)
print ( "After converting integer to string : " ,end = "")
print (c)
# printing tuple converting to expression dictionary
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 : 1After converting tuple to dictionary : {'a': 1, 'f': 2, 'g': 3}

12.chr(编号): 这个函数 将数字转换为相应的ASCII字符。

Python3

# Convert ASCII value to characters
a = chr ( 76 )
b = chr ( 77 )
print (a)
print (b)


输出:

LM 

本文由 曼吉特·辛格(S.南迪尼) .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享