Skip to content

Python function return variable

  • by

You can return variables from Python functions the same as return any value. You have to make sure variables are in the scope of the function.

Note: Python function is not required to return a variable, it can return zero, one, two, or more variables.

Python function return variable

Simple example code store a return value in a variable in Python.

# int var
def func():
    a = 10
    return a


print(func())


# string var
def func2():
    x = "random"

    print("random")
    return x


func2()

Output:

Python function return variable

Or

def myfunction():
    value = "myvalue"
    return value

var = myfunction()
print(var)

Output: myvalue

Do comment if you have any doubts or suggestions on this Python function 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.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading