


Detailed explanation of examples of connectors (+, +=) in Python
Preface
This article introduces the connectors (+, +=) in Python to you in detail through the problems found in a piece of sample code. Without further ado, let’s take a look at the detailed introduction. Bar.
Suppose there is the following piece of 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 a print b print c
The meaning of this code is that there are three lists, and it is necessary to fill 0 at the end of the list whose length is not 10, so that The length becomes 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, now the requirements have changed and we need to pad the front of the list with a length other than 10 with zeros.
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)) + item print a print b print c
Look at the output directly:
[1, 2, 3, 4] [5, 6, 7, 8, 9] [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
The result is not That's what we imagined. If you don't see the problem, keep reading. Of course, if you have already seen the clues, there is no need to waste time here.
According to our inherent thinking, the above method is feasible, such as the following example:
>>> l = [1, 2, 3, 4, 5] >>> l = [0]*5 + l >>> l [0, 0, 0, 0, 0, 1, 2, 3, 4, 5]
Such an operation allows the list to get the changes we expect.
But what if we add a few 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
At this point, have you seen the problem? As you can see from the output of the id() method, the "l" at the back is no longer the "l" at the front.
Look at the following example again:
>>> 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 using +=, "l" is preceded and followed by one. At this point, we should understand the fact that the example at the beginning of the article is not inexplicable, but has a reason.
Don’t worry, let’s look at the example again:
>>> 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
As you can see, when we replace the list with a tuple, the result changes again.
So what if we use the + operation on 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, no different.
So, 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 tuple, "s" is using += to concatenate a string Later, it was reassigned a value, and it was no longer the previous variable. Reflected in the memory, "s" has been opened up an additional storage space to store the value.
Here, the Python connectors we are going to talk about are + and +=. It should be noted that these two symbols have different meanings in Python. One is the addition operation used in mathematics, and the other is the splicing function used on sequence types. However, when used as an addition operator, it also follows the usage rules discussed in this article. Because discussing these two symbols is essentially discussing Python's immutable and mutable, that is, variable types and immutable types. For mutable types, we can modify the variable in place, which means that its storage space is readable and writable, such as a list; for immutable types, its storage space is read-only. , it cannot be modified. If you need to perform certain operations on the immutable type to get a new result, you need to create a new storage space to store the newly generated result.
From the examples listed above, we can draw the following conclusions:
For variable types:
+: represents the connection operation, the result of which will create a new object.
+=: Represents the append operation, that is, the in-place operation, which appends the content of another object to the object in place.
For immutable types: + and += both represent connection or summation operations. There is no difference between the two. The result of the operation will produce a new object.
Let’s analyze the example at the beginning of the article. Since for iteration is equivalent to assignment, for the sake of simplicity, we only analyze a, 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 actually operates on a, and += operates in place, so when t is changed, a also changes; if + is used to operate on t, and the result is assigned to t, then At this time, t no longer points to a, but points to [0]*6 + t, so a has not been changed.
Summary
The above is the entire content of this article. What is discussed here is just a simple issue, but I have spent such a long time talking about this issue, so I want to say The thing is, if you don't fully understand these small issues, they may cause you trouble during the programming process.
For more detailed examples of connectors (+, +=) in Python, please pay attention to the PHP Chinese website for related articles!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

Using python in Linux terminal...
