Fun Python

xiaoxiao2021-04-03  246

If we exchange X and Y values ​​with C or C language need to be implemented using an intermediate variable (of course, it can be implemented without using the intermediate variable), but for the Python language, the value of the two variables is very simple:

x, y = y, x

This achieves the exchange. Is it fun, in fact, for the Python language, there is a function called sequence unpacking.

Atuple = (1, 2, 3)

This is a "tuple package" because the value is packaged into the tuple. The tuple and other sequences can also be unpacking --- assigns the value stored in the sequence to each identifier. By "unpacking", a plurality of variable assigned values ​​in a single statement is an effective means of simplifying programming.

x, y = y, x

It is to fully utilize the functions of packaging and unpacking.

转载请注明原文地址:https://www.9cbs.com/read-131501.html

New Post(0)