fastread homefastrread library fastread menu

Python : Check if a Number is Positive, Negative or Zero

Tutorial by:Rizwan Abdullah      Date: 2016-06-16 00:48:16

❰ Previous Next ❱

Source Code


# In this python program, user enters a number and checked if the number is positive or negative or zero

num = float(input("Enter a number: "))
if num > 0:
   print("Positive number")
elif num == 0:
   print("Zero")
else:
   print("Negative number")

Here, we have used the if...elif...else statement. We can do the same thing using nested if statements as follows.



# This time use nested if to solve the problem

num = float(input("Enter a number: "))
if num >= 0:
   if num == 0:
       print("Zero")
   else:
       print("Positive number")
else:
   print("Negative number")

Output 1


Enter a number: 2
Positive number

 

 
 

Output 2


Enter a number: 0
Zero

A number is positive if it is greater than zero. We check this in the expression of if. If it is False, the number will either be zero or negative. This is also tested in subsequent expression.

❰ Previous Next ❱


Python

Submit Your Thought, Tutorial, Articls etc.

Submit Your Information India's Number one online promotion website