Python提供不同的数据类型来存储值。Python中的每个数据类型实际上都是类的实例。Python提供了以下数据类型。
- 数字
- 布尔值
- 字符串
- 列表
- 元组
- 套
- 字典
- 二进制类型
Python数字
Python数字由两种主要类型组成:整数、浮点和复数。
Integer
数字可以存储任何可以是正数或负数的十进制数。整数可以是受可用内存限制的任何长度。
a = 5b = 10c = a + bprint(c)
![图片[1]-Python数据类型-yiteyi-C++库](https://www.yiteyi.com/wp-content/uploads/2020/03/poftut_image-182.png)
Floating point
数字对于存储浮点值很有用。
a = 1.2b = 2.8c = a + bprint(c)
![图片[2]-Python数据类型-yiteyi-C++库](https://www.yiteyi.com/wp-content/uploads/2020/03/poftut_image-183.png)
Complex
数字主要由数学家和数据专家使用,它们由实部和虚部组成。
a = 1+2jb = 2+1j
Python布尔值
Python布尔值将存储基本逻辑 True
或 False
。布尔值也用于不同的布尔运算,如AND、OR、XOR、NOT等。
a = Trueb = Falsec = a | bprint(c)#Output will be Truec = a & bprint(c)#Output will be False
![图片[3]-Python数据类型-yiteyi-C++库](https://www.yiteyi.com/wp-content/uploads/2020/03/poftut_image-184.png)
Python字符串
Python字符串数据类型是由单个或多个Unicode字符组成的序列。为了提供字符串值,可以使用单引号或双引号。对于多行字符串,应使用三个引号。
name ="ismail"myname = 'ismail'print(name)#Output will beismailprint(myname)#Output will be ismailprint(myname[1])#Output will be s
![图片[4]-Python数据类型-yiteyi-C++库](https://www.yiteyi.com/wp-content/uploads/2020/03/poftut_image-185.png)
Python列表
Python列出存储单个或多个对象或其他数据类型的序列的数据类型。列表在其他编程语言中也称为数组。列表非常灵活,可以在单个列表中存储多种类型的数据。
mylist = [ 1 , "two" , 3 , "four" , 5.0 ]len(mylist)#Output will 5 becuase there are 5 elements in this list
![图片[5]-Python数据类型-yiteyi-C++库](https://www.yiteyi.com/wp-content/uploads/2020/03/poftut_image-186.png)
我们可以看到,第一项是一个数字,其中第二项是字符串,最后一项是浮点数。
相关文章: 如何使用Python打印函数教程及示例
Python元组
Python Tuple与Python列表非常相似。除了元组是只读的,这意味着它不能在创建后更新或更改。但另一方面,列表可以在创建后更新或更改。为了更新元组,应该使用旧项创建一个新元组。
mytuple = ( 1 , "two" , 3 , "four" , 5.0 )len(mytuple)#Output will be 5
![图片[6]-Python数据类型-yiteyi-C++库](https://www.yiteyi.com/wp-content/uploads/2020/03/poftut_image-187.png)
Python集
pythonset数据类型提供了数学集合的特性,其中每个项都是唯一的。即使是多次定义的元素,只有一个实例会存储在集合中。元素项不支持索引,这意味着不能使用索引号访问元素。
myset = { 1 , 1, 1, "two" , 3 , "four" , 5.0 }len(myset)#Output will be 5print(myset)#The output will be{1, 3, 5.0, 'two', 'four'}
![图片[7]-Python数据类型-yiteyi-C++库](https://www.yiteyi.com/wp-content/uploads/2020/03/poftut_image-188.png)
我们可以看到,在名为myset的集合中,1被定义了3次,但只存储了一个1,myset的长度是5,即使定义提供了8项。
Python词典
Python字典是一种数据类型,它将存储键值对或项。它被称为字典,因为它的结构类似于字典。每把钥匙都与价值有关。字典经过优化,可以通过使用键来检索值。
mydict = { 1:'value' , 'key':2 , 'mykey':'myvalue' , 'name':'ismail'}len(mydict)#The output will be 4
![图片[8]-Python数据类型-yiteyi-C++库](https://www.yiteyi.com/wp-content/uploads/2020/03/poftut_image-189.png)
我们可以在示例中看到,我们可以使用值的键访问这些值。键可以是整数,也可以是字符串。
Python二进制类型
Python字节数据类型用于以字节格式存储给定的数据。字节数据类型由 b
在下面的值之前。
mybyte= b'Hello Poftut.com'
![图片[9]-Python数据类型-yiteyi-C++库](https://www.yiteyi.com/wp-content/uploads/2020/03/poftut_image-190.png)
当我们逐个打印字节时,我们可以看到字符串ASCII值被存储为一个字节。
查找Python数据类型
Python编程语言数据类型是在将初始值设置为变量时指定的。所以变量数据类型没有特定的标志。这使得查找数据类型有点复杂,但是 type()
函数可用于获取所提供变量或结构的数据类型。
a = 1b = 2.0c = 1 + 2jmyname = "İsmail Baydan"mylist = [ 1 , "two" , 3 , "four" , 5.0 ]mytuple = ( 1 , "two" , 3 , "four" , 5.0 )myset = { 1 , 1, 1, "two" , 3 , "four" , 5.0 }mydict = { 1:'value' , 'key':2 , 'mykey':'myvalue' , 'name':'ismail'}mybyte= b'Hello Poftut.com'type(a)#The output willtype(b)#The output will type(c)#The output will type(myname)#The output will type(mylist)#The output will type(mytuple)#The output will type(myset)#The output will type(mydict)#The output will type(mybyte)#The output will
![图片[10]-Python数据类型-yiteyi-C++库](https://www.yiteyi.com/wp-content/uploads/2020/03/poftut_image-191.png)