How to Check If a Number Is Negative in Python

PythonPythonBeginner
Practice Now

Introduction

In this lab, you will learn how to determine if a number is negative in Python. The lab begins by introducing the concept of negative numbers and demonstrating how to assign and manipulate them within a Python script. You'll create a negative_numbers.py file, assign negative values to variables representing temperature, debt, and change, and then perform basic arithmetic operations to observe how negative numbers behave.

The lab then proceeds to teach you how to use conditional statements to check if a number is less than zero. This involves using the "less than zero" check to determine the sign of a number.


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{{"How to Check If a Number Is Negative in Python"}} python/numeric_types -.-> lab-559551{{"How to Check If a Number Is Negative in Python"}} python/conditional_statements -.-> lab-559551{{"How to Check If a Number Is Negative in Python"}} end

Understand Negative Numbers

In this step, you will learn about negative numbers in Python. Negative numbers are numbers less than zero and are represented with a minus sign (-) in front of them. Understanding negative numbers is crucial for various programming tasks, such as representing temperatures below zero, debts, or changes in quantities.

Let's start by creating a Python script to explore negative numbers.

  1. Open the VS Code editor in the LabEx environment.

  2. Create a new file named negative_numbers.py in the ~/project directory.

    ~/project/negative_numbers.py
  3. Add the following code to the negative_numbers.py file:

    ## 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)

    This script demonstrates how to assign negative values to variables and perform basic arithmetic operations with them.

  4. Save the negative_numbers.py file.

  5. Run the script using the python command in the terminal:

    python negative_numbers.py

    You should see the following output:

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

    This output shows the initial negative values and the results of the calculations.

Use Less Than Zero Check

In this step, you will learn how to use conditional statements to check if a number is less than zero in Python. This is a fundamental concept in programming and is used to make decisions based on the value of a variable.

Let's create a Python script to demonstrate the "less than zero" check.

  1. Open the VS Code editor in the LabEx environment.

  2. Create a new file named less_than_zero.py in the ~/project directory.

    ~/project/less_than_zero.py
  3. Add the following code to the less_than_zero.py file:

    ## 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.")

    This script uses an if statement to check if the value of the number variable is less than zero. If it is, it prints a message indicating that the number is less than zero. Otherwise, it prints a message indicating that the number is not less than zero.

  4. Save the less_than_zero.py file.

  5. Run the script using the python command in the terminal:

    python less_than_zero.py

    You should see the following output:

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

    This output shows that the script correctly identifies whether the number is less than zero or not.

Account for Zero

In this step, you will learn how to account for zero in your Python programs. Zero is a special number that is neither positive nor negative. It's important to handle zero correctly in your code to avoid unexpected behavior.

Let's create a Python script to demonstrate how to account for zero.

  1. Open the VS Code editor in the LabEx environment.

  2. Create a new file named account_for_zero.py in the ~/project directory.

    ~/project/account_for_zero.py
  3. Add the following code to the account_for_zero.py file:

    ## 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.")

    This script uses an if-elif-else statement to check if the value of the number variable is less than zero, greater than zero, or equal to zero. It then prints a message indicating the result.

  4. Save the account_for_zero.py file.

  5. Run the script using the python command in the terminal:

    python account_for_zero.py

    You should see the following output:

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

    This output shows that the script correctly identifies whether the number is less than zero, greater than zero, or equal to zero.

Summary

In this lab, you begin by understanding negative numbers in Python, recognizing that they are numbers less than zero represented with a minus sign. You create a negative_numbers.py script to assign negative values to variables like temperature, debt, and change, and then perform basic arithmetic operations with these negative numbers, printing both the initial values and the results of the calculations.

The lab then proceeds to introduce the concept of using conditional statements to check if a number is less than zero.