python-How to return multiple values

Source: Internet
Author: User

The return statement of a function can return only one value, which could be any type.

Therefore, we can " return a tuple type to indirectly reach multiple values ".

Example: the remainder of x divided by Y and quotient function

def F1 (x, y):

A = x% y

b = (x-a)/y

return (b) # can also write return a, b

(c, D) = F1 (9, 4) # can also write C, d = F1 (9, 4)

Print C, D

Results showed: 1, 2

Python, like most other languages, has local variables and global variables, but it does not have a distinct variable declaration. The variable is generated by first assignment and automatically dies out when it goes out of scope.

Example 3.17. DefinedMyparamsVariable

"__main__":
Myparams = {"server":"Mpilgrim",/
"Database":"Master",/
"UID":"sa",/
"PWD":"secret"/
}

First notice the indentation. The if statement is a block of code that needs to be indented like a function.

Second, the assignment of a variable is a command that is divided into multiple lines, with a backslash ("/ ") as the continuation character.

When a command is split into multiple rows with a continuation character ("/ "), subsequent lines can be narrowed in any way, and the usual strict rules of the regular Python are not adhered to. If your Python IDE is free to zoom in on subsequent lines, you should treat it as a default unless you have specific reasons not to do so.

Strictly speaking, expressions in parentheses, square brackets, or curly braces (such as defining a dictionary) can be split into multiple lines with or without a continuation character ("/ "). Even when it's not necessary, I like to use a continuation character because I think it makes the code easier to read, but it's just a matter of style.

Thirdly, you have never declared the variable myparams , you just assigned a value to it. This is like VBScript has no option explicit options. Fortunately, unlike VBScript,Python does not allow you to reference a variable that is not assigned, and attempting to do so throws an exception.

3.4.1. Variable reference Example 3.18. Referencing a variable that is not assigned a value
X
Traceback (innermost last):
File "<interactive input>", line 1, in?
Nameerror:there is no variable named ' x '
x = 1
X
1

Sooner or later, you'll thank Python for that.

3.4.2. Assigning multiple values at once

A programming shorthand for comparing "cool" in Python is to use sequences to assign values to multiple variables at once.

Example 3.19. Assign multiple values at once
v = (' e ')
(x, y, z) = V
X
A
Y
' B '
Z
E
v is a tuple of ternary, and (x, Y, z) is a tuple of three variables. Assigning a tuple to another tuple assigns each value of v to each variable in order.

There are many uses for this kind of usage. I often want to assign a certain range of values to multiple variables. In the C language, you can use the enum type to manually list each constant and its corresponding value, which is especially tedious when the values are continuous. In Python, you can use the built-in range function and multivariate assignment methods to quickly assign values.

Example 3.20. Continuous value Assignment
>>>  range (7) 
[0, 1, 2, 3, 4, 5, 6]
>>> (MONDAY, Tuesday, Wednesday, Thursday, FRIDAY, SATURDAY, SUNDAY) = range (7)
>>> monday
0
>>> tues Day
1
>>> sunday
6
  function returns a list of elements that are integers. The simplified invocation of this function is to receive an upper value, and then return a list of the initial value starting at 0, which is incremented until the upper value is not included. (If you prefer, you can pass in additional parameters to specify a non-&NBSP;0 the initial value of the   and the stride length of the non- 1 . You can also use the &NBSP;print range.__doc__   to learn more details. )
MONDAY , Tuesday , Wednesday , Thursday , FRIDAY , SATURDAY and SU Nday is a variable that we define. (This example comes from the calendar module.) It is a very interesting printed calendar of small modules, like the UNIX Cal command. This calendar module defines the integer constant representation of the day of the week. )
Now each variable has its own value: The value of MONDAY is 0 , the value of Tuesday is 1 , and so on.

You can also use a multivariate assignment to create a function that returns multiple values, as long as a tuple containing all the values is returned. The caller can treat it as a tuple, or assign a value to a separate variable. Many of the standard Python libraries do this, including OS modules.

python-How to return multiple values

Related Article

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.