Given a number as an input to the Python program, the python script should check whether a given integer number is positive, negative or zero. Let's use a simple statement to compare the number to check if it is positive, negative or zero.
This is a simple example for using nested if else statements.
Write A Python Script To Check Whether A Given Number Is Positive Negative Or Zero
Let's take a variable named as number and ask the user to enter a number using input function call and type convert it into an integer.
number = int(input("Enter a Number : "))
Use the nested if statement to check if the number is greater than zero, then print the number is positive using the print function call.
Use the else if block to check if the number is less than zero, then print the number is negative using the print function call.
Finally use the else block to print the number is zero.
if number > 0:
print(f"Number : {number} is Positive Number")
elif number < 0:
print(f"Number : {number} is an Negative Number")
else:
print(f"Number : {number} is a Zero")
Complete Python program
====================================
number = int(input("Enter a Number : "))
if number > 0:
print(f"Number : {number} is Positive Number")
elif number < 0:
print(f"Number : {number} is an Negative Number")
else:
print(f"Number : {number} is a Zero")
Output
==============
Enter a Number : 34
Number : 34 is Positive Number
Output
==============
Enter a Number : -52
Number : -52 is an Negative Number
Output
============
Enter a Number : 0
Number : 0 is a Zero
Conclusion
====================
Execute the python script by passing the input number as an integer, Note down the results. You can also modify the type conversion to floating point number and use the program to check for floating point values.
For converting the program into a floating point program just modify the first line
number = int(input("Enter a Number : "))
To
number = float(input("Enter a Number : "))
Comment it down below if you have any queries related to above Python program
0 Comments