Examples
- Hello World Program in Python (How to Print Hello World in Python?)
- Python Program to Add Two Numbers (Sum/Addition Program)
- How to Generate Random Number in Python? (Program)
- Leap Year Program in Python (Check Leap Year or Not in Python)
- Python Program to Check Number is Positive, Negative, or Zero
- Even Odd Program in Python (How to Check Number is Even or Odd?)
- Python Program to Find Largest of 3 Numbers (Greatest of Three Numbers in Python)
- Prime Number Program in Python (Check Prime or Not)
- Python Program to Find Square Root of a Number (Examples)
- Python Factorial Program (How to Find Factorial of a Number in Python?)
- Python Program to Find Prime Numbers in Range
- Python Program to Find Area of Triangle (Different Ways With Examples)
- How to Solve Quadratic Equation in Python? (Program Examples)
- How to Swap Two Numbers in Python? (Program Examples to Swap Variables)
- Python Program to Convert Kilometers to Miles (km to mi) and Vice Versa
- Python Program to Convert Celsius To Fahrenheit (and Fahrenheit to Celsius)
- Program to Check Armstrong Numbers in Python (Code Examples)
- Program for Armstrong Number Between Two Intervals in Python
- Program to Print Multiplication Table in Python (Code Examples)
- Python Program for Fibonacci Series (Print Fibonacci Sequence in Python)
- Program for Sum of n Natural Numbers in Python (Code Examples)
- Program to Print Powers of 2 in Python (Code Examples)
- Python Program to Find Numbers Divisible by Another Number
- Convert Decimal to Binary, Octal and Hexadecimal in Python (Program with Example)
- How to Convert Decimal to Binary in Python? Program with Examples
- How to Convert Decimal to Octal in Python? Program with Examples
- How to Convert Decimal to Hexadecimal in Python? Program and Examples
- How to Convert Binary to Decimal in Python? Program With Examples
- How to Convert Hexadecimal to Octal in Python? Program With Examples
- Program to Find LCM of Two Numbers in Python (Calculate LCM in Python)
- GCD of Two Numbers in Python (Program to Find HCF or GCD)
- GCD of Three Numbers in Python (HCF Program for n Numbers)
- Simple Calculator Program in Python (Basic Calculator Code)
- Find Factors of a Number in Python (Programs and Examples)
- Program to Find Prime Factors of a Number in Python
- How to Find ASCII Value in Python? ASCII Value of Character
Python Program to Check Number is Positive, Negative, or Zero
Are you curious about determining whether a number is positive, negative, or zero in Python? Being able to discern the sign of a number is a fundamental skill in programming. Whether you're working on mathematical calculations, data analysis, or simply need to classify numbers based on their sign, having a Python program to check if a number is positive or negative can be incredibly useful.
In this post, we will explore Python code that allows you to effortlessly identify whether a given number is positive, negative, or zero. By leveraging the power of conditional statements and comparison operators, we can create a program that provides quick and accurate results.
Check Negative and Positive Number in Python Using if...elif...else
Code
# Check given number is positive, negative or 0
number = float(input("Enter a number: "))
if number > 0:
print("The number is positive")
elif number < 0:
print("The number is negative")
else:
print("The number is zero")
Output
Enter a number: -4
The number is negative
Explanation
In this program, we prompt the user to enter a number using the input() function. The input is converted to a floating-point number using the float() function and stored in the variable number.
We then use if...elif...else statements to check the value of the number and print the corresponding message based on its sign.
-
If the number is greater than 0, we print "The number is positive".
-
If the number is less than 0, we print "The number is negative".
-
If the number is neither greater than 0 nor less than 0, it must be 0, so we print "The number is zero".
When you run this program, it will prompt you to enter a number. After you provide the input, it will check the value of the number and display the appropriate message indicating whether the number is negative, positive, or zero.
Find If Number is Positive, Negative or Zero Using Nested if
Code
# Using nested if to check number is positive, negative or zero
number = float(input("Enter a number: "))
if number < 0:
print("The number is negative")
else:
if number > 0:
print("The number is positive")
else:
print("The number is zero")
Output
Enter a number: 2
The number is positive
Explanation
In the above program, we ask the user to enter a number using the input() function. The input is converted to a floating-point number using the float() function and stored in the variable number.
We use nested if statements to check the value of the number.
-
If the number is less than 0, we print "The number is negative".
-
If the number is not less than 0, we move to the inner if statement.
-
If the number is greater than 0, we print "The number is positive".
If the number is neither less than 0 nor greater than 0, it must be 0, so we print "The number is zero".
Practice More Python Programs:
- Hello World Program in Python
- Python Program to Add Two Numbers
- Generate Random Number in Python
- Leap Year Program in Python
- Even Odd Program in Python
- Python Program to Find Largest of 3 Numbers
- Prime Number Program in Python
- Python Program to Find Square Root of a Number
- Python Factorial Program
- Python Program to Find Area of Triangle
- Solve Quadratic Equation in Python
- Swap Two Numbers in Python
- Python Program to Convert Kilometers to Miles
- Python Program to Convert Celsius To Fahrenheit
- Check Armstrong Numbers in Python
- Print Multiplication Table in Python
- Python Program for Fibonacci Series
- Convert Decimal to Binary, Octal and Hexadecimal in Python
- Find LCM of Two Numbers in Python
- GCD of Two Numbers in Python
- Simple Calculator Program in Python
- Python Matrix Addition Program
- Transpose of Matrix in Python
- Python Matrix Multiplication
- Python Calendar Program