How to Check If a Number Is Positive in Python

PythonPythonBeginner
Practice Now

Introduction

In this lab, you will learn how to check if a number is positive in Python. This involves defining positive numbers and writing a simple program to identify them. You'll start by creating a Python file and using an if statement to compare a number with zero, printing a message if it's positive.

You will then expand your understanding by modifying the script to handle different scenarios, including identifying positive, negative, and zero values. The lab will guide you through modifying the Python file and running it to observe the output for various numerical inputs.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("Python")) -.-> python/ControlFlowGroup(["Control Flow"]) python(("Python")) -.-> python/BasicConceptsGroup(["Basic Concepts"]) 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-559553{{"How to Check If a Number Is Positive in Python"}} python/numeric_types -.-> lab-559553{{"How to Check If a Number Is Positive in Python"}} python/conditional_statements -.-> lab-559553{{"How to Check If a Number Is Positive in Python"}} end

Define Positive Numbers

In this step, you will learn how to define positive numbers in Python and write a simple program to identify them. This is a fundamental concept in programming and will help you understand how to work with numerical data.

First, let's create a Python file named positive_numbers.py in your ~/project directory using the VS Code editor.

~/project/positive_numbers.py

Now, open the positive_numbers.py file in the editor and add the following code:

number = 10
if number > 0:
    print(number, "is a positive number")

This code snippet does the following:

  • Assigns the value 10 to a variable named number.
  • Uses an if statement to check if number is greater than 0.
  • If the condition is true (i.e., number is positive), it prints a message indicating that the number is positive.

To run this Python script, open your terminal and navigate to the ~/project directory:

cd ~/project

Then, execute the script using the python command:

python positive_numbers.py

You should see the following output:

10 is a positive number

Now, let's modify the positive_numbers.py file to use a different positive number. Change the value of the number variable to 5:

number = 5
if number > 0:
    print(number, "is a positive number")

Save the file and run the script again:

python positive_numbers.py

You should see the following output:

5 is a positive number

This confirms that your program correctly identifies positive numbers.

Compare with Zero

In this step, you will expand your understanding of numbers by comparing them with zero. You'll learn how to identify positive, negative, and zero values using Python.

Let's modify the positive_numbers.py file you created in the previous step to handle different scenarios. Open the positive_numbers.py file in your ~/project directory using the VS Code editor.

Now, replace the existing code with the following:

number = 0
if number > 0:
    print(number, "is a positive number")
elif number == 0:
    print(number, "is zero")
else:
    print(number, "is a negative number")

This code introduces the elif (else if) and else statements:

  • The if statement checks if number is greater than 0 (positive).
  • The elif statement checks if number is equal to 0.
  • The else statement is executed if neither of the above conditions is true (meaning number is negative).

Save the file and run the script:

cd ~/project
python positive_numbers.py

You should see the following output:

0 is zero

Now, let's change the value of number to a negative number, such as -5:

number = -5
if number > 0:
    print(number, "is a positive number")
elif number == 0:
    print(number, "is zero")
else:
    print(number, "is a negative number")

Save the file and run the script again:

python positive_numbers.py

You should see the following output:

-5 is a negative number

This demonstrates how to compare numbers with zero and handle different cases using if, elif, and else statements.

Handle Both Integers and Floats

In this step, you will learn how to handle both integers and floating-point numbers (decimals) in your Python program. This is important because you'll often encounter different types of numerical data in real-world applications.

Let's modify the positive_numbers.py file again to work with floats. Open the positive_numbers.py file in your ~/project directory using the VS Code editor.

Now, change the value of the number variable to a floating-point number, such as 3.14:

number = 3.14
if number > 0:
    print(number, "is a positive number")
elif number == 0:
    print(number, "is zero")
else:
    print(number, "is a negative number")

Save the file and run the script:

cd ~/project
python positive_numbers.py

You should see the following output:

3.14 is a positive number

Now, let's try a negative floating-point number, such as -2.5:

number = -2.5
if number > 0:
    print(number, "is a positive number")
elif number == 0:
    print(number, "is zero")
else:
    print(number, "is a negative number")

Save the file and run the script again:

python positive_numbers.py

You should see the following output:

-2.5 is a negative number

As you can see, the program works correctly with both integers and floats. Python automatically handles the different data types without requiring any special modifications to the code. This flexibility makes Python a powerful language for numerical computations.

Summary

In this lab, you learned how to define positive numbers in Python and write a simple program to identify them by comparing them with zero. You created a Python file, assigned a value to a variable, and used an if statement to check if the number is greater than zero, printing a message if it is positive.

You also practiced modifying the script with different positive numbers to confirm that the program correctly identifies them. The lab sets the foundation for understanding how to work with numerical data and identify positive, negative, and zero values.