Python 教程之变量(5)—— Python中的类型转换

简介: Python 教程之变量(5)—— Python中的类型转换

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

Python中有两种类型转换:

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

让我们详细讨论它们。


隐式类型转换


在 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中隐式类型转换的一个简单案例。


显式类型转换


在 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 
_

感谢大家的阅读,有什么问题的话可以在评论中告诉我。希望大家能够给我来个点赞+收藏+评论 ,你的支持是海海更新的动力!后面我会持续分享前端 & 后端相关的专业知识。



目录
相关文章
|
2天前
|
安全 Python 容器
|
2天前
|
Python
Python中解包到单独的变量对于字典
【6月更文挑战第20天】
17 11
|
2天前
|
Python
|
3天前
|
Python
Python中解包到单独的变量
【6月更文挑战第19天】
9 4
|
3天前
|
Java Python
Python进阶之旅:深入理解变量作用域、垃圾回收、拷贝机制与异常处理
Python进阶之旅:深入理解变量作用域、垃圾回收、拷贝机制与异常处理
|
3天前
|
Python
Python中解包到嵌套变量
【6月更文挑战第19天】
5 2
|
3天前
|
存储 数据处理 Python
Python中解包到变量并忽略某些元素
【6月更文挑战第19天】
7 2
|
2天前
|
Shell Python
Python教程:return和yield的区别
Python教程:return和yield的区别
6 0
Python教程:return和yield的区别
|
4天前
|
数据采集 存储 数据处理
使用Python获取1688商品详情的教程
使用Python爬取1688商品详情,涉及requests库抓取页面、BeautifulSoup解析HTML,安装必要库如requests、beautifulsoup4、pandas和lxml。通过get_page发送请求,BeautifulSoup解析提取如标题、价格等信息。数据处理后可使用pandas保存至CSV。注意遵守法律法规和网站政策,避免频繁请求。[代码片段及更多详情见链接
|
22小时前
|
机器人 API 开发者
Python基于Mirai开发的QQ机器人保姆式教程(亲测可用)
Python基于Mirai开发的QQ机器人保姆式教程(亲测可用)