What does it mean when it's like this:
self.something += ('somethin',)
What does "+=" mean and what does the comma mean?
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.
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 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.
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.
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.
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
+=
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With