Foreword
The text and pictures of the text come from the Internet and are only for learning and communication. They do not have any commercial use. The copyright belongs to the original author. If you have any questions, please contact us in time for processing.
Author: kwsy
PS: If you need Python learning materials, you can click the link below to obtain http://t.cn/A6Zvjdun
1. Python packets
When assigning multiple values to a variable, Python will automatically encapsulate these values into tuples, this feature is called encapsulation
When the function returns multiple values, it will also be packaged
In practice, packet operations are rarely actively used
2. Python unpacking
Python unpacking is a very frequently used technology. The following lists several scenarios that use python unpacking
2.1 Receive function return value
The return value of the function is a tuple, and the left side is three variables, so that unpacking will occur, a, b, c are in turn equal to the elements in the tuple, the return value of the function has 3, and it is packed into a tuple , The left side of the assignment statement does not have to be 3 variables
Variable a is assigned the value 1, variable b is preceded by an asterisk, and the remaining 2, 3 will be unpacked as a list
2.2 Traversing the dictionary
2.3 Passing parameters
Using unpacking technology, you can unpack tuples into variable parameters and unpack dictionaries into keyword parameters. This technique is widely used in practice. For example, when using python to operate redis, if you want to add Value, you must use the unpacking end parameter
The sadd method is defined as follows
If you do not use the unpacking technique, you can only manually write the parameters one by one when calling the sadd method, which is time-consuming and laborious
2.4 Merging two dictionaries
Clever use of unpacking technology can easily and conveniently merge two dictionaries into a new dictionary