数据类型

  • 不可变数据: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 中,TureFalse 表示布尔值

  • boolint的子类,因此布尔值可以被看作整数来使用,其中Ture等价于1

  • 布尔类型可以和其他数据类型进行比较,在比较时,会将 Ture 视为1,False视为0

  • 布尔类型可以和逻辑运算符一起使用,包括andornot。这些运算符可以用来组合多个布尔表达式,生成一个新的布尔值。

  • 布尔类型也可以被转换成其他数据类型,比如整数、浮点数和字符串。在转换时,True 会被转换成 1,False 会被转换成 0。

  • 可以使用 bool() 函数将其他类型的值转换为布尔值。以下值在转换为布尔值时为 FalseNoneFalse、零 (00.00j)、空序列(如 ''()[])和空映射(如 {})。其他所有值转换为布尔值时均为 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