如何在 Python 中检查一个数是否为负数

PythonPythonBeginner
立即练习

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

简介

在这个实验中,你将学习如何在 Python 中判断一个数是否为负数。实验首先会介绍负数的概念,并演示如何在 Python 脚本中赋值和操作负数。你将创建一个 negative_numbers.py 文件,为代表温度、债务和变化的变量赋予负值,然后执行基本的算术运算,以观察负数的特性。

接着,实验会教你如何使用条件语句来检查一个数是否小于零。这包括使用“小于零”的检查来确定一个数的正负性。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("Python")) -.-> python/BasicConceptsGroup(["Basic Concepts"]) python(("Python")) -.-> python/ControlFlowGroup(["Control Flow"]) python/BasicConceptsGroup -.-> python/variables_data_types("Variables and Data Types") python/BasicConceptsGroup -.-> python/numeric_types("Numeric Types") python/ControlFlowGroup -.-> python/conditional_statements("Conditional Statements") subgraph Lab Skills python/variables_data_types -.-> lab-559551{{"如何在 Python 中检查一个数是否为负数"}} python/numeric_types -.-> lab-559551{{"如何在 Python 中检查一个数是否为负数"}} python/conditional_statements -.-> lab-559551{{"如何在 Python 中检查一个数是否为负数"}} end

理解负数

在这一步中,你将学习 Python 中的负数。负数是小于零的数,在其前面用减号 (-) 表示。理解负数对于各种编程任务至关重要,例如表示零下温度、债务或数量的变化。

让我们从创建一个 Python 脚本来探索负数开始。

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

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

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

    ## Assigning negative values to variables
    temperature = -10
    debt = -100
    change = -5
    
    ## Printing the values
    print("Temperature:", temperature)
    print("Debt:", debt)
    print("Change:", change)
    
    ## Performing calculations with negative numbers
    new_temperature = temperature + 5
    remaining_debt = debt + 20
    new_change = change * 2
    
    print("New Temperature:", new_temperature)
    print("Remaining Debt:", remaining_debt)
    print("New Change:", new_change)

    此脚本演示了如何为变量分配负值并对其执行基本的算术运算。

  4. 保存 negative_numbers.py 文件。

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

    python negative_numbers.py

    你应该会看到以下输出:

    Temperature: -10
    Debt: -100
    Change: -5
    New Temperature: -5
    Remaining Debt: -80
    New Change: -10

    此输出显示了初始的负值以及计算结果。

使用小于零检查

在这一步中,你将学习如何在 Python 中使用条件语句来检查一个数是否小于零。这是编程中的一个基本概念,用于根据变量的值做出决策。

让我们创建一个 Python 脚本来演示“小于零”的检查。

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

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

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

    ## Assign a value to a variable
    number = -5
    
    ## Check if the number is less than zero
    if number < 0:
        print("The number is less than zero.")
    else:
        print("The number is not less than zero.")
    
    ## Change the value of the variable
    number = 10
    
    ## Check again if the number is less than zero
    if number < 0:
        print("The number is less than zero.")
    else:
        print("The number is not less than zero.")

    此脚本使用 if 语句来检查 number 变量的值是否小于零。如果是,则打印一条消息表明该数小于零;否则,打印一条消息表明该数不小于零。

  4. 保存 less_than_zero.py 文件。

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

    python less_than_zero.py

    你应该会看到以下输出:

    The number is less than zero.
    The number is not less than zero.

    此输出表明脚本能够正确识别该数是否小于零。

处理零值

在这一步中,你将学习如何在 Python 程序中处理零值。零是一个特殊的数字,它既不是正数也不是负数。在代码中正确处理零值很重要,以避免出现意外的行为。

让我们创建一个 Python 脚本来演示如何处理零值。

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

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

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

    ## Assign a value to a variable
    number = 0
    
    ## Check if the number is less than zero
    if number < 0:
        print("The number is less than zero.")
    elif number > 0:
        print("The number is greater than zero.")
    else:
        print("The number is equal to zero.")
    
    ## Change the value of the variable
    number = -3
    
    ## Check again if the number is less than zero
    if number < 0:
        print("The number is less than zero.")
    elif number > 0:
        print("The number is greater than zero.")
    else:
        print("The number is equal to zero.")
    
    ## Change the value of the variable
    number = 5
    
    ## Check again if the number is less than zero
    if number < 0:
        print("The number is less than zero.")
    elif number > 0:
        print("The number is greater than zero.")
    else:
        print("The number is equal to zero.")

    此脚本使用 if-elif-else 语句来检查 number 变量的值是小于零、大于零还是等于零,然后打印一条消息来表明检查结果。

  4. 保存 account_for_zero.py 文件。

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

    python account_for_zero.py

    你应该会看到以下输出:

    The number is equal to zero.
    The number is less than zero.
    The number is greater than zero.

    此输出表明脚本能够正确识别该数是小于零、大于零还是等于零。

总结

在本次实验中,你首先了解了 Python 中的负数,认识到负数是小于零且用负号表示的数字。你创建了一个 negative_numbers.py 脚本,将负值赋给诸如温度、债务和变化量等变量,然后对这些负数进行基本的算术运算,并打印出初始值和计算结果。

接着,本次实验引入了使用条件语句检查一个数是否小于零的概念。