Python learning Python -- 2.3.5 Python returns multiple values

Source: Internet
Author: User
Tags define function

This section describes how Python functions return multiple values at the same time.

When learning Python, I was surprised to find that Python functions can return multiple values at the same time, which is very interesting.

#define function sumdef sum(x, y):   z = x + y   x = x ^ y   y = x ^ y   x = x ^ y      return z, x, y#define function maindef main():   a = int(raw_input("plz input a "))   b = int(raw_input("plz input b "))   c = sum(a, b)   print(c)#entry of programmemain()

First, let's take a look at the execution results of the program.

plz input a 12plz input b 13(25, 13, 12)

In the result (, 12), it looks strange. This is the tuple (tuples) in Python. It's a new term. It doesn't matter. We will mention it later in list. In the sub-function sum, in addition to the sum (3rd rows), there are operation operations for exchanging the values of x and y (4th ~ 6 rows), returns the print results (7th rows) of z, x, and y (13th rows) to the c (14th rows) of the main function. The x and y values are indeed exchanged.

Does a and B in the main function change? A, 12, B, or 13? In the main function, we add two print statements after print (c) (14th rows) to print the values of a and B (15th and 16 rows), respectively ), check if a and B have changed?

#define function sumdef sum(x, y):   z = x + y   x = x ^ y   y = x ^ y   x = x ^ y      return z, x, y#define function maindef main():   a = int(raw_input("plz input a "))   b = int(raw_input("plz input b "))   c = sum(a, b)   print(c)   print(a)   print(b)#entry of programmemain()

Running result

plz input a 12plz input b 13(25, 13, 12)1213

  

The conclusion is that the values of a and B are not affected.

 

 

 

 

 

 

When the sum function is called, the values of a and B are transmitted to x and y (both a and x point to the data unit of 12) (both B and y point to the data unit where 13 is located.) In the sum function, x and y change the data unit that x and y point.

 

 

 

 

 

 

-------------------------------------------

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.