python-数据类型

1.整型
2.浮点型
3.字符串类型
4.类型转换
5.如何删除内存中的变量
6.bool类型

1.整型

In [14]: a = 1
In [15]: print(a)
1

In [16]: type(a)
Out[16]: int

2.浮点型

In [17]: b = 1.2

In [18]: type(b)
Out[18]: float

3.字符串类型

In [19]: c = westos
------------------------------------------------------------------------
NameError                              Traceback (most recent call last)
<ipython-input-19-47a30bc3df16> in <module>
----> 1 c = westos

NameError: name 'westos' is not defined
In [20]: c = 'westos'

In [21]: type(c)
Out[21]: str

4.类型转换

In [22]: float(a)	#整型转换为浮点型
Out[22]: 1.0

In [23]: int(b)	#浮点型转换为整型
Out[23]: 1

In [24]: float(c)	#字符串‘westos’不能转换为浮点型和整型
------------------------------------------------------------------------
ValueError                             Traceback (most recent call last)
<ipython-input-24-36b90e4449a0> in <module>
----> 1 float(c)
ValueError: could not convert string to float: 'westos'

In [25]: str(a)	#整型转换为字符型
Out[25]: '1'

In [26]: str(b)	#浮点型转换为字符串型
Out[26]: '1.2'


In [27]: d = '123456'

In [28]: d
Out[28]: '123456'

In [29]: int(d)	#字符串型转换为整型
Out[29]: 123456

In [30]: float(d)	#字符串型转换为浮点型
Out[30]: 123456.0

5.如何删除内存中的变量

In [31]: del a

In [32]: a
------------------------------------------------------------------------
NameError                              Traceback (most recent call last)
<ipython-input-32-3f786850e387> in <module>
----> 1 a

NameError: name 'a' is not defined

6.bool类型
bool:只有两个值(True False) #非0即真

In [34]: a =  1

In [35]: bool(a)
Out[35]: True

In [36]: bool(0)
Out[36]: False

In [37]: bool(1)
Out[37]: True

In [38]: bool(' ')
Out[38]: True

In [39]: bool('')	#空值
Out[39]: False

In [40]: bool(2412414)
Out[40]: True
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值