Start learning free

LESSON 77 OF 410 · CONDITIONAL STATEMENTS

Return Negative in Python: Make a Number Negative

Practice Read the full explanation

Create a function that takes a number as an argument and returns negative of that number. Return negative numbers without any change.

Examples:

4 ➞ -4

15 ➞ -15

-4 ➞ -4

0 ➞ 0

Hints:

You can make use of < operator with an if statement to check if n is negative.

You can add an else statement to handle the case when n is not negative.
STEP 1 OF 3
    Ctrl + Enter to check

    PYTHON · CONDITIONAL STATEMENTS

    Return Negative in Python, explained

    Problem overview

    The method negative receives a number n and returns its negative. If n is already negative, it comes back unchanged. The examples are negative(4) = -4, negative(15) = -15, negative(-4) = -4 and negative(0) = 0.

    This is the usual answer to how to make a number negative in Python: put a minus sign in front of it. The twist in this lesson is that a negative input must not be flipped to positive, which is what the minus sign would do on its own.

    How to make a number negative in Python

    The unary minus operator - changes the sign of a number. It works on integers and floats and also on variables:

    x = 5
    x = -x
    print(x)       # -5
    print(-(-5))   # 5
    print(-2.5)    # -2.5
    

    The second line shows the problem. Applying - to a number that is already negative makes it positive. So -n works for positive inputs but would turn -4 into 4, and the lesson wants -4.

    Approach: check the sign first

    There are two cases, and an if / else statement separates them:

    • If n < 0, the number is already negative, so return it as it is.
    • Otherwise n is zero or positive, and -n is the negative value. For zero, -0 is 0, which matches the last example.

    Step by step

    1. Write the condition. if n < 0: asks whether the input is already negative.
    2. Return n unchanged inside the if.
    3. Add an else branch that returns -n. It handles zero and all positive numbers.

    The solution in Python

    class Solution:
        def negative(self, n: int) -> int:
            if n < 0:
                return n
            else:
                return -n
    

    Python details to notice:

    • Indentation marks the branches. The two return lines are indented under if and else, and a missing indent is a syntax error.
    • The else is optional. After return n inside the if, the function has already ended, so the second return could stand alone without else. Both forms behave the same.
    • The annotations do not enforce anything. n: int and -> int are hints for readers and tools, and Python does not check them at run time. A float works too: negative(2.5) returns -2.5.

    Other ways to write it

    The built-in abs() returns the absolute value, the number without its sign. Negating that always gives a non-positive result:

    print(-abs(4))    # -4
    print(-abs(-4))   # -4
    print(-abs(0))    # 0
    print(min(4, -4)) # -4
    

    So return -abs(n) solves the whole problem in one line, and so does return min(n, -n). The lesson uses if and else because it practises conditional statements, and the branches make the two cases visible.

    Complexity

    Time: O(1) and space: O(1). The method does one comparison and at most one negation.

    Edge cases and common mistakes

    • Zero. negative(0) returns 0. Putting zero in the else branch is fine, because -0 equals 0 for integers.
    • Floating-point zero. For a float, -0.0 is a separate value that compares equal to 0.0 and prints as -0.0. negative(0.0) returns -0.0.
    • Always returning -n. That turns -4 into 4.
    • Returning n for positive numbers. Mixing up the branches leaves positive inputs unchanged.
    • Using <=. n <= 0 also works, since zero returns zero either way, but the lesson's n < 0 is the cleaner statement of "already negative".
    • Negating text. A string such as "5" has no unary minus, and -"5" raises TypeError. Convert it first with -int("5").
    • Operator precedence with powers. -2 ** 2 is -4, because ** binds tighter than the unary minus, while (-2) ** 2 is 4.

    Testing

    • negative(4) returns -4.
    • negative(15) returns -15.
    • negative(-4) returns -4.
    • negative(0) returns 0.
    Keep going with PythonAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free