Python Program to Check if a Number Is Positive, Negative or Zero
Beginner · Basics
Every number is either positive (greater than 0), negative (less than 0) or exactly zero. This program checks which one a number is, using an if, elif and else chain.
Program
-7.5 is a negative number
How it works
if num > 0checks for a positive number first.- If that is false,
elif num < 0checks for a negative number. - If neither condition is true, the number can only be zero, so the
elsebranch handles it. - Reading the number with
float()in theinput()version means decimals like -7.5 work as well as whole numbers.
Other ways to write it
Using a nested if
The number is zero
The outer if splits numbers into "0 or more" and "below 0", and the inner if then separates zero from the positive numbers.
Checking a list of numbers
12 is positive -3 is negative 0 is zero 4.5 is positive -0.1 is negative
A function that returns a word can be reused on many numbers inside a loop.
Try it yourself
Extend the program so that for a whole number it also says whether it is even or odd.
Edit the program above and press Run, or open it in the full editor to save it and share it. To watch it run line by line, open it in the visualizer.
For a practical file: draw its flowchart or write its algorithm in steps.
Related programs
All Python programs · CBSE class 12 practical file · Python exercises