- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
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}$.
- Magnifying power of a concave lens is(a) always > 1 (b) always < 1 (c) always = 1 (d) can have any value
- 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
- 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 product of a non-zero rational and an irrational number is(A) always irrational(B) always rational(C) rational or irrational(D) one
- Elements with valency 1 are(a) always metals(b) always metalloids(c) either metals or non-metals(d) always non-metals
- Elements having valency 'one' are :(a) always metals(b) always non-metals(c) always metalloids(d) either metals or non-metals
- Check if a string follows a^n b^n pattern or not in Python
- 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.
- 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}}$.
- 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 )
- Show that the following grammar is LR (1) S → A a |b A c |B c | b B a A → d B → d
- 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}) )

Advertisements