How to use the return keyword in python?

The return statement exits the function and returns a value back to the caller. A return keyword can only be used inside a function. If no value or expression is provided after a return keyword a special value None is returned to the caller.

The below examples illustrates the use of the return keyword.

Example 1: Returning a string value.

def my_function():
    return "Hello World"

print(my_function())

The above code returns the output as "Hello World".

Hello World

Example 2: Returning an expression.

def my_function():
    return  10 - 5

val = my_function()
print("The value returned: ", val)

The above code returns the output as 5.

The value returned:  5

Example 3: Return keyword followed by no value.

def my_function():
    return  

val = my_function()
print("The value returned: ", val)

The function returns the value None.

The value returned:  None
The statements after the return statement are not executed in the function.

Consider, the function my_function() that contains a return statement followed by some other statements. The statement after the return statement is not executed.

def my_function():
    return  "Welcome to Chercher Tech"
    print("Python Programming")

print( my_function())

Output

Welcome to Chercher Tech

How to return multiple values in python?

0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions