Examples of connectors (+, + =) in Python, and python examples

Source: Internet
Author: User

Examples of connectors (+, + =) in Python, and python examples

Preface

This article introduces the connector (+, + =) in Python in detail through the issues found in the sample code. Let's take a look at the details below.

Suppose there is the following code:

a = [1, 2, 3, 4]b = [5, 6, 7, 8, 9]c = [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]for item in (a, b, c): item += [0] * (10 - len(item))print aprint bprint c

This Code indicates that there are three lists. You need to fill in 0 at the end of the list with a length of less than 10 to change its length to 10.

The output is as follows:

[1, 2, 3, 4, 0, 0, 0, 0, 0, 0][5, 6, 7, 8, 9, 0, 0, 0, 0, 0][11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

There is no problem here. Everything is normal. However, the requirement has changed. You need to fill 0 in front of the list with a length of less than 10.

Then, we try to make the following changes:

a = [1, 2, 3, 4]b = [5, 6, 7, 8, 9]c = [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]for item in (a, b, c): item = [0] * (10 - len(item)) + itemprint aprint bprint c

Let's take a look at the output:

[1, 2, 3, 4][5, 6, 7, 8, 9][11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

The results are not what we think. If you have not found the problem, continue. Of course, if you have seen the clues, you do not need to waste time here.

According to our inherent thinking, the above method is feasible, for example, the following example:

>>> l = [1, 2, 3, 4, 5]>>> l = [0]*5 + l>>> l[0, 0, 0, 0, 0, 1, 2, 3, 4, 5]

This operation allows the list to be changed as expected.

However, what if we add several more steps:

>>> l = [1, 2, 3, 4, 5]>>> id(l)139935500860952>>> l = [0]*5 + l>>> l[0, 0, 0, 0, 0, 1, 2, 3, 4, 5]>>> id(l)139935500783272

So far, have you seen the problem. Pass id() The method output shows that the "l" behind is no longer the "l" on the front.

Let's take a look at the example below:

>>> l = [1, 2, 3, 4, 5]>>> id(l)139935500861024>>> l += [0]*5>>> l[1, 2, 3, 4, 5, 0, 0, 0, 0, 0]>>> id(l)139935500861024

When + = is used, there is one before and after "l. At this point, we should understand the fact that the example at the beginning of the article is not inexplicable, but for a reason.

Don't worry. Let's take a look at the example:

>>> t = (1, 2, 3, 4, 5)>>> id(t)139935501840656>>> t += (0,)*5>>> t(1, 2, 3, 4, 5, 0, 0, 0, 0, 0)>>> id(t)139935502151336

We can see that when we replace the list with a tuples, The results change again.

Then we use + operations for tuples:

>>> t = (1, 2, 3, 4, 5)>>> id(t)139935501081200>>> t = (0,)*5 + t>>> t(0, 0, 0, 0, 0, 1, 2, 3, 4, 5)>>> id(t)139935502151336

This is the same as the list result.

Then, let's take a look at the string:

>>> s = "hello">>> id(s)139935500909712>>> s += "world">>> s'helloworld'>>> id(s)139935500909664

The result is like a tuples. "s" is re-assigned after a string is concatenated by "+ =", which is no longer the previous variable. In the memory, "s" is opened into another bucket to store values.

Here, the Python connector is + and + =. Note that the two symbols in Python have meanings. One is the addition operation in mathematics, and the other is the concatenation function used in the sequence type. However, as an addition operator, we also follow the rules discussed in this article. Because these two symbols are discussed, they are essentially the immutable and mutable of Python, that is, the variable type and the immutable type. For a mutable type, we can modify it in the original variable, that is, its storage space is readable and writable, such as list; for an immutable type, its bucket is read-only and cannot be modified. If you need to perform some operations on the immutable type to get new results, A new bucket is required to store the new results.

From the examples listed above, we can draw the following conclusions:

For variable types:

  1. +: Indicates the connection operation. A new object is created in the result.
  2. + =: Indicates the append operation, that is, the in-place operation, which appends the content of another object to the object.

For an unchangeable type, both the plus (+) and minus (+) represent the join or sum operation. There is no difference between the two, and the result of the operation produces a new object.

Next we will analyze the example at the beginning of the article. Because for iteration is equivalent to assigning values, we will analyze only a for the sake of simplicity, as shown below:

>>> a = [1, 2, 3, 4]>>> t = a>>> id(a)139712695835400>>> id(t)139712695835400>>> t += [0]*6>>> t[1, 2, 3, 4, 0, 0, 0, 0, 0, 0]>>> id(t)139712695835400>>> id(a)139712695835400>>> a[1, 2, 3, 4, 0, 0, 0, 0, 0, 0]>>>>>>>>> a = [1, 2, 3, 4]>>> t = a>>> id(a)139712695835464>>> id(t)139712695835464>>> t = [0]*6 + t>>> t[0, 0, 0, 0, 0, 0, 1, 2, 3, 4]>>> a[1, 2, 3, 4]>>> id(a)139712695835464>>> id(t)139712695835400

Here, t is a reference to a, which is equivalent to the item in the example at the beginning of the article. Using + = to operate on t is actually to operate on a, while + = is an in-situ operation, so when t is changed, a also changes; if you use + to operate on t, when the result is assigned to t, t no longer points to a, but to [0] * 6 + t, so a is not changed.

Summary

The above is all the content of this article. Here we are only discussing a simple question, but I have used such a long space to talk about this question. So what I want to say is, if you do not fully understand these small issues, it may cause you a lot of trouble in the programming process.

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.