Check if Number is Positive, Negative, or Zero
Decide whether a value is positive, negative, or zero. A beginner Python exercise on float input, ordered comparisons, and choosing one if-elif-else branch.
1# Take a number as input2num = float(input("Enter a number: "))34# Check if positive, negative, or zero5if num > 0:6 print("Positive")7elif num < 0:8 print("Negative")9else:10 print("Zero")
Output
Enter a number: 5 Positive Enter a number: -3 Negative Enter a number: 0 Zero
In this one
- Convert text with float
- Compare signs with zero
- Follow exclusive branches
How it works
- 1
Read num as a float.
- 2
Test positive, then negative only if needed.
- 3
Print the matched sign or the zero fallback.
Walk through Check if Number is Positive, Negative, or Zero
Input 5
- 1.float converts the response to 5.0.
- 2.5.0 > 0 is true, so Positive prints.
- 3.The elif and else branches are skipped.
Edge cases
What happens at zero?
What happens at zero?
Neither strict comparison succeeds, so Zero is printed.
Can I enter -0.5?
Can I enter -0.5?
Yes. float accepts it, and the negative branch runs.
What's going on
A sign check divides the number line into three parts. float converts the response from text to a number, so decimal values can be compared with zero.
Python tries num > 0 first, then num < 0 only if needed. The final else covers zero; it does not mean negative. With 5, the first branch prints Positive and the remaining tests are skipped.
What happens at zero? Neither strict comparison succeeds, so Zero is printed.
Can I enter -0.5? Yes. float accepts it, and the negative branch runs.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
How many classification messages appear per run?
Pick an answer to check it.
Question 2
What happens at zero?
Pick an answer to check it.
Questions people ask
What happens at zero?
Neither strict comparison succeeds, so Zero is printed.
Can I enter -0.5?
Yes. float accepts it, and the negative branch runs.
How many classification messages appear per run?
One.
๐Related Content
- ๐
Phase 1 - Learn Concepts
Review Phase 1 concepts and explanations
- ๐
Phase 1 - Quiz
Test your Phase 1 understanding with quiz questions
- ๐ป
Phase 1 - All Practice Problems
Explore all practice problems for Phase 1
- ๐
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- ๐ง
Logic Building Overview
Learn about the complete logic building curriculum