Full-stack Python development: python Assignment Method, python Assignment Method
Chained assignment
A = 1b = 1c = 1 # For variables with the same value, the following method can be used to assign a value for a = B = c = 1 print (id (a), a) print (id (B), B) together) print (id (c), c)
Result:
Cross assignment
# Swap m, n value m = 1n = 2 # general method temp = mm = nn = tempprint (m, n) # Cross assignment m, n = n, mprint (m, n)
Extract variable
# Here is a list of nums = [1, 2, 3, 4, 5] a, B, c, d = numsprint (a, B, c, d) # result 1 2 3 4 # if the number of variables does not match the number of elements, the following error occurs: nums = [1, 2, 3] a, B, c, d = nums # ValueError: not enough values to unpack (expected 4, got 3) # If you only want to extract a portion of (, 5) nums = [, 5] a, _, _, B, c = numsprint (a, B, c) # result 1 4 5 # unwanted values can pass _ (underline) placeholder # Do not use * _ nums = [1, 2, 3, 4, 5] a, * _, B = numsprint (a, B) for multiple values in the middle # result 1 5
Summary:
Simple decompression is assigned to multiple variables, provided that the number of variables is consistent with the number of sequence elements.
For variable extraction, we recommend that you obtain the header or tail data. Intermediate data is not recommended.