给宝宝讲python:(三)、python基本数据类型

给宝宝讲python:(三)、python基本数据类型

标准数据类型

python3中有6种标准的数据类型:

  • Number(数字)
  • String (字符串)
  • List (列表)
  • Tuple (元组)
  • Set (集合)
  • Dictionary(字典)

Python3的6个标准的数据类型中:

  • 不可变数据(3个):Number,String,Tuple
  • 可变数据(3个):List,Dictionary,Set

可以使用type()来变量所指的对象的类型,此外还要使用isinstance(variable, type来判断,返回值为True
,表示变量variable是type类型。

Operation

这里补充一个:python中变量不需要申明,每个变量在使用之前必须赋值,变量赋值后该变量才会被创建,在python中,变量就是变量,它没有数据类型,我们所说的类型是变量所指的内存中对象的类型。

Number

python3支持int,float,bool,complex(复数)
下面的a,b,c,d就称作为变量,它可以指向内存空间中,不同的地址,称之为变量。

In [1]: a,b,c,d = 20, 3.2, True, 3+4j                           
                                                                
In [2]: print(type(a), type(b), type(c), type(d))               
<class 'int'> <class 'float'> <class 'bool'> <class 'complex'>  

可以使用del方法来删除变量,变量的引用数为0时,python虚拟机就会回收这个对象的内存。
Python垃圾回收机制参考这里

In [3]: del d                                                                                                                                                   
In [4]: print(d)                                                                 
---------------------------------------------------------------------------      
NameError                                 Traceback (most recent call last)      
<ipython-input-4-85549cb1de5f> in <module>                                       
----> 1 print(d)                                                                 
                                                                                 
NameError: name 'd' is not defined                                             

String

python中的字符串用引号'或双引号"括起来,如下:

In [5]: string = 'Lovely Girl'                
                                              
In [6]: print('string is:{}'.format(string))  
string is:Lovely Girl                         

List

List是Python中使用最频繁的数据类型,List中的元素类型可以不相同,支持数字,字符串和嵌套列表。
List使用[]括起来,其中的元素使用`,’ 隔离开来,如下:

In [7]: lit = ['wenwen', 20.48, ['wenwen', 5, 3+4j]]

In [8]: print("lit's type:{}, content:{}".format(type(lit), lit))
lit's type:<class 'list'>, content:['wenwen', 20.48, ['wenwen', 5, (3+4j)]]

Tuple

tuple与列表类似,不同之处有二,其一tuple使用()括起来,其二tuple中的元素不能修改。
下面创建一个tuple对象:

In [11]: tuple_1 = ('wenenq', 20.48)

In [12]: tuple(tuple_1)
Out[12]: ('wenenq', 20.48)

未完待续。

发布了30 篇原创文章 · 获赞 21 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_41288824/article/details/102584549