codescracker


python

Python Program to Check Positive Negative Zero



« Previous Program Next Program »


Check Positive Negative Zero in Python

To check for positive, negative, or zero in python, you have to ask from user to enter a number to check whether that number is a positive number, negative number, or zero as shown in the program given here.

Python Programming Code to Check Positive Negative Zero

Following python program ask from user to enter any number to check for positive number, negative number or zero:

# Python Program - Check Positive Negative and Zero

print("Enter 'x' for exit.");
num = input("Enter any number: ");
if num == 'x':
    exit();
try:
    number = int(num);
except ValueError:
    print("Try to enter a number..exiting..");
else:
    if number < 0:
    	print("It is a negative number.");
    elif number == 0:
    	print("It is a zero.");
    elif number > 0:
    	print("It is a positive number.");
    else:
    	print(number, "is strange.");

Here is the sample run of the above Python program. This is the initial output asking from user to enter any number to check it out for whether the given or supplied number is a positive number, negative number, zero, or anything other than all these options:

check positive number python

Let's first check the above program for positive number by entering any positive number say 10 and press enter key to see the output given here in the below screenshot:

check negative number python

Now let's check for negative number by entering -10, a negative number:

check positive negative zero python

Now again re-run the same program and check it out for zero as shown here:

check zero python

Now let's type anything other than these options (that is, numbers) by typing any special character or any alphabet say $ and press enter key to see the output given here:

check positive negative number python

Here is the same program written and run directly on python shell.

python check positive negative zero

Program Video with Compile and Run


« Previous Program Next Program »








Tools
Calculator

Quick Links
Signup - Login - Give Online Test