Python Forum

Full Version: Assignment operator
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I cannot seem to grasp the difference between an arithmetic operator and an assignment operator.

Could someone please show point me to examples where the one is appropriate and the other would fail in practice.
arithmetic: +, -, *, /, ** (exponentiation), % (modulus), // (floor division, or integral part of a division)
assignment: =

Oddball: +=, -= shorthand assignment, a += 1 is shorthand for a=a+1. The plus is arithmetic, the equal is assignment.

Do not confuse with logical operator ==, which is used in comparisons as "is equal to"
if a == 1:

For a website with examples:
https://www.tutorialspoint.com/python/py..._operators
I'm not sure what you mean by arithmetic operator. The assignment operator (=) is often confused with the comparison operator (==), however.

>>> breakfast = 'spam'   # assigns a value to the breakfast variable
>>> breakfast            # show that value
'spam'
>>> breakfast == 'eggs'  # compares the breakfast variable to another value.
False
>>> breakfast == 'spam'
True
The assignment operator is for storing values in variables. The comparison operator is usually used in conditionals (if/elif) and while loops.
Assignment happens at places, where you don't expect it.
For example a for-loop is an assignment.
For each iteration the returned result from the iterator is assigned to the name.
When you create a class or a function, it's an assignment of the code block to a name.
The name is just a reference to the in memory living object.
An object can have many references (assigned to names).
The term variables is miss-leading.

a = 42
This code means, that a is now a reference to 42, which is the type int, which is in memory.
b = a
Now it's getting complicated. The name a is pointing to 42.
The name b is not pointing to a.
Instead it has the reference to object 42
If you change now the name a, b will still hold the reference to 42.
In other words, the assignment is by reference and not by value.

This is in Python a big pitfall.
There is another case, where you work with mutable types.
This means a value or many values can be changed on the object itself.
int, float, tuple, str, frozenset are immutable types, which could not change the value after creation.
The counter part are list, dict, set (and all other I forgot to name). They are mutable.
This means you can change the content of a list, dict, set etc. and all names, which have a reference to
the object which has still access to the mutated object.


It's very late, I do not have enough time to explain arithmetic operators with the right terms.
Familiar with quite a few programming languages, I am new to Python and so have URL's and some books to hand. They all seem to list under "Operators" : Arithmetic Operators and Assignment Operators. This clearly implies that they have a different function and that if you use the wrong one an unexpected result could appear. The matter is made worse by descriptions of the assignment operators having comments like "Assignment Operator x += 3 is the same as x = x + 3" Why would I bother with the assignment operator ?

Although I am grateful for the replies you have kindly taken the trouble to send, I still cannot figure out the underlying point.
(Aug-10-2019, 08:05 PM)DavidTheGrockle Wrote: [ -> ]"Assignment Operator x += 3 is the same as x = x + 3" Why would I bother with the assignment operator ?

x += 3 is just shorthand for x = x + 3. It's a common operation, so they made it easier. You can use it or not as you wish.

(Aug-10-2019, 08:05 PM)DavidTheGrockle Wrote: [ -> ]I still cannot figure out the underlying point.

The underlying point is that there are two different things going on. One is assigning a value to a variable, and the other is comparing two values to see if they are the same. Python, like many languages, uses two different symbols to represent this.
(Aug-10-2019, 08:05 PM)DavidTheGrockle Wrote: [ -> ]I still cannot figure out the underlying point.

Underlying point of assignments is very well explained by Ned Batchelder in Python Names and Values
.
A very good tutorial video i highly recommend to watch!
Ned Batchelder - Facts and Myths about Python names and values - PyCon 2015

Edit: If i would have clicked on perfringo´s link to Ned Batchelders website, i would have seen that he himself has included his recorded talk at PyCon 2015 first hand.
But i didn´t so we have two independent recommendations.