Full-stack Python development: python Assignment Method, python Assignment Method

Source: Internet
Author: User

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.

 

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.