python语法:变量与数据类型

python 所有的数据类型: int float complex str bool list tuple dict set

1.1 数字

1.1.1 int 整型

python2.2中, 整型溢出会自动转为长整型

强制类型转换 int()

a = 100002.6
a = int(a)
print(type(a)) # <class 'int'>
print(hex(a)) # 0x186a2
print(type(hex(a))) # <class 'str'>
print(oct(a)) # 0o303242
print(type(oct(a))) # <class 'str'>

1.1.2 long 长整型

python3中, 没有长整型,只用int来表达所有整数类型
强制类型转换 long()

long类型的强制类型转换只在python2中有效

a = 12.6
print(type(long(a))) # <class 'long'> 

1.1.3 float 浮点型

强制类型转换 float()

a = 12
print(type(float(a))) # <class 'float'> 

1.1.4 complex 复数

创建一个复数 complex()

a = complex(12, 5)
print(a) # (12+5j)
print(type(a)) # <class 'complex'>

1.2 str 字符串

强制类型转换str()

a = 12345
print(str(a)) # 12345
print(type(str(a))) # <class 'str'>

1.3 bool 布尔

强制类型转换bool()

a = -1
print(bool(a)) # True
print(type(bool(a))) # <class 'bool'>

1.3 list 列表

强制类型转换list()

a = "abcdef"
print(list(a)) # ['a', 'b', 'c', 'd', 'e', 'f']
print(type(list(a))) # <class 'list'>

1.4 tuple 元祖

强制类型转换tuple()

a = "abcdef"
print(tuple(a)) # ('a', 'b', 'c', 'd', 'e', 'f')
print(type(tuple(a))) # <class 'tuple'>

1.5 dict 字典

强制类型转换dict()

a = (("abc", 132), ("def", 456), (456, 7))
print(dict(a)) # {'abc': 132, 'def': 456, 456: 7}
print(type(dict(a))) # <class 'dict'>

1.6 set 集合

强制类型转换set()

a = [12, 3.5, "hello"]
print(set(a)) # {'hello', 3.5, 12}
print(type(set(a))) # <class 'set'>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

__万波__

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值