Skip to main content

How to check if a number is positive, negative, or zero in Python

How to check if a number is positive, negative, or zero in Python.

Here's a detailed step-by-step tutorial on how to check if a number is positive, negative, or zero in Python:

Step 1: Declare a variable and assign a number to it.

number = 42

Step 2: Use an if statement to check if the number is positive.

if number > 0:
print("The number is positive")

Step 3: Use an elif statement to check if the number is negative.

elif number < 0:
print("The number is negative")

Step 4: Use an else statement to handle the case where the number is zero.

else:
print("The number is zero")

Step 5: Run the code and observe the output.

Here's the complete code:

number = 42

if number > 0:
print("The number is positive")
elif number < 0:
print("The number is negative")
else:
print("The number is zero")

Output:

The number is positive

You can replace the value of the number variable with any other number to test different scenarios.

Additional Examples:

Example 1: Checking a negative number

number = -10

if number > 0:
print("The number is positive")
elif number < 0:
print("The number is negative")
else:
print("The number is zero")

Output:

The number is negative

Example 2: Checking zero

number = 0

if number > 0:
print("The number is positive")
elif number < 0:
print("The number is negative")
else:
print("The number is zero")

Output:

The number is zero

Feel free to modify the code and test it with different numbers to see how it behaves in different scenarios.