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.
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.
Open the VS Code editor in the LabEx environment.
Create a new file named
negative_numbers.pyin the~/projectdirectory.~/project/negative_numbers.pyAdd the following code to the
negative_numbers.pyfile:## 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.
Save the
negative_numbers.pyfile.Run the script using the
pythoncommand in the terminal:python negative_numbers.pyYou should see the following output:
Temperature: -10 Debt: -100 Change: -5 New Temperature: -5 Remaining Debt: -80 New Change: -10This 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.
Open the VS Code editor in the LabEx environment.
Create a new file named
less_than_zero.pyin the~/projectdirectory.~/project/less_than_zero.pyAdd the following code to the
less_than_zero.pyfile:## 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
ifstatement to check if the value of thenumbervariable 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.Save the
less_than_zero.pyfile.Run the script using the
pythoncommand in the terminal:python less_than_zero.pyYou 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.
Open the VS Code editor in the LabEx environment.
Create a new file named
account_for_zero.pyin the~/projectdirectory.~/project/account_for_zero.pyAdd the following code to the
account_for_zero.pyfile:## 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-elsestatement to check if the value of thenumbervariable is less than zero, greater than zero, or equal to zero. It then prints a message indicating the result.Save the
account_for_zero.pyfile.Run the script using the
pythoncommand in the terminal:python account_for_zero.pyYou 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.



