数据类型
不可变数据:Number、String、Tuple
可变数据:List、Dictionary、Set
Number
Python3 支持 int、float、bool、complex(复数)
integer_example = 42
float_example = 3.14
complex_example = 1 + 2j
String
单双引号均可
string_example = "Hello, World!"
string_example2 = """Hello
World!"""
bool (布尔类型)
在 Python 中,Ture
和 False
表示布尔值
bool
是int
的子类,因此布尔值可以被看作整数来使用,其中Ture
等价于1布尔类型可以和其他数据类型进行比较,在比较时,会将
Ture
视为1,False
视为0布尔类型可以和逻辑运算符一起使用,包括
and
、or
、not
。这些运算符可以用来组合多个布尔表达式,生成一个新的布尔值。布尔类型也可以被转换成其他数据类型,比如整数、浮点数和字符串。在转换时,True 会被转换成 1,False 会被转换成 0。
可以使用
bool()
函数将其他类型的值转换为布尔值。以下值在转换为布尔值时为False
:None
、False
、零 (0
、0.0
、0j
)、空序列(如''
、()
、[]
)和空映射(如{}
)。其他所有值转换为布尔值时均为True
。
List(列表)
names = ["Geeks", "for", "Geeks"]
Tuple(元组)
tup = (1, 2, 3, 4, 5)
print("The list of tuples is : " + str(tup))
// The list of tuples is : (1, 2, 3, 4, 5)
Set(集合)
Dictionary(字典)
bytes(类型)
Last updated