Python教程-Python 数据类型
每个值都有一个数据类型,变量可以保存值。Python 是一种强大的组合语言;因此,在声明变量时不必定义变量的类型。解释器会隐式地将值绑定到其类型上。
a = 5
我们没有指定变量 a 的类型,它的值是整数五。Python 解释器将自动将变量解释为整数。
我们可以通过 Python 的 type() 函数来验证程序中使用的变量的类型。Python 中的 type() 函数返回传递变量的类型。
考虑以下示例,用于定义和验证各种数据类型的值。
a=10
b="Hi Python"
c = 10.5
print(type(a))
print(type(b))
print(type(c))
输出:
<type 'int'>
<type 'str'>
<type 'float'>
标准数据类型
变量可以包含各种值。另一方面,人的身份证号必须存储为整数,而他们的名字必须存储为字符串。
Python 指定了每种标准数据类型的存储方法。以下是 Python 定义的数据类型列表。
- 数字(Numbers)
- 序列类型(Sequence Type)
- 布尔值(Boolean)
- 集合(Set)
- 字典(Dictionary)
在本教程的本节中,将简要讨论这些数据类型。我们稍后会在本教程中详细讨论每个数据类型。
数字(Numbers)
数值被存储在数字中。整数、浮点数和复数属于 Python 的数字数据类型。Python 提供 type() 函数来确定变量的数据类型。instance() 函数用于检查一个对象是否属于特定类。
当数字赋值给一个变量时,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 支持三种类型的数值数据。
- 整数(Int): 整数值可以是任何长度,如 10、2、29、-20、-150 等。在 Python 中,整数可以是任何您想要的长度。它的值属于 int 类型。
- 浮点数(Float): 浮点数存储浮点数,如 1.9、9.902、15.2 等。它可以精确到小数点后 15 位。
- 复数(Complex): 复数包含一个有序对,即 x + iy,其中 x 和 y 分别表示实部和虚部。复数例如 2.14j、2.0 + 2.3j 等。
序列类型(Sequence Type)
字符串(String)
引号中的字符序列可以用来描述字符串。可以使用单引号、双引号或三引号来在 Python 中定义字符串。
在 Python 中,处理字符串是一个简单的任务,因为 Python 提供了内置的函数和操作符来执行字符串操作。
在处理字符串时,“hello” + “ python” 操作返回 “hello python”,加号操作符(+)用于合并两个字符串。
由于操作 "Python" 2 返回 "Python",因此乘号操作符()被称为重复操作符。
以下是 Python 字符串的示例。
示例 - 1
str = "string using double quotes"
print(str)
s = '''''A multiline
string'''
print(s)
输出:
string using double quotes
A multiline
string
看一下下面的字符串处理示例。
示例 - 2
str1 = 'hello javatpoint' #string str1
str2 = ' how are you' #string str2
print (str1[0:2]) #printing first two character using slice operator
print (str1[4]) #printing 4th character of the string
print (str1*2) #printing the string twice
print (str1 + str2) #printing the concatenation of str1 and str2
输出:
he
o
hello javatpointhello javatpoint
hello javatpoint how are you
列表(List)
在 Python 中,列表类似于 C 中的数组,但列表可以包含不同类型的数据。在列表中存储的项目用逗号(,)分隔,并放在方括号 [] 中。
要访问列表的数据,我们可以使用切片 [:] 操作符。与它们在字符串中的工作方式相似,列表由连接操作符(+)和重复操作符(*)处理。
看一下以下示例。
示例:
list1 = [1, "hi", "Python", 2]
#Checking type of given list
print(type(list1))
#Printing the list1
print (list1)
# List slicing
print (list1[3:])
# List slicing
print (list1[0:2])
# List Concatenation using + operator
print (list1 + list1)
# List repetation using * operator
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]
元组(Tuple)
在许多方面,元组与列表相似。元组也包含来自不同数据类型的项目集合,它们与列表一样。用圆括号 () 将元组的组成部分与彼此分隔开。
由于不能修改元组中的项的大小或值,它是一个只读数据结构。
让我们看一个简单的元组示例。
示例:
tup = ("hi", "Python", 2)
# Checking type of tup
print (type(tup))
#Printing the tuple
print (tup)
# Tuple slicing
print (tup[1:])
print (tup[0:1])
# Tuple concatenation using + operator
print (tup + tup)
# Tuple repatation using * operator
print (tup * 3)
# Adding value to tup. It will throw an error.
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
字典(Dictionary)
字典是一组键值对,按任意顺序排列。它为每个键存储一个特定的值,类似于关联数组或哈希表。值可以是任何 Python 对象,而键可以保持任何原始数据类型。
在字典中,逗号(,)和花括号用于分隔项目。
看一下以下示例。
d = {1:'Jimmy', 2:'Alex', 3:'john', 4:'mike'}
# Printing dictionary
print (d)
# Accesing value using keys
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'])
布尔值(Boolean)
布尔类型有两个默认值:True 和 False。这些值用于判断给定的陈述是真还是假。这由类名指示。False 可以用 0 或字母 "F" 表示,而 true 可以用任何非零的值表示。
看一下以下示例。
# Python program to check the boolean type
print(type(True))
print(type(False))
print(false)
输出:
<class 'bool'>
<class 'bool'>
NameError: name 'false' is not defined
集合(Set)
Python 集合是无序的数据类型。它是可迭代的、可变的(创建后可更改)并且具有独特的元素。集合的元素没有固定的顺序;它可能返回元素的不同顺序。要创建集合,可以通过花括号和逗号传递元素序列,也可以使用内置函数 set() 来创建集合。它可以包含不同类型的值。
看一下以下示例。
# Creating Empty set
set1 = set()
set2 = {'James', 2, 3,'Python'}
#Printing Set value
print(set2)
# Adding element to the set
set2.add(10)
print(set2)
#Removing element from the set
set2.remove(2)
print(set2)
输出:
{3, 'Python', 'James', 2}
{'Python', 'James', 3, 2, 10}
{'Python', 'James', 3, 10}