Source Code: Using if...elif...else
num = float(input("Enter a number: ")) if num > 0: print("Positive number") elif num == 0: print("Zero") else: print("Negative number")Here, we have used the if...elif...else statement. We can do the same thing using nested if statements as follows.
Source Code: Using Nested if
num = float(input("Enter a number: ")) if num >= 0: if num == 0: print("Zero") else: print("Positive number") else: print("Negative number")The output of both programs will be the same.
Output 1
Enter a number: 2 Positive numberOutput 2
Enter a number: 0 ZeroA number is positive if it is greater than zero. We check this in the expression of if. If it is False, the number will either be zero or negative. This is also tested in subsequent expression.
I am trying to add a piece of code to this string to check for a negative number in the first position. If the code identifies a negative number it should return "False". I'm having trouble with int and str at the user input command. Sorry, new to Python.
def is_number(S): #if s < 0: #print("False") #else: try: float(s) return True except ValueError: pass try: import unicodedata unicodedata.numeric(s) return True except (TypeError, ValueError): pass return False s = input() is_number(s)asked Feb 8, 2018 at 3:29
1
Is this what you want?
def check_negative(s): try: f = float(s) if (f < 0): return True # Otherwise return false return False except ValueError: return FalseNot entirely sure if this is what you want though, maybe you should see ᴡʜᴀᴄᴋᴀᴍᴀᴅᴏᴏᴅʟᴇ3000's answer
answered Feb 8, 2018 at 3:37
heoheo
11611 bronze badges
2
is_number=lambda S: str(S)[0]=='-' and len(str(S))>1
Try this code. You can replace your is_number function with this code. It is a function which checks whether the first character is a negative sign, and if it is, it will return true. Otherwise, the number cannot be negative, and it will then return false. However, if there is only a - sign, then it is neither and will return false. It only uses one line and is much simpler. It works for strings and floats too.
answered Feb 8, 2018 at 3:38
1
Python Program to check if a Number is Positive, Negative or Zero
Here, we will learn to write a Python program through which we can check if the given number is positive, zero, or negative.
Positive Numbers: A number is said to be positive if the number has a value greater than 0, like 1, 2, 3, 5, 7, 9, 11, 13, etc. All the natural numbers are positive numbers.
Negative Numbers: If a given number has a value less than 0 like -1, -2, -3, -5, -7, -9, -11, -13, etc., then we can say that the given number is a negative number. Only rational and integer type numbers can have negative values or numbers.
Let us look at the following example to understand the implementation
Example:
Output:
Enter a number as input value: -6 Number given by you is Negative
Explanation:
We have used a nested if else condition in the program to check the number. When the user gives an input number, the program will first check if the value of the number is greater than 0 (if yes, it will print positive and the program ends), otherwise it will check if the value is less than 0 and it last it will print that number is 0.