Python function returns multi-value

Source: Internet
Author: User

Returns multiple values
Can a function return multiple values? The answer is yes.
For example, in the game often need to move from one point to another point, give the coordinates, displacements and angles, you can calculate the new coordinates:
# The Math package provides the sin () and cos () functions, which we first refer to with import:

Import mathdef Move (x, y, step, Angle):    NX = x + step * math.cos (angle)    NY = y-step * Math.sin (angle)    return NX, Nyprint (Move (+, +, MATH.PI/6))

  

But this is just an illusion, and the Python function returns a single value:
>>> r = Move (+, +, MATH.PI/6)
>>> Print R
(151.96152422706632, 70.0)
Prints the returned result with print, the original return value is a tuple!
However, in syntax, the return of a tuple can omit parentheses, and multiple variables can receive a tuple at the same time, by the location of the corresponding value, so the Python function returns a multi-value is actually returned a tuple, but it is more convenient to write.

Python function returns multi-value

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.