In this post, we will learn how to check whether a number is positive, negative or zero using Python Programming language.
This program asks the user to enter a number, then it will check whether the entered number is positive, negative or zero using the following approaches:
- Using If. . .Else Statement
- Using Nested List
So, without further ado, let’s begin this tutorial.
Python Program to Check If a Number is Positive, Negative or Zero
# Python Program to Check If a Number is Positive, Negative or Zero num = int(input("Enter a number: ")) # Checking the number if num > 0: print("The entered number is positive.") elif num == 0: print("The entered number is zero.") else: print("The entered number is negative.")
Output 1
Enter a number: 5
The entered number is positive.
Output 2
Enter a number: -7
The entered number is negative.
Output 3
Enter a number: 0
The entered number is zero.
How Does This Program Work ?
num = int(input("Enter a number: "))
The user is asked to enter a number. The entered number gets stored in the num named variable.
if num > 0: print("The entered number is positive.") elif num == 0: print("The entered number is zero.") else: print("The entered number is negative")
Then, we check whether the entered number is greater or less than 0. If the entered number is greater than 0, then the entered number is a positive number.
If the entered number equals to 0, then the entered number is zero.
Similarly, if the entered number is less than 0, then the entered number is a negative number.
Python Program to Check If a Number is Positive, Negative or Zero Using Nested List
# Python Program to Check If a Number is Positive, Negative or Zero Using Nested If # Asking for input num = int(input("Enter a number: ")) # Checking the number if num >= 0: if num == 0: print("The entered number is zero.") else: print("The entered number is positive.") else: print("The entered number is negative.")
Output 1
Enter a number: -18
The entered number is negative.
Output 2
Enter a number: 27
The entered number is positive.
Output 3
Enter a number: 0
The entered number is zero.
Conclusion
I hope after going through this post, you understand how to check if a number is positive, negative or zero using Python Programming language.
If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to assist you.
Also Read: