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: Start by declaring a variable and assigning a value to it. This variable will represent the number you want to check.

Step 2: Use an if statement to check if the number is greater than zero. If the condition is true, it means the number is positive. You can use the comparison operator ">" to check if the number is greater than zero.

number = 10

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

Step 3: If the number is not greater than zero, you can use an elif statement to check if the number is less than zero. If the condition is true, it means the number is negative. You can use the comparison operator "<" to check if the number is less than zero.

number = -5

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

Step 4: If the number is neither greater than zero nor less than zero, it means the number is zero. You can use an else statement to handle this case.

number = 0

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

That's it! You now have a complete code to check if a number is positive, negative, or zero in Python. Feel free to use this code as a reference for your own projects.