Python-数据类型
在本教程中,我们将学习Python数据类型。
以下是我们在Python中使用的数据类型的列表。
- 数值
- 布尔型
- 字符串
- 列表
- 元组
- 组
- 字典
Everything in Python is an object. So, data types are the classes and variables are the objects.
类型函数
我们可以使用type()函数来知道变量的类型。
现在,让我们谈谈Python数据类型。
数
Python中存在三种类型的数字,它们在下面列出。
- 整数
- 浮点数
- 复数
整数
这些数字不带小数部分。
整数示例:1、2、3,-10、0等。
在下面的示例中,我们将创建一个变量并分配值10。
# integer variable x x = 10 # type of x print("Type of x =", type(x), "Value of x =", x)
上面的代码将打印以下输出。
Type of x = <class 'int'> Value of x = 10
浮点数
这些是带小数部分的数字。
示例:3.14。
在以下示例中,我们将创建一个变量并分配浮点值22/7。
# float variable x x = 22/7 # type of x print("Type of x =", type(x), "Value of x =", x)
上面的代码将打印以下输出。
Type of x = <class 'float'> Value of x = 3.142857142857143
复数
复数以" x + yj"的形式写成,其中" x"代表数字的实部," y"代表数字的虚部。
在下面的示例中,我们将创建一个变量并分配复数1 + 2j。
# complex variable x x = 1 + 2j # type of x print("Type of x =", type(x), "Value of x =", x)
上面的代码将打印以下输出。
Type of x = <class 'complex'> Value of x = (1+2j)
布尔型
布尔数据类型可以采用两个值:True和False。
我们使用布尔值表示逻辑思想。
例如,语句"现在在下雨吗?"可以有两个值"是"或者"否",我们用"真"表示"是"或者肯定结果,而用"假"表示否定结果或者否定结果。
在下面的示例中,我们将创建两个布尔变量。
# boolean variables isGameOver = True isUserActive = False # type print("Type of isGameOver =", type(isGameOver), "Value of isGameOver =", isGameOver) print("Type of isUserActive =", type(isUserActive), "Value of isUserActive =", isUserActive)
对于上述程序,我们将获得以下输出。
Type of isGameOver = <class 'bool'> Value of isGameOver = True Type of isUserActive = <class 'bool'> Value of isUserActive = False
字符串
字符串是Unicode字符序列。
我们使用单引号'
和双引号"
来表示Python中的字符串值。
对于多行字符串值,我们使用三引号'''
。
在下面的示例中,我们将创建变量并分配字符串值。
# string str1 = 'Hello World' str2 = "This is string." str3 = '''This is a multiline string.''' # type print("Type of str1 =", type(str1), "Value of str1 =", str1) print("Type of str2 =", type(str2), "Value of str2 =", str2) print("Type of str3 =", type(str3), "Value of str3 =", str3)
我们将获得以下输出。
Type of str1 = <class 'str'> Value of str1 = Hello World Type of str2 = <class 'str'> Value of str2 = This is string. Type of str3 = <class 'str'> Value of str3 = This is a multiline string.
列表
Python中的列表是项目的有序序列。
这些项目可以是多种类型,并且可以重复。
我们使用方括号" []"创建列表,并使用逗号","分隔列表项。
在下面的示例中,我们将创建一个数字列表。
# list myList = [1, 3.14, 1+2j] # type print("Type of myList =", type(myList), "Value of myList =", myList)
上面的代码将为我们提供以下输出。
Type of myList = <class 'list'> Value of myList = [1, 3.14, (1+2j)]
元组
元组是类似于List的不变项的有序序列。
创建元组后,将无法对其进行修改。
我们使用括号"()"在Python中创建元组,并且各项之间用逗号","分隔。
# tuple myTuple = ('Hello World', 1, 3.14, 10+20j) # type print("Type of myTuple =", type(myTuple), "Value of myTuple =", myTuple)
上面的代码将为我们提供以下输出。
Type of myTuple = <class 'tuple'> Value of myTuple = ('Hello World', 1, 3.14, (10+20j))
集合(set)
集合是项目的无序唯一集合。
我们使用大括号" {}"在Python中创建一个集合,并使用逗号","分隔各项。
在以下示例中,我们将创建一个集合。
# set mySet = {1, 3.14, 1-2j} # type print("Type of mySet =", type(mySet), "Value of mySet =", mySet)
我们将获得以下输出。
Type of mySet = <class 'set'> Value of mySet = {1, 3.14, (1-2j)}
字典
Python中的字典是键-值对的无序集合。
我们使用大括号" {}"创建字典,并使用冒号":"将键与值分开。
字典的每一对都以逗号,
分隔。
在下面的示例中,我们将创建一个字典。
# dictionary myDictionary = { 'name': 'theitroad', 'score': 9.1, 'isOnline': False } # type print("Type of myDictionary =", type(myDictionary), "Value of myDictionary =", myDictionary)
我们将获得以下输出。
Type of myDictionary = <class 'dict'> Value of myDictionary = {'name': 'theitroad', 'score': 9.1, 'isOnline': False}