Python基础-python的数据类型

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

类型转换

 In [22]: float(a)                                                       
 Out[22]: 1.0

 In [23]: int(b)                                                         
 Out[23]: 1

 In [24]: float(c)                                                       
 ------------------------------------------------------------------------
 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

如何删除内存中的变量

 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

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

猜你喜欢

转载自blog.csdn.net/qq_43273590/article/details/84328592