How to return int value from python function

I am really new to Python and found this snippet on the Internet that I changed, right now I have print x * y, but I want to be able to return it as an int value, so I can use it later in the script.

I am using Python 2.7.6.

def show_xy(event):
    xm, ym = event.x, event.y
    x3 = xm * ym
    print x3
root = tk.Tk()
frame = tk.Frame(root, bg = 'yellow', 
                width = 300, height = 200)
frame.bind("<Motion>", showxy)
frame.pack()

root.mainloop()

Regards, Postie

+4
source share
2 answers

To return a value, you simply use returninstead print:

def showxy(event):
    xm, ym = event.x, event.y
    x3 = xm*ym
    return x3

A simplified example:

def print_val(a):
    print a

>>> print_val(5)
5

def return_val(a):
    return a

>>> result = return_val(8)
>>> print result
8
+4
source

Using "return", you can return it from a function. In addition, you can specify the data type.

Example:

def myFunction(myNumber):
    myNumber = myNumber + 1
    return int(myNumber)

print myFunction(1)

Conclusion:

2

, , ()

print type( myFunction(1) )

:

<type 'int'>
0

Source: https://habr.com/ru/post/1540021/


All Articles