Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it mean += in Python?

Tags:

python

syntax

What does it mean when it's like this:

self.something += ('somethin',)

What does "+=" mean and what does the comma mean?

like image 261
hyeomans Avatar asked Oct 11 '11 03:10

hyeomans


People also ask

What does a% mean in Python?

But in Python, as well as most other programming languages, it means something different. The % symbol in Python is called the Modulo Operator. It returns the remainder of dividing the left hand operand by right hand operand. It's used to get the remainder of a division problem.

What does '@' mean in Python?

Decorators. The main use case of the symbol @ in Python are decorators. In Python, a decorator extends the functionality of an existing function or class.

What does %d mean in Python?

What does %d do in Python? The %d operator is used as a placeholder to specify integer values, decimals, or numbers. It allows us to print numbers within strings or other values. The %d operator is put where the integer is to be specified. Floating-point numbers are converted automatically to decimal values.

What is >> and << in Python?

They are bit shift operator which exists in many mainstream programming languages, << is the left shift and >> is the right shift, they can be demonstrated as the following table, assume an integer only take 1 byte in memory.


3 Answers

The expression a += b is shorthand for a = a + b, where a and b can be numbers, or strings, or tuples, or lists (but both must be of the same type).

The comma in ('x',) means that this is a tuple of a single element, 'x' . If the comma is absent, is just an 'x' between parenthesis.

like image 54
Óscar López Avatar answered Oct 06 '22 00:10

Óscar López


Python has an operator to assign value to a name and it's =.

The language also support many other operators like +, -, ** for operations defined in special methods of your objects.

Although + is the math sign to add things, it can be customized to do whatever you want.

Sometimes you want to make an operation and store it using the same name. For these situations you have in-place operators that are just the normal operators you're use to plus the = sign.

For immutable objects (numbers, strings, tuples,...) you can't have in-place changes because... they're immutable. So, the in-place methods do exactly the same thing the normal method followed by an assignment.

For mutable objects the difference is much clear:

In-place add:

>>> a = []
>>> b = a
>>> b += [1,2]
>>> a
[1, 2]

Add and assign:

>>> a = []
>>> b = a
>>> b = b + [1,2]
>>> a
[]

See? The object itself was transformed with the in-place add for lists, but, on the other case, a new object was created.


For your other question, the comma is the tuple separator.

a = (1)   # Just number 1 inside parenthesis
a = (1,)  # A tuple with one element
like image 42
JBernardo Avatar answered Oct 06 '22 01:10

JBernardo


+= is addition and assignment into one (sometimes referred to as iadd or in-place add). It is the same as a = a + x

a = 4
a += 5  # add 5 to a, and assign the result into a
b = 4
b = b + 5   # this does the same thing as +=
print a  # prints out 9
print b  # prints out 9

You can also do other operations in this style, such as -=, *=, /=, &= (bitwise and), |= (bitwise or), ^= (bitwise xor), %= (mod), **= (exponent).


('something',) is a tuple. ('something') (without the comma) is using the parenthesis in grouping, kind of like ('some' + 'thing') or (a + b). In order to differentiate between the one-member tuple and the grouping syntactically, Python uses a comma.

like image 27
Donald Miner Avatar answered Oct 06 '22 01:10

Donald Miner