Skip to content

Python return -1 Minus One | Example code

Just write -1 in the Python function return statement to get Minus One. This statement can be used to indicate an error or an abnormal termination of a function.

def return_negative_one():
    return -1



Python return -1 Minus One

This function may be useful in certain programming scenarios, such as testing and debugging, where you want to explicitly return a negative value to verify that your code can handle it correctly.

If you want to convert a boolean into an int then use the below code to get Python to return 1

def one():
    myBoolean = True
    return int(myBoolean)


Output: 1

Another example, let’s say you have a function that searches for a specific element in a list and returns its index. If the element is not found in the list, you can use “return -1” to indicate that the element was not found.

def search_element(my_list, element):
    for i in range(len(my_list)):
        if my_list[i] == element:
            return i
    return -1

In the above code, if the element is found, the function returns its index. If the element is not found, the function returns -1.

In Python, the expression return -1 is used to exit a function and return the value -1 as the result of the function. It does not specifically refer to the phrase “minus one,” but rather represents a negative integer value.

Note: the use of “return -1” is not a universal convention in Python or any other programming language. It depends on the context and the specific requirements of the function being written.

Comment if you have any doubts or suggestions on this Python return topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

2 thoughts on “Python return -1 Minus One | Example code”

Leave a Reply

Your email address will not be published. Required fields are marked *