变量可以保存值,并且每个值都有一个数据类型。 Python是一种动态类型化的语言;因此,无涯教程在声明变量时不必定义变量的类型。解释器隐式地将值与其类型绑定。
a = 5
变量 a 保留整数5,没有定义其类型。 Python解释器会自动将变量 a 解释为整数类型。
Python能够检查程序中使用的变量的类型。 Python提供了 type()函数,该函数返回传递的变量的类型。
考虑以下示例,以定义不同数据类型的值并检查其类型。
输出:
<type 'int'> <type 'str'> <type 'float'>
变量可以容纳不同类型的值。例如,一个人的名字必须存储为字符串,而其id必须存储为整数。
Python提供了各种标准数据类型,这些数据类型定义了每种数据类型的存储方法。 Python中定义的数据类型如下。
在本教程的这一部分中,将简要介绍上述数据类型。在本教程的后面将详细讨论其中的每一个。
数字存储数值。整数,浮点数和复数值属于Python Number数据类型。 Python提供了type()函数来知道变量的数据类型。同样,isinstance()函数用于检查属于特定类的对象。
将数字分配给变量后,Python会创建Number对象。例如;
a = 5 print("The type of a", type(a)) b = 40.5 print("The type of b", type(b)) c = 1+3j print("The type of c", type(c)) print(" c is a complex number", isinstance(1+3j,complex))
输出:
The type of a <class 'int'> The type of b <class 'float'> The type of c <class 'complex'> c is complex number: True
Python支持三种类型的数字数据。
可以将字符串定义为引号中表示的字符序列。在Python中可以使用单引号,双引号或三引号来定义字符串。
在字符串处理的情况下,操作符+用于连接两个字符串,因为操作" hello" +" python" 返回" hello python" 。
运算符*被称为重复运算符,因为运算" Python" * 2返回'Python Python'。
以下示例说明了Python中的字符串。
示例-1
输出:
string using double quotes A multiline string
考虑下面的字符串处理示例。
示例-2
str1 = 'hello learnfk' #字符串 str1 str2 = ' how are you' #字符串 str2 print (str1[0:2]) #使用切片运算符打印前两个字符 print (str1[4]) #打印字符串的第四个字符 print (str1*2) #打印字符串两次 print (str1 + str2) #打印 str1 和 str2 的连接
输出:
he o hello learnfkhello learnfk hello learnfk how are you
Python列表类似于 C 中的数组。但是,列表可以包含不同类型的数据。列表中存储的项目用逗号(,)分隔,并括在方括号[]中。
无涯教程可以使用slice [:]运算符来访问列表中的数据。串联运算符(+)和重复运算符(*)对列表的处理方式与对字符串的处理方式相同。
链接:https://www.learnfk.comhttps://www.learnfk.com/python3/python-data-types.html
来源:LearnFk无涯教程网
考虑以下示例。
list1 = [1, "hi", "Python", 2] #检查给定列表的类型 print(type(list1)) #打印列表1 print (list1) # 列表切片 print (list1[3:]) print (list1[0:2]) # 使用 + 运算符进行列表连接 print (list1 + list1) # 使用 * 运算符重复列表 print (list1 * 3)
输出:
[1, 'hi', 'Python', 2] [2] [1, 'hi'] [1, 'hi', 'Python', 2, 1, 'hi', 'Python', 2] [1, 'hi', 'Python', 2, 1, 'hi', 'Python', 2, 1, 'hi', 'Python', 2]
元组在很多方面都类似于列表。像列表一样,元组也包含不同数据类型的项的集合。元组的项目用逗号(,)分隔并括在括号()中。
元组是只读的数据结构,因为无法修改元组项目的大小和值。
tup = ("hi", "Python", 2) # 检查 tup 的类型 print (type(tup)) #打印元组 print (tup) #切片 print (tup[1:]) print (tup[0:1]) # 使用 + 运算符连接 print (tup + tup) # 使用 * 运算符返回 print (tup * 3) # 为 tup 增加价值。它会抛出一个错误。 t[2] = "hi"
输出:
<class 'tuple'> ('hi', 'Python', 2) ('Python', 2) ('hi',) ('hi', 'Python', 2, 'hi', 'Python', 2) ('hi', 'Python', 2, 'hi', 'Python', 2, 'hi', 'Python', 2) Traceback (most recent call last): File "main.py", line 14, in <module> t[2] = "hi"; TypeError: 'tuple' object does not support item assignment
字典是一组键值对的无序集合。这就像一个关联数组或哈希表,其中每个键都存储一个特定值。键可以保存任何原始数据类型,而值是一个任意的Python对象。
字典中的项目用逗号(,)分隔,并括在大括号{}中。考虑以下示例。
d = {1:'Jimmy', 2:'Alex', 3:'john', 4:'mike'} # 打印字典 print (d) # 使用键访问值 print("1st name is "+d[1]) print("2nd name is "+ d[4]) print (d.keys()) print (d.values())
输出:
1st name is Jimmy 2nd name is mike {1: 'Jimmy', 2: 'Alex', 3: 'john', 4: 'mike'} dict_keys([1, 2, 3, 4]) dict_values(['Jimmy', 'Alex', 'john', 'mike'])
布尔类型提供两个内置值True和False。这些值用于确定给定语句为true或false。它由bool类表示。 True可以由任何非零值或'T'表示,而false可以由0或'F'表示。考虑以下示例。
输出:
<class 'bool'> <class 'bool'> NameError: name 'false' is not defined
Python Set是数据类型的无序集合。它是可迭代的,可变的(创建后可以修改),并且具有独特的元素。在设置中,元素的顺序未定义;它可能会返回元素的更改顺序。该集合是通过使用内置函数set()创建的,或者一系列元素在花括号中传递并用逗号分隔。它可以包含各种类型的值。考虑以下示例。
# 创建空集 set1 = set() set2 = {'James', 2, 3,'Python'} #打印设定值 print(set2) # 向集合中添加元素 set2.add(10) print(set2) #从集合中移除元素 set2.remove(2) print(set2)
输出:
{3, 'Python', 'James', 2} {'Python', 'James', 3, 2, 10} {'Python', 'James', 3, 10}
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)