Python中的类型转换
Python定义了类型转换函数以将一种数据类型直接转换为另一种数据类型,这在日常和竞争性编程中很有用。本文旨在提供有关某些转换函数的信息。
Python中有两种类型转换:
- 隐式类型转换
- 显式类型转换
让我们详细讨论它们。
隐式类型转换
在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))
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)
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)
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)
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)
Python3
# Convert ASCII value to characters
a = chr(76)
b = chr(77)
print(a)
print(b)
输出:
x is of type:
y is of type:
20.6
x is of type:
正如我们所见,'x' 的类型自动从“integer”类型更改为“float”类型。这是Python中隐式类型转换的一个简单案例。
显式类型转换
在Python中的显式类型转换中,数据类型由用户根据需要手动更改。下面解释了各种形式的显式类型转换:
1. int(a, base) :此函数将任何数据类型转换为整数。 'Base' 指定数据类型为字符串时 string 所在的字符串。
2.float() :该函数用于将任意数据类型转换为浮点数
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 : 18
After converting to float : 10010.0
3. ord() :该函数用于将字符转换为整数。
4. hex():这个函数是将整数转换为十六进制字符串。
5. oct() :这个函数是将整数转换为八进制字符串。
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 : 52
After converting 56 to hexadecimal string : 0x38
After converting 56 to octal string : 0o70
6. tuple() :该函数用于转换为元组。
7. set() :该函数返回转换为 set 后的类型。
8. list():该函数用于将任何数据类型转换为列表类型。
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() :该函数用于将顺序为 (key,value) 的元组转换为字典。
10. str() :用于将整数转换为字符串。
11. complex(real,imag) :此函数将实数转换为复数(real,imag)。
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 : 1
After converting tuple to dictionary : {'a': 1, 'f': 2, 'g': 3}
12. chr(number):该函数将数字转换为对应的ASCII字符。
Python3
# Convert ASCII value to characters
a = chr(76)
b = chr(77)
print(a)
print(b)
输出:
L
M