Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Assignment operator
#1
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.
Reply
#2
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
Reply
#3
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
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.
Reply
#6
(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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
(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
.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#8
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  assignment: not an operator nor expression, but x=y=z=3 works fine? jefdaels 1 2,146 Jan-29-2019, 02:19 PM
Last Post: perfringo
  How many variables/values are limited to single assignment operator? Prabakaran141 1 2,004 Sep-06-2018, 03:32 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020