Check Natural Number in Python
What you’ll learn
- Natural number meaning in this page: integer greater than zero.
- A reusable helper function for yes/no checking.
- A range-print example and a live checker.
Overview
Under this tutorial rule, a number is natural if it is an integer and strictly positive.
Single check
One comparison: num > 0.
Range print
Second example prints 1 to 10.
Live preview
Type any integer and test instantly.
Prerequisites
Basic if conditions, loops, and integer values.
- Understand comparisons like
>and<=. - Know that integers do not include decimals.
The idea
Check only one condition: num > 0. If true, call it natural (under this page definition).
Zero and negative numbers are not natural here.
Live preview
Type an integer and check using the same rule as the code: n > 0.
Algorithm
Goal: decide if integer num is natural using rule num > 0.
Take integer input
Use a fixed value or user input converted to int.
Check positivity
If num > 0, return true; else false.
Print message
Show whether it is natural under this definition.
📜 Pseudocode
function is_natural(num):
if num > 0:
return true
return false Yes / no for one integer
Checks one integer and prints natural or not.
def is_natural(num: int) -> bool:
return num > 0
def main() -> None:
number = 42
if is_natural(number):
print(f"{number} is a natural number.")
else:
print(f"{number} is not a natural number.")
if __name__ == "__main__":
main() Print from 1 to 10 in order
Shows a basic range of natural numbers.
def print_integers_in_range(start: int, end: int) -> None:
print(f"Natural numbers in the range {start} to {end}:")
for i in range(start, end + 1):
print(i, end=" ")
print()
def main() -> None:
start = 1
end = 10
print_integers_in_range(start, end)
if __name__ == "__main__":
main() Notes
Definition consistency. Keep your code rule and explanation aligned (especially for zero).
Input safety. Validate user input before converting to integer.
❓ FAQ
🔄 Input / output
Examples use fixed values for clarity. You can replace with user input using int(input(...)).
Edge cases
n = 0
Not natural under this page rule (n > 0).
Non-integer text
Validate and show a clear error message.
⏱️ Time and space complexity
| Operation | Time | Extra space |
|---|---|---|
| Single natural check | O(1) | O(1) |
| Print range start..end | O(end - start + 1) | O(1) |
Summary
- Definition used: integer
nwheren > 0. - Patterns: one-value check and range listing.
- Reminder: keep your code aligned with your syllabus definition for zero.
In many school texts, natural numbers start at 1 ({1, 2, 3, ...}). Some definitions include 0. This page uses the rule n > 0.
8 people found this page helpful
