Python
Java前端运维数据库
Python
Python
  • 安装
  • 基础语法
  • 数据类型
    • 列表
    • 元组
    • 集合
    • 字典
  • 数据类型转换
  • 函数
  • 条件判断和循环
  • 模块
  • 多线程
  • 错误和异常
  • 装饰器
  • pip & pipx
Powered by GitBook
On this page
  • Number
  • String
  • bool (布尔类型)
  • List(列表)
  • Tuple(元组)
  • Set(集合)
  • Dictionary(字典)
  • bytes(类型)

数据类型

  • 不可变数据: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(类型)

Previous基础语法Next列表

Last updated 1 month ago