Python variable Exchange

Source: Internet
Author: User

The way to exchange two variables in Python is to use automatic tuple packaging and packet.

eg
>>> a=3>>> b=4>>> a,b=b,a>>> print a4>>> print B3 multi-Program language lets you use temporary intermediate variables to swap the values of variables.excerpt from Daniel's introduction: in an assignment, Python requires an expression to the right of the equals sign. What we have written here is B, C, and a is really an expression. To be clear, it is a tuple that has three immutable sequences of elements. Tuples are often enclosed in parentheses, like (b, C, a). But here in addition to the comma there are some other meanings (in a function call), the parentheses are not necessary. Create a tuple with commas by packing each item of a tuple.  on the left side of the equal sign of an assignment statement, you can use a single object normally. This object can be a simple notation (known as a variable), an index (such as alist[i] and adict[' Freep '), a reference to a property (such as Anobject.someattribute), and so on. However, Python also lets you use several comma-separated objects (variables, indexes, and so on) to the left of the assignment statement. Such a multi-assignment is also known as unpacking assignment. When there are two or more comma-separated objects on the left side of an assignment statement, the value on the right must be a sequence, and its number is as many as the comma-delimited object on the left. Each item of the sequence is assigned to the corresponding target value from left to right.

Python variable Exchange

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.