一、Python数据类型
Python中有多种数据类型,每种数据类型都有其特定的用途和行为。以下是一些主要的数据类型:
- 整数(int):表示整数,例如
10
、-3
。 - 浮点数(float):表示小数,例如
3.14
、-2.718
。 - 字符串(str):表示文本,例如
"Hello, World!"
、"Python编程"
。 - 布尔值(bool):表示真或假,只有两个值
True
和False
。 - 列表(list):有序的元素集合,可以包含不同类型的元素,例如
[1, 2, 3]
、["apple", "banana", "cherry"]
。 - 元组(tuple):与列表类似,但一旦创建便不可修改,例如
(1, 2, 3)
、("apple", "banana", "cherry")
。 - 字典(dict):键值对的无序集合,例如
{"name": "Alice", "age": 25}
。 - 集合(set):无序且不重复的元素集合,例如
{1, 2, 3}
、{"apple", "banana", "cherry"}
。 - NoneType:表示空值或缺失值,例如
None
。
下面是一个示例代码,用type()函数来查看并输出一些变量的数据类型,以及预输出结果:
# 数字类型
a = 10
print(type(a)) # 预输出: <class 'int'>
b = 3.14
print(type(b)) # 预输出: <class 'float'>
c = 1 + 2j
print(type(c)) # 预输出: <class 'complex'>
# 字符串类型
d = "hello"
print(type(d)) # 预输出: <class 'str'>
# 布尔类型
e = True
print(type(e)) # 预输出: <class 'bool'>
# 列表类型
f = [1, 2, 3]
print(type(f)) # 预输出: <class 'list'>
# 元组类型
g = (1, 2, 3)
print(type(g)) # 预输出: <class 'tuple'>
# 字典类型
h