Python a += b is not always a = a + b
If two variables are of the same data types and not iterators like list and dictionary etc, then the expressions a += b is same as a =+b gives the same result. But when n iterator is involved we can not always expect the same. Below is one of such scenario.
Case of a = a +b
Here we can see when we apply the expression to a list and a string expecting they will get merged, we get back an error.
Example
x ='Hello '
z_list = [1,2,3]
z_list = z_list + x
print(z_list)
Output
Running the above code gives us the following result −
Traceback (most recent call last):
File "C:\Users\Pradeep\AppData\Roaming\JetBrains\PyCharmCE2020.3\scratches\scratch.py", line 11, in
z_list = z_list + x
TypeError: can only concatenate list (not "str") to list
Case of a += b
But when we apply the expression a += b we see that the sting implicitly gets converted to series of elemnst to become a part of the list.
Example
Live Demo
z_list = [1,2,3]
x ='Hello'
z_list += x
print(z_list)
Output
Running the above code gives us the following result −
[1, 2, 3, 'H', 'e', 'l', 'l', 'o']
Related Articles
- Let $a$ and $b$ be positive integers. Show that $\sqrt{2}$ is always between $\frac{2}{b}$ and $\frac{a+2 b}{a+b}$.
- Consider two statements A and B given below:A: real image is always invertedB: virtual image is always erectOut of these two statements:(a) only A is true (b) only B is true (c) both A and B are true (d) none is true
- Magnifying power of a concave lens is(a) always > 1 (b) always < 1 (c) always = 1 (d) can have any value
- The mean is one of the numbers in the given data :(a) Yes (b) No (c) Not always
- Which of the following is/are not always necessary to observe a shadow?$(a)$. Sun$(b)$. Screen$(c)$. Source of light$(d)$. Opaque object
- Prove that $a^2 + b^2 + c^2 – ab – bc – ca$ is always non-negative for all values of $a, b$ and $c$.
- The area of a triangle with vertices \( (a, b+c),(b, c+a) \) and \( (c, a+b) \) is(A) \( (a+b+c)^{2} \)(B) 0(C) \( a+b+c \)(D) \( a b c \)
- The product of a non-zero rational and an irrational number is(A) always irrational(B) always rational(C) rational or irrational(D) one
- Check if a string follows a^n b^n pattern or not in Python
- If $\frac{1}{b} \div \frac{b}{a} = \frac{a^2}{b}$, where a, b not equal to 0, then find the value of $\frac{\frac{a}{(\frac{1}{b})} - 1}{\frac{a}{b}}$.
- Show that \( \frac{a \sqrt{b}-b \sqrt{a}}{a \sqrt{b}+b \sqrt{a}}=\frac{1}{a-b}(a+b-2 \sqrt{a b}) \)
- Elements with valency 1 are(a) always metals(b) always metalloids(c) either metals or non-metals(d) always non-metals
- Three vertices of a parallelogram are $(a + b, a – b), (2 a + b, 2a – b), (a – b, a + b)$. Find the fourth vertex.
- If the point $P( x,\ y)$ is equidistant from the points $A( a\ +\ b,\ b\ –\ a)$ and $B( a\ –\ b,\ a\ +\ b)$. Prove that $bx=ay$.
- Which of the following statement is not correct for an object moving along a straight path in an accelerated motion?$(a)$. Its speed keeps changing.$(b)$. Its velocity always changes.$(c)$. It always goes away from the earth.$(d)$. A force is always acting on it.
Kickstart Your Career
Get certified by completing the course
Get Started