Python Check if a Number is Positive, Negative or Zero

In this snippet, we will learn how to check positive number, negative number and zero in Python.

Using if...elif...else

Let's have a look at the example using if…elif…else:

num = float(input("Enter a number: "))
if num > 0:
   print("Positive number.")
elif num == 0:
   print("The number is zero!")
else:
   print("Negative number.")

Using Nested if

Using nested if:

num = float(input("Enter a number: "))
if num >= 0:
    if num == 0:
        print("The number is zero!")
    else:
        print("Positive number.")
else:
    print("Negative number.")

Software Engineer | Ethical Hacker & Cybersecurity...

Md Obydullah is a software engineer and full stack developer specialist at Laravel, Django, Vue.js, Node.js, Android, Linux Server, and Ethichal Hacking.