In this article, we will learn about python program to check if number is positive or negative. We can check it using many ways which we will see it in this article.
Getting Started
The task is to find if number is positive or negative using python program. For example,
If a = 10, output should be “The number is positive.”
If a = -3, output should be “The number is negative.”
If a = 0, output should be “The number is 0.”
We can do it in below ways –
Using if-else Condition
We can simply use if-else condition check to if number is positive or negative in python.
def check_number_sign(number): if number > 0: print(f"The number {number} is positive.") elif number < 0: print("The number is negative.") else: print("The number is zero.") # Get user input user_input = float(input("Enter a number: ")) check_number_sign(user_input)
Here,
- check_number_sign method checks if number is positive or negative or zero. A statement is printed using print() method.
- number > 0 is true if number is positive. If yes, “The number {number} is positive.” is printed.
- number < 0 is true if number is negative. If yes, “The number is negative.” is printed.
- Otherwise “The number is zero.” is printed.
Output:
Enter a number: 0.12 The number 0.12 is positive.
Using Ternary Operator
Above program can be simplified using ternary operator in python. Although it’s more like concise way of if-else condition shown in above program.
def check_pos_neg(number): result = "positive" if number > 0 else "negative" if number < 0 else "zero" print(f"The number is {result}.") number = float(input("Enter a number: ")) check_pos_neg(number)
Output:
Enter a number: 0.0 The number is zero.
Using Lambda Function
We can also write a lambda function to check if number is positive or not as shown below –
check_number_sign = lambda number: print("The number is positive.") if number > 0 else print("The number is negative.") if number < 0 else print("The number is zero.") user_input = float(input("Enter a number: ")) check_number_sign(user_input)
Here,
- check_number_sign is a lambda function which prints “The number is positive.” or “The number is negative.” depending upon whether number is positive or negative respectively.
Output:
Enter a number: -9.12 The number is negative.
Using copysign() Method
We can also use copysign() method from math module in python to check if number is positive or negative in python.
import math def check_pos_neg(number): sign = math.copysign(1, number) if sign > 0: print("The number is positive.") elif sign < 0: print("The number is negative.") else: print("The number is zero.") user_input = float(input("Enter a number: ")) check_pos_neg(user_input)
Here,
- math.copysign() method return value of first parameter and sign of second parameter.
- In above case, it will return 1 with either positive or negative sign depending upon number is positive or negative.
- After that, we compare the value and print result using print() method.
Output:
Enter a number: 2.0 The number is positive.
In this post, we saw how can we write python program to check if number is positive or negative using if-else condition, using ternary operator, using copysign() method from math module.
Learn about python at official Doc