如何在 Python 中检查变量是否为布尔值

PythonPythonBeginner
立即练习

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

简介

在这个实验中,你将学习如何在 Python 中检查一个变量是否为布尔值。你将从理解布尔值开始,布尔值表示真或假,是编程中决策的基础。你将把 TrueFalse 赋值给变量,并观察输出结果。

然后,本实验将指导你使用 type() 函数和 isinstance() 函数来确认一个变量是否为布尔值。你还将探索布尔值是如何从比较运算中产生的,这对于编写条件语句和控制程序流程至关重要。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("Python")) -.-> python/BasicConceptsGroup(["Basic Concepts"]) python(("Python")) -.-> python/ControlFlowGroup(["Control Flow"]) python(("Python")) -.-> python/FunctionsGroup(["Functions"]) python/BasicConceptsGroup -.-> python/booleans("Booleans") python/BasicConceptsGroup -.-> python/type_conversion("Type Conversion") python/ControlFlowGroup -.-> python/conditional_statements("Conditional Statements") python/FunctionsGroup -.-> python/build_in_functions("Build-in Functions") subgraph Lab Skills python/booleans -.-> lab-559598{{"如何在 Python 中检查变量是否为布尔值"}} python/type_conversion -.-> lab-559598{{"如何在 Python 中检查变量是否为布尔值"}} python/conditional_statements -.-> lab-559598{{"如何在 Python 中检查变量是否为布尔值"}} python/build_in_functions -.-> lab-559598{{"如何在 Python 中检查变量是否为布尔值"}} end

理解布尔值

在这一步中,你将学习 Python 中的布尔值。布尔值表示真或假,是编程中决策的基础。Python 有两个内置的布尔值:TrueFalse。请注意,TrueFalse 是 Python 中的关键字,必须大写。

让我们从将布尔值赋给变量并打印它们开始:

  1. 在 LabEx 环境中打开 VS Code 编辑器。

  2. ~/project 目录下创建一个名为 boolean_example.py 的新文件。

    ~/project/boolean_example.py
  3. boolean_example.py 文件中添加以下代码:

    ## Assign True to a variable
    is_active = True
    
    ## Assign False to a variable
    is_admin = False
    
    ## Print the values
    print("Is active:", is_active)
    print("Is admin:", is_admin)
  4. 保存文件。

  5. 在终端中使用 python 命令运行脚本:

    python ~/project/boolean_example.py

    你应该会看到以下输出:

    Is active: True
    Is admin: False

布尔值通常是比较运算的结果。让我们看一些例子:

  1. 修改 boolean_example.py 文件,加入比较运算:

    ## Comparison operations
    x = 10
    y = 5
    
    is_greater = x > y  ## True because 10 is greater than 5
    is_equal = x == y    ## False because 10 is not equal to 5
    
    print("Is x greater than y:", is_greater)
    print("Is x equal to y:", is_equal)
  2. 保存文件。

  3. 再次运行脚本:

    python ~/project/boolean_example.py

    你应该会看到以下输出:

    Is active: True
    Is admin: False
    Is x greater than y: True
    Is x equal to y: False

理解布尔值以及它们如何从比较运算中产生,对于编写条件语句和控制程序流程至关重要。

使用 type() 检查布尔值

在这一步中,你将学习如何使用 Python 中的 type() 函数来确定变量的数据类型,特别是检查一个变量是否为布尔值。type() 函数是一个内置函数,用于返回对象的类型。

基于上一个示例,让我们来检查我们定义的变量的类型:

  1. 使用 VS Code 编辑器打开 ~/project 目录下的 boolean_example.py 文件。

  2. 修改 boolean_example.py 文件,加入 type() 函数:

    ## Assign True to a variable
    is_active = True
    
    ## Assign False to a variable
    is_admin = False
    
    ## Print the values
    print("Is active:", is_active)
    print("Is admin:", is_admin)
    
    ## Comparison operations
    x = 10
    y = 5
    
    is_greater = x > y  ## True because 10 is greater than 5
    is_equal = x == y    ## False because 10 is not equal to 5
    
    print("Is x greater than y:", is_greater)
    print("Is x equal to y:", is_equal)
    
    ## Check the types of the variables
    print("Type of is_active:", type(is_active))
    print("Type of is_greater:", type(is_greater))
    print("Type of x:", type(x))
  3. 保存文件。

  4. 在终端中使用 python 命令运行脚本:

    python ~/project/boolean_example.py

    你应该会看到以下输出:

    Is active: True
    Is admin: False
    Is x greater than y: True
    Is x equal to y: False
    Type of is_active: <class 'bool'>
    Type of is_greater: <class 'bool'>
    Type of x: <class 'int'>

如你所见,type() 函数对于布尔变量返回 <class 'bool'>,对于整数变量返回 <class 'int'>。这使你能够以编程方式检查变量的类型,并根据其类型做出决策。

使用 isinstance() 进行确认

在这一步中,你将学习如何使用 Python 中的 isinstance() 函数来检查一个对象是否是某个特定类的实例。这是确认变量是否为布尔值的另一种方法。isinstance() 函数接受两个参数:要检查的对象和要对照的类。如果对象是该类的实例,则返回 True,否则返回 False

让我们使用 isinstance() 来检查我们的变量是否为布尔值:

  1. 使用 VS Code 编辑器打开 ~/project 目录下的 boolean_example.py 文件。

  2. 修改 boolean_example.py 文件,加入 isinstance() 函数:

    ## Assign True to a variable
    is_active = True
    
    ## Assign False to a variable
    is_admin = False
    
    ## Print the values
    print("Is active:", is_active)
    print("Is admin:", is_admin)
    
    ## Comparison operations
    x = 10
    y = 5
    
    is_greater = x > y  ## True because 10 is greater than 5
    is_equal = x == y    ## False because 10 is not equal to 5
    
    print("Is x greater than y:", is_greater)
    print("Is x equal to y:", is_equal)
    
    ## Check the types of the variables
    print("Type of is_active:", type(is_active))
    print("Type of is_greater:", type(is_greater))
    print("Type of x:", type(x))
    
    ## Check if the variables are instances of the bool class
    print("is_active is an instance of bool:", isinstance(is_active, bool))
    print("x is an instance of bool:", isinstance(x, bool))
  3. 保存文件。

  4. 在终端中使用 python 命令运行脚本:

    python ~/project/boolean_example.py

    你应该会看到以下输出:

    Is active: True
    Is admin: False
    Is x greater than y: True
    Is x equal to y: False
    Type of is_active: <class 'bool'>
    Type of is_greater: <class 'bool'>
    Type of x: <class 'int'>
    is_active is an instance of bool: True
    x is an instance of bool: False

如你所见,isinstance(is_active, bool) 返回 True,因为 is_active 是一个布尔值,而 isinstance(x, bool) 返回 False,因为 x 是一个整数。isinstance() 函数对于检查变量是否属于特定类很有用,它提供了一种更可靠的方式来验证数据类型。

总结

在本次实验中,你学习了 Python 中的布尔值。布尔值用于表示真或假,是决策过程中不可或缺的元素。你将 TrueFalse 赋值给变量,并打印出它们的值。

此外,你还探究了布尔值是如何通过比较运算得出的,例如检查一个数字是否大于或等于另一个数字。理解布尔值及其在比较运算中的来源,对于编写条件语句和控制程序流程至关重要。