【无标题】类型转化

这篇博客介绍了Python中常见的数据类型转换,包括字符串转整型、整型转浮点型、整型转字符串以及列表转元组和元组转列表的实践。还演示了eval函数如何将字符串作为Python表达式执行,用于数据类型的转换和计算。
摘要由CSDN通过智能技术生成
# # 将输入的字符串 转化为整型 str字符串的意思
# password = input("请输入数字密码:")
# print("密码是:", password)
# print("密码的数据类型是:", type(password))
# print("===============================")
#
# # 将password转换为整型 password 必须是纯数字
# password_int = int(password)
# print("密码是:", password_int)
# print("密码转换后的数据类型是:", type(password_int))

# 2, 整型转换为浮点型
num1 = 20
print(num1,type(num1))
num2 = float(num1)
# 整型转换为浮点型 在后面直接加.0
print(num2,type(num2))

# 3, 整型转换为字符串类型
num3 = 37
print(num3,type(num3))
str1 = str(num3)
print(str1,type(str1))

# 4, 将列表转换为元组
list1 = ["杨",30,181.1]
print(list1, type(list1))
tuple1 = tuple(list1)
# tuple是元组的意思 使用小括号
print(tuple1, type(tuple1))
print("===================")

5.#将元组转换为列表
tuple2=("马",3,0.9)
print(tuple2,type(tuple2))
list2=list(tuple2)
print(list2,type(list2))

# eval将字符串 作为Python 表达式执行
code1 = "123456"
code2 = eval(code1)
# 将字符串转换为整型
print(code2, type(code2))

# eval直接将里面的内容进行计算 作为Python 代码执行
print("1+1")
print(eval("1+1"))











  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值