The following Python snippet demonstrates how to test if an inputted number is positive, negative, or zero. The code is simple, straightforward, and works with any number or integer. The code uses an if...elif...else statement to determine if a number is greater than 0 (in which case it has to be positive), elif a number equals zero (in which case it's neither positive nor negative...it's simply zero), or else it's less than zero (negative).
num = 3 if num > 0: print("Positive number") elif num == 0: print("Zero") else: print("Negative number")
In the example above, the number is equal to three, so the output would be "Positive number" because it's greater than zero. If num was equal to -19, then the output would be "Negative number." The code is very simple and straightforward.
One way you might use this code is to have your users input a number and present them with the output. You could also use the code to execute functions based on whether or not the number in question is positive or negative. To do this, you'll need to tweak the code to insert your functions in place of the print commands.To Practice: Try this interactive course on the basics of Lists, Functions, Packages and NumPy in Python.