How to Return None in Python?

Estimated read time 1 min read

In Python, the None keyword is used to represent the absence of a value or to indicate that a function does not return a specific value. To return None in Python, you can simply use the return statement without specifying any value. Here’s an example:

def do_something():
    # Perform some operations
    if condition:
        return None
    else:
        return some_value

In the above example, if the condition is met, the function do_something() will return None. If the condition is not met, it will return some_value. It’s important to note that None is a special object in Python and can be used in conditional statements like any other value. For example:

result = do_something()
if result is None:
    print("The result is None.")
else:
    print("The result is:", result)

In the code above, if the do_something() function returns None, the message “The result is None.” will be printed. Otherwise, the value of result will be printed.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply