Python Positive or Negative Number Checker
Check whether a number is positive, negative, or zero in Python using conditional branching.
How it Works
Determining the sign of a number is one of the most fundamental operations in logical decision-making and mathematical computing.
We use Python's if-elif-else statements to check if the number is greater than zero (positive), less than zero (negative), or equal to zero.
This exercise reinforces fundamental boolean comparisons and conditional branching logic in Python.
Source Code
Checking a series of test inputs to classify them as positive, negative, or zero.
def check_number(num):
if num > 0:
return "Positive"
elif num < 0:
return "Negative"
else:
return "Zero"
test_cases = [10, -5, 0]
for val in test_cases:
print(f"Number {val:2d} is {check_number(val)}")Number 10 is Positive
Number -5 is Negative
Number 0 is ZeroReal-world Applications
- Input validation in data pipelines
- Sign-dependent calculation branching
- Basic control flow learning
Frequently Asked Questions
Is zero considered positive or negative?
In standard mathematics and Python programming, zero is neither positive nor negative. It is classified as neutral or zero.
More Examples
Recommended Python Resources
Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.
Python Loops
Learn how to use Python loops to iterate over data. Master for loops, while loops, break, continue, and loop best practices with interactive examples.
How to Sort a List in Python
Learn how to sort a list in Python using the sort() method and the sorted() function. Discover custom key sorting and reverse order examples.
Python String Methods
A complete reference guide for Python string manipulation. Master formatting, searching, splitting, replacing, and checking string properties.
Python vs JavaScript: Which Programming Language is Best?
A comprehensive comparison between Python and JavaScript. Explore syntax differences, performance, use cases (backend vs frontend), and coding examples.