Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

^=, -= and += symbols in Python

Tags:

I am quite experienced with Python, but recently, when I was looking at the solutions for the codility sample tests I encountered the operators -=, +=, ^= and I am unable to figure out what they do. Perhaps could anyone explain the context in which they are used?

like image 877
Mc Tor Avatar asked Jun 15 '16 20:06

Mc Tor


People also ask

What does -= mean in Python?

-= Subtraction Assignment Subtracts a value from the variable and assigns the result to that variable.

What is the += symbol in Python?

+= adds another value with the variable's value and assigns the new value to the variable. -= , *= , /= does similar for subtraction, multiplication and division.

Is it += or =+ in Python?

This operator is called the addition assignment operator. This tutorial discussed, with reference to an example, the basics of Python operators and how to use the += assignment operator. Now you're equipped with the knowledge you need to use the =+ operator like a professional Python developer!

What does a% mean in Python?

The % operator is mostly to find the modulus of two integers. a % b returns the remainder after dividing a by b . Unlike the modulus operators in some other programming languages (such as C), in Python a modulus it will have the same sign as b , rather than the same sign as a .


2 Answers

As almost any modern language, Python has assignment operators so they can use them every time you want to assign a value to a variable after doing some arithmetic or logical operation, both (assignment and operation) are expressed in a compact way in one statement...

Table from Tutorials Point:

Operator Description Example
= Assigns values from right side operands to left side operand c = a + b assigns value of a + b into c
+= Add AND It adds right operand to the left operand and assign the result to left operand c += a is equivalent to c = c + a
-= Subtract AND It subtracts right operand from the left operand and assign the result to left operand c -= a is equivalent to c = c - a
*= Multiply AND It multiplies right operand with the left operand and assign the result to left operand c *= a is equivalent to c = c * a
/= Divide AND It divides left operand with the right operand and assign the result to left operand c /= a is equivalent to c = c / a
%= Modulus AND It takes modulus using two operands and assign the result to left operand c %= a is equivalent to c = c % a
**= Exponent AND Performs exponential (power) calculation on operators and assign value to the left operand c **= a is equivalent to c = c ** a
//= Floor Division It performs floor division on operators and assign value to the left operand c //= a is equivalent to c = c // a
like image 132
ΦXocę 웃 Пepeúpa ツ Avatar answered Oct 31 '22 02:10

ΦXocę 웃 Пepeúpa ツ


When you compute X = X + Y you are actually returning the sum of X and Y into a new variable, which, in your example, overwrites the previous value of X. When you use an assignment operator in the form of X += 1, the value 1 is directly summed on the current value of X, without returning the result in a new variable. Take a look at the code below:

>>> V = np.arange(10) >>> view = V[3:]        # view is just a subspace (reference) of the V array >>> print(V);print(view) [0 1 2 3 4 5 6 7 8 9] [3 4 5 6 7 8 9]  >>> view = view + 3     # add view to a constant in a new variable  >>> print(V);print(view) [0 1 2 3 4 5 6 7 8 9] [ 6  7  8  9 10 11 12] >>> view = V[3:] >>> view += 3           # here you actually modify the value of V >>> print(V);print(view) [ 0  1  2  6  7  8  9 10 11 12] [ 6  7  8  9 10 11 12] 

You can also look for the documentation of numpy.ndarray.base to check if an array is actually a reference of another array.

like image 25
Oliari Avatar answered Oct 31 '22 00:10

Oliari