Answers.Body: <p>I don't teach CS in any official capacity but I have tutored a number of friends and colleagues on the basics. I find that differentiating the language I use when describing the different concepts helps a lot, for instance:</p>
  
  <ul>
  <li>Mathematical <code>x=x*x</code> = "X equals X squared"</li>
  <li>Assignment <code>x=x*x</code> = "X is made equal to X squared"</li>
  <li>Comparison <code>x==x*x</code>= "Test if X is equal to X squared"</li>
  </ul>
  
  <p>Using the above terms when talking through code I have never been asked to explain the difference between assignment and comparison. YMMV</p>
  
  <p>I think this stemmed from the first language I was taught academically was pascal where the assignment operator is <code>:=</code> and comparison is <code>=</code>, having that difference caused me to internally narrate my code in this way, which has helped my understanding and my vocabulary when explaining to others.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-03T12:50:57.047Z
Answers.Text: YMMV? what does that mean?
Answers.UserDisplayName: ItamarG3
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-03T14:01:02.263Z
Answers.Text: Welcome to [cseducators.se]!
Answers.UserDisplayName: Ben I.
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-04-05T21:00:10.190Z
Answers.Text: @ItamarG3 Your Mileage May Vary, meant as an acknowledgement that my experience may differ from someone else applying this technique.
Answers.UserDisplayName: RobbG
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-03T12:49:48.903Z
Answers.LastActivityDate: 2017-07-03T12:49:48.903Z
Answers.UserDisplayName: RobbG
Answers.Body: <p>All of the below is meant to be thoughts/analogies/illustrations to help explain the difference (meant to be said to students) but in reality, this is all dependent on the level of your students.</p>
  
  <hr>
  
  <p>The expression <code>x == x**2</code> in python is a "test" - is it true, or is it not? Much like when defining a set, you have $\{x|x&gt;2\}$ (i.e., x is only included <em>iff</em> $x&gt;2$) - a test to see if something should be included in the set, whereas in python, it is often used to check if something should be done. In this line of reasoning, you could write a program to define a set, expanding on what exactly the mathematical notation means:</p>
  
  <pre><code>if x &gt; 2:
      list.append(x)
  </code></pre>
  
  <p>In other words, <code>if x &gt; 2 is True</code> then <code>do list.append(x)</code>.</p>
  
  <p>On the other hand, the expression <code>x = 5</code> in python is an "assignment" - this is equal to this, no need for argument. *This is much like in math assigning $x = 5$ or something of the sort. We can later use <code>x</code> to represent 5, much like in an equation $x+5$, given the above definition, we know that the result is $10$, because we are using $x$ to represent $5$. A common analogy is that of the buckets - a bucket has <code>x</code> painted on it and whatever is in the bucket is what <code>x</code> represents. </p>
  
  <p>However, things get more complicated from there - what if <code>x = x**2</code>? Then, you take what <code>x</code> was previously defined as, square it, and then <em>redefine</em> <code>x</code> as equal to the new thing. For example, let's "run" the following program:</p>
  
  <pre><code>x = 5
  print(x)
  x = x**2
  print(x)
  x = x**2
  print(x)
  </code></pre>
  
  <p>What will we see? Well, it is quite obvious that the first thing printed is "5"; I doubt there will be much confusion there. Now, the next line is squaring the previous <code>x</code>, i.e., squaring 5 ($5^2$) and then setting that equal to <code>x</code>, so now we basically have the line of code <code>x = 5**2</code> - which produces 25. All very well and good, now we do the same thing - so we basically have <code>x = 25**2</code> or 625. </p>
  
  <p>The difference between those two and what they mean is a very useful and important distinction, but to be honest I think it's not completely unintuitive. Once explained the former is a test and the latter a definition, I think students will catch on fairly quickly with a reminder here and there.</p>
  
  <p>Now we come to your last (well, first) type of equality. $x = x^2$ is a statement of definition, but it can also be manipulated. In python <code>x = x**2</code> is a definition, and it accesses the <em>previous definition</em> of <code>x</code> to "solve" it, like we did above. However, to solve $x = x^2$, we move terms around the equals sign - this is a quadratic, so we get $0 = x^2 - x$ and we can factor out the $x$ so $0 = x(x - 1)$ so using the zero product property one gets $x=0$ or $x=1$. We can solve equations like this in Python, but that's not automatically done when you define <code>x = x**2</code>, instead it uses previous definitions.</p>
  
  <p>I hope that conveys what you are wanting, but I really don't think you need to bring up your first definition of equality - the rules of that have been drummed into them from math class, and the new rules of equality appear distinctly enough that I doubt they will be confused.</p>
  
  <p>Now, all of the above is with a nice, good language like Python. <em>Some</em> other languages have <code>=</code>, <code>==</code>, and <code>===</code>. I'm not qualified to get into that, but this might not be valid for such craziness.</p>
  
  <hr>
  
  <p>As for <em>when</em> this all should be pointed out - the uses for these concepts are so fundamental, this really is almost day 1 (maybe day 2) of class - install/open the IDE/whatever, learn how to print, then start with variables. This is one of the most important things to learn in a language like python - how to use variables and manipulate them to your advantage.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-07-03T21:51:48.907Z
Answers.Text: I get what you are trying to say in each sentence but the use of language in this post is so sloppy/fuzzy that it is the sort of writing/presentation that is why students get confused.
Answers.UserDisplayName: philipxy
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-01T17:57:25.803Z
Answers.LastActivityDate: 2017-07-02T18:30:46.907Z
Answers.LastEditDate: 2017-07-02T18:30:46.907Z
Answers.UserDisplayName: auden
Answers.Body: <p>I'm going to explore an orthogonal concept that sheds light on the question at hand: What is a variable? </p>
  
  <p>In mathematics a variable is a representative for some value, a name. It may be a name for something we know or don't (abstraction comes in here). In an equation, the variable is sometimes called an "unknown". But that isn't precisely true. An equation is a constraint on which values are appropriate for reference. That is because our typical use of equations is to find solutions (i.e. the set of values for which the constraint is true). The value is only "unknown" if we haven't (or can't) solve the equation. But, in math, a variable is misnamed in that (again, normally - there can be exceptions) the value doesn't "vary". So the math terminology is an historic accident to some extent, not absolute "truth." </p>
  
  <p>In computing, on the other hand, a variable refers to a value (also), but the value referred to CAN change over time (or with context). So in computing a variable varies. </p>
  
  <p>In math a variable refers to something (or a set of somethings) that doesn't change. </p>
  
  <p>In computing a variable can refer to different things as a computation proceeds. </p>
  
  <p>So, in maths = represents a relationship. But in Java = (Pascal :=, somewhere else &lt;-, a favorite of mine to see, but harder to type on most keyboards) represents an action: changing a reference. </p>
  
  <p>And in java == is an imperfect representation of maths =. It is a relationship (has value true or false). (imperfect since, while 1 == 0.2 + 0.2 + 0.2 + 0.2 + 0.2 is true, 2 == 0.2 + 0.2 + 0.2 + 0.2 + 0.2 + 0.2 + 0.2 + 0.2 + 0.2 + 0.2 is false)</p>
  
  <p>Part of the confusion of some beginning students who "know" some math and are learning computing is that they don't understand the math well enough. So giving them a solid understanding of what $x^2 - 2x + 1 = 0$ means to a mathematician may help. </p>
  
  <p>I think that Ritchie and Thompson just hated Pascal's := (too many keystrokes) and so wanted to simplify it when they created C. But, in their defense, they may have been programming on a card punch where only 80 characters were permitted per card and mistakes were costly (repunch the entire card). Typing one extra character slowed down the programmer too much (unless you were lucky enough to employ a keypunch operator). The issue of thinking speed v typing speed is lessened now, but it doesn't go away. However, it is still true that typing is not what makes programming hard. </p>
  
  <p>It is easy (in java) to illustrate the difference between = and ==. Suppose x and y are boolean variables. Then if(x==y), and if(x=y) are both legal, but very different in effect (unless they are not, of course). Fortunately some modern development environments issue a warning when you try something legal but probably incorrect. </p>
  
  <p>But for the record, there are situations in java where you do need to make an assignment in the "test" of an if or while; doing I/O, for example, where certain conventions require it. </p>
  
  <pre><code>String line = null; 
  while ((line = reader.readLine()) != null)
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-01T21:40:16.000Z
Answers.Text: Re "a variable is a representative for some value" - so is a constant, which is the opposite of a variable.
Answers.UserDisplayName: alancalvitti
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-01T21:43:22.927Z
Answers.Text: Yes, of course, @alancalvitti, but as the discussion was about variables, I didn't give a fuller exposition. In some ways a constant is more like a math variable than a variable is, though that might just confuse the situation for students rather than clarify it.
Answers.UserDisplayName: Buffy
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-02T04:52:08.927Z
Answers.Text: I think the difference between a variable in algebra and a variable in CS deserves its own question and crows sourcing of ideas about how to clarify the difference for students.
Answers.UserDisplayName: Bennett Brown
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-02T07:04:18.943Z
Answers.Text: I actually can't think of any common languages where `3 == 3.0` would evaluate to false. Not that I know every common language, but it can't be all _that_ common, can it?.
Answers.UserDisplayName: David Z
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-02T08:12:10.927Z
Answers.Text: Hmmm. @DavidZ. Sorry, just me being too literal minded. I changed the example. Hopefully correct now.
Answers.UserDisplayName: Buffy
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-02T09:28:34.307Z
Answers.Text: @Buffy Sure, I'll take your word for this one. :-) (The point is clearer now anyway)
Answers.UserDisplayName: David Z
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-02T12:32:45.223Z
Answers.Text: Mistaking rounding error for an issue with the intent of the equivalence operator could cause more confusion.  The == means the same as the algebraic =, but the unequaled being reported is a function of rounding error inherent in representing decimal fractions in a binary forum. Like fractions of 9 in base 10, many fractions of 10 are infinitely repeating series in base 2
Answers.UserDisplayName: pojo-guy
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-01T18:56:24.727Z
Answers.LastActivityDate: 2017-07-02T08:10:44.333Z
Answers.LastEditDate: 2017-07-02T08:10:44.333Z
Answers.UserDisplayName: Buffy
Answers.Body: <blockquote>
    <p>When and how should the differences be pointed out to students?</p>
  </blockquote>
  
  <p>When: As soon as you introduce one of those operators...</p>
  
  <p>On the "how" - I'd try to not beat around the bush, so to speak, but to point out straight away that only because we use the same symbol, they still have <em>completely</em> different meanings.</p>
  
  <blockquote>
    <p>Are there examples producing unexpected results that are especially effective in causing students to abandon the idea that these three meanings of equality are equivalent?</p>
  </blockquote>
  
  <p>I'd say it is important to be very clear about the fact that these are <em>not</em> "three equalities".</p>
  
  <p>The <code>=</code> in these three statements are completely different (e.g.: mathematical constraint &lt;=> assignment operation &lt;=> equality expression) and have no particular relationship with each other except for using the same symbol and some vague, indirect and maybe confusing similarities.</p>
  
  <p>That is a great time to introduce related concepts:</p>
  
  <ul>
  <li>Operations and expressions are quite different in languages, especially imperative ones. Python is one of the few languages I never touched, but this would be very clear and easy to show in Java, C, Pascal (where expressions and operations are very much separated). A different imperative example would be Ruby, where almost everything is an expression, even "if ... then ... else". Or, in the extreme, you have LISP, where only very, very few things indeed exist which are not expressions. Obviously, you'd have to take whatever languages your pupils know, for that...</li>
  <li>There is no particular reason to assume that any given symbol used in a programming language needs to have any specific semantics, nor, that every semantic has the same symbol in every language. The designers of the language can more or less do whatever they want (for example <code>.</code> vs. <code>-&gt;</code> for method calls, in different languages, or check out the <code>===</code> operator in Ruby, or <code>=~</code> in Perl, etc.).</li>
  <li>Depending on the knowledge of your students, you could add even more uses of the <code>=</code>, e.g. in modern functional languages like Haskell (definition, pattern matching), or in something like Prolog (where equality is complicated on a whole new level). But I guess that's a bit too much if they're concerned with the basics, still. Maybe an addendum for the over-achievers. ;)</li>
  </ul>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-02T09:41:22.760Z
Answers.Text: +1 for "There is no particular reason to assume that any given symbol used in a programming language needs to have any specific semantics." Just point them to some of the languages used on the codegolf stackexchage.
Answers.UserDisplayName: Sumyrda - remember Monica
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-01T21:56:56.687Z
Answers.LastActivityDate: 2017-07-02T10:16:25.860Z
Answers.LastEditDate: 2017-07-02T10:16:25.860Z
Answers.UserDisplayName: AnoE
Answers.Body: <p>I've not tried the following, so I'm unable to vouch for their effectiveness, but if you're looking for suggestions:</p>
  
  <ul>
  <li>Avoid assigning variables directly (i.e. x = y) </li>
  <li>Hide the implementation in functions that have semantically clear names (e.g. x.Assign(y) ), although this assumes an object-oriented approach. Something like: x = Int.Create(y) uses assignment but the function may make it less likely to be confused with an equality assertion.</li>
  <li>Similarly functions can be used to test for equality in favour of ==</li>
  <li>Use addition assignment: +=, which is behaves as I intuitively expected an assignment to.</li>
  </ul>
  
  <p>Of course the above will only buy you some time before assignment and equality checks need to be explained properly, but it gives the students some time to adapt to programming without being overwhelmed with too many new concepts at once.</p>
  
  <p>Depending on the course it might be appropriate to use languages like J, which uses =:, or f# where the 'let' keyword is used to indicate the = is an assignment (though, possibly confusingly, a single = is also used to test equality).</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-02T05:43:16.200Z
Answers.Text: Very interesting ideas. Welcome to [cseducators.se], and I hope we hear more from you!
Answers.UserDisplayName: Ben I.
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-02T05:40:28.243Z
Answers.LastActivityDate: 2017-07-02T05:43:32.300Z
Answers.LastEditDate: 2017-07-02T05:43:32.300Z
Answers.UserDisplayName: Vhon Newmahn
Answers.Body: <p>When introducing <code>=</code> and <code>==</code> in python, I refrain from reading them loud with a name that contains "equal" (or rather « égale » in French).</p>
  
  <p>Instead, I always read <code>x = y</code> as "x takes the value of y" (« x prend la valeur de y » which happens to be literally what the pseudo-language used in earlier classes mandates) and <code>x == y</code> "x has the same value as y" (« x a même valeur que y »).</p>
  
  <p>And this leaves "x equals y" (« x égale y ») for $x=y$ to my math colleagues, which they usually use.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-25T18:38:50.597Z
Answers.LastActivityDate: 2017-08-25T18:38:50.597Z
Answers.UserDisplayName: ysalmon
Answers.Body: <p>I would generalise this, because other symbols and terminology are also affected (">" or textual statements such as commands).</p>
  
  <p>Start by explaining that symbols and terms in <strong>each</strong> language have meanings specific to that language. They may overlap or have similarities, but never assume that just because a symbol or word has some meaning in one language or scenario, that it has the same meaning in another.</p>
  
  <p><strong>Then use "=" as an example of this general rule</strong>. This <em>changes</em> "=" from "a problem affecting one situation" into "a classic example of a really important rule that applies to all of computer science" and into a memorable learning point. You can then pick a few other examples if the students need it. </p>
  
  <p>This will prepare them for the idea that different languages often <em>also</em> have different philosophies, approaches to, and ways of representing even superficially similar programming structures.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-02T13:46:46.530Z
Answers.Text: Hi Stilez! Welcome to [cseducators.se]! This is an interesting take on the problem, thanks for sharing it.
Answers.UserDisplayName: thesecretmaster
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-02T13:36:28.067Z
Answers.LastActivityDate: 2017-07-02T16:59:30.043Z
Answers.LastEditDate: 2017-07-02T16:59:30.043Z
Answers.UserDisplayName: Stilez
Answers.Body: <p>The way I explain it in my lectures is slightly different from what I have read in the other responses. I think there is one important piece of implicit information missing so far: the difference between imperative and declarative programming.
  Since the difference between the two is hardly ever shown explicitly but is often important to understand what's going on and avoid bugs I focus on that concept early on.</p>
  
  <p>In this case the issue is that the equals sign means something different in declarative code (i.e. the maths statement) and in imperative code (the two python examples).</p>
  
  <p>The mathematical statement "x=x*x" doesn't do anything. It declares a rule that shall be fulfilled by the following calculations, but it doesn't perform any immediate action. It is not fixed to a certain point in time - as soon as you write it down it was practically in existence before anyone performed the first actual calculation.
  The same applies to declarative code in computer programming. When you declare a new class it simply exists whenever a line of code is run. There is no defined point in time when it is created (unlike imperative construction of object prototypes as in Lua or JavaScript).</p>
  
  <p>The two python lines on the other hand are imperative, they perform an action at the time when they are called. I don't know about Python, but in most languages both operators are actually just shortcuts for a "normal" function definition like </p>
  
  <pre><code>bool operator==(a,b)
  </code></pre>
  
  <p>so there is a clearly defined order in which the involved expressions are evaluated and there is a clear task to be performed.</p>
  
  <p>Once you know the difference there is basically no room for ambiguity left.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-05T07:32:28.913Z
Answers.LastActivityDate: 2017-07-05T07:32:28.913Z
Answers.UserDisplayName: Lars Kokemohr
Answers.Body: <p>The programmer's <code>x == x**2</code> is the exact same statement as the mathematics $x=x^2$: it is a statement that can be either True or False. Most people are just rather cavalier in their usage of the mathematics version:  They'll just STATE that $x=x^2$ and then derive $x \in \{0,1\}$, but what they are really doing is writing a proof that if the statement $x=x^2$ is True, then the statement $x \in \{0,1\}$ is also True.  Notably, the statement $x=x^2$ does NOT simply constrain $x \in \{0,1\}$, since one might very well start out a mathematical argument as "Suppose that $x=x^2$ is False...."</p>
  
  <p>The real difference is in the control structures surrounding the statement.  <code>if...then</code> in programming is a control structure, whereas <code>if...then</code> in mathematics is simply another statement that can be either True or False.... i.e. mathematics is generally concerned with the logical implications of $x=x^2$ being either True or False, whereas programmers are generally concerned with the difference of what they will show on a screen when <code>x==x**2</code> or <code>x!=x**2</code>.  </p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-03T15:46:26.203Z
Answers.Text: Welcome to [cseducators.se]!  I'm not so sure I agree with you, though, that *"The programmer's `x == x**2` is the exact same statement as the mathematics $x=x^2$"*.  The first is a test, and the second is an axiomatic constraint.
Answers.UserDisplayName: Ben I.
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-03T16:19:12.777Z
Answers.Text: It's not an axiomatic constraint.  I can very well stipulate in a mathematics context that $x=x^2$ is False.
Answers.UserDisplayName: Him
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-03T16:24:06.867Z
Answers.Text: For instance, suppose one wants to prove that "If $x \in \{0,1\}$ then $x=x^2$", one could set out to prove the contrapositive:  "Suppose that it is not the case that $x=x^2$, then $x^2-x=0$ is False, so $x(x-1)=0$ is False, so $x-1=0$, $x=0$ are False, therefore, $x \in \{0,1\}$ is False."
Answers.UserDisplayName: Him
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-03T16:25:57.467Z
Answers.Text: Disclaimer:  I am a mathematics educator, and when I use the statement $x=x^2$, I very much mean that that is a statement that can either be True or it can be False, depending on the value of $x$.
Answers.UserDisplayName: Him
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-03T15:25:53.857Z
Answers.LastActivityDate: 2017-07-03T15:25:53.857Z
Answers.UserDisplayName: Him
Answers.Body: <p>I actually cover this idea in my very first lesson in AP Computer Science, because I have found that students find the different uses of <code>=</code> to be confusing, indeed.  I run a unit based on Tom Roger's unit, <a href="http://www.intuitor.com/student/Q1_Comp_Sci_Objectives.php" rel="noreferrer">Java Ain't Algebra</a>, and I also talk about it a little <a href="https://cseducators.stackexchange.com/a/345/27">here</a>.</p>
  
  <p>I do this right at the beginning because, if mutability is not made explicit, students do <em>not</em> seem to exhibit a total loss of comprehension or ability right away, but rather, very slowly slip further and further behind.  They will never be able to articulate what they find confusing about the material, because every piece seems to make sense when looked at in isolation.</p>
  
  <p>The confusion itself stems from simply not realizing that there <em>are</em> articulably different uses of equality in the first place.  In my experience, simply pointing it out is actually enough to stem the tide of the confusion.  (You gave a perfectly wonderful explanation in the question itself.)  Then, two or three more times over the first few weeks of class, I will gently remind them that <em>this equals</em> (some code statement we are examining) is a <em>mutating</em> equals, not the <em>constraining</em> equals of mathematics.</p>
  
  <p>I have not had problems from students in this regard since I began this practice.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-01T19:23:57.500Z
Answers.Text: "Java ain't Algebra" is the key point. A computer program (at least in the procedural languages you are likely to teach first in CS) is a description of an algorithm, not a collection of mathematical equations. You won't find a cookery recipe that tells you how to make a pizza just by saying "pizza = dough + toppings." Of course an algorithm (or a recipe) may contain *logical tests* - for example "if (dough == black and smoking) then pizza is overcooked" (!!)
Answers.UserDisplayName: alephzero
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-02T05:01:19.470Z
Answers.Text: Selecting as the "answer" because the link to Roger's lesson is helpful, mutating/constraining are nice terms to introduce, and because I think the claim of "early and repeatedly" is correct and backed by your anecdotal data. I do think it should be studied and submitted to a peer reviewed journal, as more systematically collected results might violate our shared intuition and anecdotal observations. Without real data, it feels odd to pick an "answer," but I wager yours is correct, including the insightful observation that confusion is delayed, seeping, and hard for students to pinpoint.
Answers.UserDisplayName: Bennett Brown
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-03T08:36:26.447Z
Answers.Text: @BennettBrown While teaching it early is essential this is certainly not  a CS problem. The inability to grasp the different uses of "=" even just within mathematics is a huge problem that students struggle with. There are actually quite a few papers written on this (https://www.ncbi.nlm.nih.gov/pmc/articles/PMC3374577/ https://www.jstor.org/stable/30034852 )
Answers.UserDisplayName: DRF
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-05T14:40:30.990Z
Answers.Text: Are you really using "mutating equals" when speaking about "assignment" (or binding in less state loving languages)?? Please don't. I don't have much teaching experience, but I can draw conclusions from the mass of homework questions that get posted on SO: a good amount of them are only due to students not knowing (and thus not searching) for the correct terms.
Answers.UserDisplayName: Daniel Jour
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-05T14:43:30.763Z
Answers.Text: That's a good point. :) You may be glad to hear that no, I don't use that term regularly. I might use it (or something similar) one time to draw the distinction we are discussing here, but I do go on to then explain the proper term. :)
Answers.UserDisplayName: Ben I.
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-01-22T06:03:25.410Z
Answers.Text: That is why BASIC used the LET operator, and PASCAL used := (read becomes equal) for assignment.  In some ways it's too bad that brevity has won out over clarity in modern languages.
Answers.UserDisplayName: pojo-guy
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-01T18:55:27.690Z
Answers.LastActivityDate: 2017-07-01T18:55:27.690Z
Answers.UserDisplayName: Ben I.
Answers.Body: <p>If your students are coming from a math background, teach them that in most programming languages, all variables have a hidden subscript on them corresponding to a time/step-number (i.e. they're functions of time), and something like:</p>
  
  <blockquote>
    <p>$x = x^2$</p>
  </blockquote>
  
  <p>really means something like:</p>
  
  <blockquote>
    <p>[Let] $x_{t+1} = x_t^2$</p>
  </blockquote>
  
  <p>You might clarify that such a rule for $x_{t+1}$ only applies for values of $t$ where the "current position" (another hidden variable; make an analogy with a "program counter" register if you like) in the program is at that statement, and that, implicitly in the absence of any assignment to a variable $v$, $v_{t+1} = v_t$.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-02T00:46:44.190Z
Answers.Text: That's a very good point (and a nice first post in our community). Welcome to [cseducators.se]!
Answers.UserDisplayName: Ben I.
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-02T00:42:07.130Z
Answers.LastActivityDate: 2017-07-02T00:51:04.900Z
Answers.LastEditDate: 2017-07-02T00:51:04.900Z
Answers.UserDisplayName: R.. GitHub STOP HELPING ICE
Answers.Body: <p>When I have taught this, I have said that <code>=</code> in programming is just a different thing to <code>=</code> in maths. It's unfortunate that someone (I say "some idiot", you might not want to use such language) decided to re-use <code>=</code> for assignment in programming, but now we are stuck with it.</p>
  
  <p>Programming, while it looks like maths, is more like a series of instructions. <code>x=5</code> means the action "Give the variable <code>x</code> the value <code>5</code>".</p>
  
  <p><code>==</code> is more like maths, in that it <code>x==5</code> checks if <code>x</code> is <code>5</code>. However, it is still an instruction, it means "Tell me if x equals 5", while usually in maths <code>x=5</code> asserts that <code>x</code> is equal <code>5</code>, rather than checks if it is true.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-01T19:32:55.583Z
Answers.Text: Always glad to see more teachers here. Welcome to [cseducators.se]! I hope we hear more from you in the future.
Answers.UserDisplayName: Ben I.
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-01T23:08:12.347Z
Answers.Text: I mean, what is wrong with `:=` ?????
Answers.UserDisplayName: Mindwin Remember Monica
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-02T16:27:25.367Z
Answers.Text: Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackexchange.com/rooms/61444/discussion-on-answer-by-chris-jefferson-teaching-math-equality-vs-cs-assignm).
Answers.UserDisplayName: thesecretmaster
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-03T12:57:16.353Z
Answers.Text: I think `x=5` as assignment could be read as "_`x` equals `5` from now on_". Thus it's actually _equals_, but the demanding version of it, not constraining one as in math..
Answers.UserDisplayName: Ruslan
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-04T08:07:57.287Z
Answers.Text: "It's unfortunate that someone decided to re-use `=` for assignment in programming, but now we are stuck with it." – Not really. `=` is only used for assignment in *some* languages, not all of them. In fact, I wouldn't be surprised to find that these languages are a minority. Most Lisps use an alphanumeric identifier for assignment (e.g. `def` or `set`), Smalltalk originally used `←` and then switched to `:=` only because `←` was not available in the character sets of the early 1980s, Algol, Jovial, Simula, Pascal, Modula, Oberon, and many others, Beta uses `->` and puts the target at the …
Answers.UserDisplayName: Jörg W Mittag
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-04T08:11:16.793Z
Answers.Text: … right, gBeta uses `|` and puts the target at the right, Prolog and Erlang don't *have* assignment in that sense (they use `=` for pattern unification, which is actually somewhat similar to one of the usages in maths), purely functional languages like Haskell, Clean, Frege, Idris, Epigram, Coq, Guru, etc don't have this notion of assignment. If you don't like the use of `=` for assignment, then just don't use a language that does that. Especially in a CSE setting where the language doesn't matter anyhow, and it's all about the concepts.
Answers.UserDisplayName: Jörg W Mittag
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-06T12:47:47.063Z
Answers.Text: While many languages don't use = for assignment, almost every popular language does, if you look at any table of most popular languages.
Answers.UserDisplayName: Chris Jefferson
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-01T19:28:36.920Z
Answers.LastActivityDate: 2017-07-01T19:28:36.920Z
Answers.UserDisplayName: Chris Jefferson
Body: <p>Consider the following three uses of equality:</p>
  
  <ul>
  <li>In mathematics, the statement $x = x^2$</li>
  <li>In Python, the statement <code>x = x**2</code></li>
  <li>In Python, the expression, <code>x == x**2</code></li>
  </ul>
  
  <p>The math statement is a constraining assertion restricting x to the set $\{0, 1\}$; the Python assignment always evalutes the right side and assigns to the left side; the Python expression always returns True or False. </p>
  
  <p>When and how should the differences be pointed out to students? Are there examples producing unexpected results that are especially effective in causing students to abandon the idea that these three meanings of equality are equivalent?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-07-02T01:14:47.897Z
Comments.Text: They aren't three meanings of "equality".  They are three meanings of the _symbol_ `=`.  Many symbols are used for different meanings, especially in different contexts; this is one of them.  For example, `joe@yahoo.com` and `12 @ $1.25` mean different things though both use the symbol `@`.  The only problem I can see is if an instructor _started_ the misinterpretation by saying that something like `R = A + B` is an _equation_ or maybe "this means R _is equal to_  A + B" and then later corrected that to explain about variables and assignment.
Comments.UserDisplayName: davidbak
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-07-02T12:26:28.983Z
Comments.Text: It doesn't directly answer your question so I'm putting this as a comment. Read the assignment operator as "becomes equal to". In some programming languages, the := operator is used to disambiguate the symbolism.
Comments.UserDisplayName: pojo-guy
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-07-02T21:17:46.310Z
Comments.Text: By the way the *"The math statement is a constraining assertion restricting x to the set {0,1}"* is not accurate. It is correct if we know that x belongs to the set of Integers or the Reals or the Complex numbers. But there are other sets (with a definition of multiplication) where the equation `x=x*x` has different (less or more) solutions.
Comments.UserDisplayName: ypercubeᵀᴹ
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-07-02T22:01:59.930Z
Comments.Text: In the R statistical programming language, it's typical to use `x <- 5` for assignment, rather than `y = 5`; and to use `x == y` for testing equality.
Comments.UserDisplayName: Hatshepsut
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-07-02T22:51:20.493Z
Comments.Text: In mathematics, $x=x^2$ is a statement which can be true or false, depending on $x$, quite similarly to your second Python example.
Comments.UserDisplayName: Carsten S
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-07-03T06:56:47.720Z
Comments.Text: There is also the `=` of Prolog for unification, for even more confusion!
Comments.UserDisplayName: Fatalize
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-07-03T14:19:15.607Z
Comments.Text: @Fatalize prolog ... /shudder
Comments.UserDisplayName: pojo-guy
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-07-03T18:53:23.347Z
Comments.Text: From the point of view of a mathematics teacher, let me say that the equal sign in mathematics has several, context-dependent meanings. Some students do not appreciate the distinctions. Often they only get them only vaguely, somewhat like the way you know the idiom of your native language.  But this may be a side issue to teaching programming, but it's not like students are equipped by their mathematical backgrounds with a clear understanding of the equal sign. They're inclined to think it's always the same, which might be a source of the problem here.
Comments.UserDisplayName: Michael E2
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-07-03T19:03:21.360Z
Comments.Text: @Michael E2 https://scholar.google.com/scholar?q=math+ed+equal+sign wow.
Comments.UserDisplayName: Bennett Brown
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-07-04T18:58:48.610Z
Comments.Text: Note that maths has 2 uses: `x^2 = y` and also `let y = 3, then there are 2 solutions` / `but setting x = 2 we get y = 4`
Comments.UserDisplayName: Stilez
Tags: <mathematics><variables>
Title: How do I teach "math equality" vs. "CS assignment" vs. "CS equality logic operator"?
ThreadText: # How do I teach "math equality" vs. "CS assignment" vs. "CS equality logic operator"?
  
  (Asked by: username_0 on 2017-07-01)
  
  Consider the following three uses of equality:
  
  * In mathematics, the statement $x = x^2$
  * In Python, the statement `x = x**2`
  * In Python, the expression, `x == x**2`
  
  The math statement is a constraining assertion restricting x to the set $\{0, 1\}$; the Python assignment always evalutes the right side and assigns to the left side; the Python expression always returns True or False.
  
  When and how should the differences be pointed out to students? Are there examples producing unexpected results that are especially effective in causing students to abandon the idea that these three meanings of equality are equivalent?
  
  **Tags:** mathematics, variables
  
  > **Comments:**
  > **username_1**: They aren't three meanings of "equality".  They are three meanings of the _symbol_ `=`.  Many symbols are used for different meanings, especially in different contexts; this is one of them.  For example, `joe@yahoo.com` and `12 @ $1.25` mean different things though both use the symbol `@`.  The only problem I can see is if an instructor _started_ the misinterpretation by saying that something like `R = A + B` is an _equation_ or maybe "this means R _is equal to_  A + B" and then later corrected that to explain about variables and assignment.  
  > **username_2**: It doesn't directly answer your question so I'm putting this as a comment. Read the assignment operator as "becomes equal to". In some programming languages, the := operator is used to disambiguate the symbolism.  
  > **username_3**: By the way the *"The math statement is a constraining assertion restricting x to the set {0,1}"* is not accurate. It is correct if we know that x belongs to the set of Integers or the Reals or the Complex numbers. But there are other sets (with a definition of multiplication) where the equation `x=x*x` has different (less or more) solutions.  
  > **username_4**: In the R statistical programming language, it's typical to use `x <- 5` for assignment, rather than `y = 5`; and to use `x == y` for testing equality.  
  > **username_5**: In mathematics, $x=x^2$ is a statement which can be true or false, depending on $x$, quite similarly to your second Python example.  
  > **username_6**: There is also the `=` of Prolog for unification, for even more confusion!  
  > **username_2**: @username_6 prolog ... /shudder  
  > **username_7**: From the point of view of a mathematics teacher, let me say that the equal sign in mathematics has several, context-dependent meanings. Some students do not appreciate the distinctions. Often they only get them only vaguely, somewhat like the way you know the idiom of your native language.  But this may be a side issue to teaching programming, but it's not like students are equipped by their mathematical backgrounds with a clear understanding of the equal sign. They're inclined to think it's always the same, which might be a source of the problem here.  
  > **username_0**: @username_7 https://scholar.google.com/scholar?q=math+ed+equal+sign wow.  
  > **username_8**: Note that maths has 2 uses: `x^2 = y` and also `let y = 3, then there are 2 solutions` / `but setting x = 2 we get y = 4`  
  
  ---
  
  ## 12 answers
  
  ### Answer by username_11 on 2017-07-01. Score: 21 | Accepted answer
  
  I actually cover this idea in my very first lesson in AP Computer Science, because I have found that students find the different uses of `=` to be confusing, indeed. I run a unit based on Tom Roger's unit, [Java Ain't Algebra](http://www.intuitor.com/student/Q1_Comp_Sci_Objectives.php), and I also talk about it a little [here](https://cseducators.stackexchange.com/a/345/27).
  
  I do this right at the beginning because, if mutability is not made explicit, students do *not* seem to exhibit a total loss of comprehension or ability right away, but rather, very slowly slip further and further behind. They will never be able to articulate what they find confusing about the material, because every piece seems to make sense when looked at in isolation.
  
  The confusion itself stems from simply not realizing that there *are* articulably different uses of equality in the first place. In my experience, simply pointing it out is actually enough to stem the tide of the confusion. (You gave a perfectly wonderful explanation in the question itself.) Then, two or three more times over the first few weeks of class, I will gently remind them that *this equals* (some code statement we are examining) is a *mutating* equals, not the *constraining* equals of mathematics.
  
  I have not had problems from students in this regard since I began this practice.
  
  > **Comments:**
  > **username_24**: "Java ain't Algebra" is the key point. A computer program (at least in the procedural languages you are likely to teach first in CS) is a description of an algorithm, not a collection of mathematical equations. You won't find a cookery recipe that tells you how to make a pizza just by saying "pizza = dough + toppings." Of course an algorithm (or a recipe) may contain *logical tests* - for example "if (dough == black and smoking) then pizza is overcooked" (!!)  
  > **username_0**: Selecting as the "answer" because the link to Roger's lesson is helpful, mutating/constraining are nice terms to introduce, and because I think the claim of "early and repeatedly" is correct and backed by your anecdotal data. I do think it should be studied and submitted to a peer reviewed journal, as more systematically collected results might violate our shared intuition and anecdotal observations. Without real data, it feels odd to pick an "answer," but I wager yours is correct, including the insightful observation that confusion is delayed, seeping, and hard for students to pinpoint.  
  > **username_25**: @username_0 While teaching it early is essential this is certainly not  a CS problem. The inability to grasp the different uses of "=" even just within mathematics is a huge problem that students struggle with. There are actually quite a few papers written on this (https://www.ncbi.nlm.nih.gov/pmc/articles/PMC3374577/ https://www.jstor.org/stable/30034852 )  
  > **username_26**: Are you really using "mutating equals" when speaking about "assignment" (or binding in less state loving languages)?? Please don't. I don't have much teaching experience, but I can draw conclusions from the mass of homework questions that get posted on SO: a good amount of them are only due to students not knowing (and thus not searching) for the correct terms.  
  > **username_11**: That's a good point. :) You may be glad to hear that no, I don't use that term regularly. I might use it (or something similar) one time to draw the distinction we are discussing here, but I do go on to then explain the proper term. :)  
  > **username_2**: That is why BASIC used the LET operator, and PASCAL used := (read becomes equal) for assignment.  In some ways it's too bad that brevity has won out over clarity in modern languages.  
  
  ---
  
  ### Answer by username_28 on 2017-07-01. Score: 16
  
  When I have taught this, I have said that `=` in programming is just a different thing to `=` in maths. It's unfortunate that someone (I say "some idiot", you might not want to use such language) decided to re-use `=` for assignment in programming, but now we are stuck with it.
  
  Programming, while it looks like maths, is more like a series of instructions. `x=5` means the action "Give the variable `x` the value `5`".
  
  `==` is more like maths, in that it `x==5` checks if `x` is `5`. However, it is still an instruction, it means "Tell me if x equals 5", while usually in maths `x=5` asserts that `x` is equal `5`, rather than checks if it is true.
  
  > **Comments:**
  > **username_11**: Always glad to see more teachers here. Welcome to [cseducators.se]! I hope we hear more from you in the future.  
  > **username_29**: I mean, what is wrong with `:=` ?????  
  > **username_21**: Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackexchange.com/rooms/61444/discussion-on-answer-by-username_28-username_28-teaching-math-equality-vs-cs-assignm).  
  > **username_30**: I think `x=5` as assignment could be read as "_`x` equals `5` from now on_". Thus it's actually _equals_, but the demanding version of it, not constraining one as in math..  
  > **username_31**: "It's unfortunate that someone decided to re-use `=` for assignment in programming, but now we are stuck with it." – Not really. `=` is only used for assignment in *some* languages, not all of them. In fact, I wouldn't be surprised to find that these languages are a minority. Most Lisps use an alphanumeric identifier for assignment (e.g. `def` or `set`), Smalltalk originally used `←` and then switched to `:=` only because `←` was not available in the character sets of the early 1980s, Algol, Jovial, Simula, Pascal, Modula, Oberon, and many others, Beta uses `->` and puts the target at the …  
  > **username_31**: … right, gBeta uses `|` and puts the target at the right, Prolog and Erlang don't *have* assignment in that sense (they use `=` for pattern unification, which is actually somewhat similar to one of the usages in maths), purely functional languages like Haskell, Clean, Frege, Idris, Epigram, Coq, Guru, etc don't have this notion of assignment. If you don't like the use of `=` for assignment, then just don't use a language that does that. Especially in a CSE setting where the language doesn't matter anyhow, and it's all about the concepts.  
  > **username_28**: While many languages don't use = for assignment, almost every popular language does, if you look at any table of most popular languages.  
  
  ---
  
  ### Answer by username_27 on 2017-07-02. Score: 12
  
  If your students are coming from a math background, teach them that in most programming languages, all variables have a hidden subscript on them corresponding to a time/step-number (i.e. they're functions of time), and something like:
  
  > $x = x^2$
  
  really means something like:
  
  > [Let] $x\_{t+1} = x\_t^2$
  
  You might clarify that such a rule for $x\_{t+1}$ only applies for values of $t$ where the "current position" (another hidden variable; make an analogy with a "program counter" register if you like) in the program is at that statement, and that, implicitly in the absence of any assignment to a variable $v$, $v\_{t+1} = v\_t$.
  
  > **Comments:**
  > **username_11**: That's a very good point (and a nice first post in our community). Welcome to [cseducators.se]!  
  
  ---
  
  ### Answer by username_14 on 2017-07-01. Score: 8
  
  I'm going to explore an orthogonal concept that sheds light on the question at hand: What is a variable?
  
  In mathematics a variable is a representative for some value, a name. It may be a name for something we know or don't (abstraction comes in here). In an equation, the variable is sometimes called an "unknown". But that isn't precisely true. An equation is a constraint on which values are appropriate for reference. That is because our typical use of equations is to find solutions (i.e. the set of values for which the constraint is true). The value is only "unknown" if we haven't (or can't) solve the equation. But, in math, a variable is misnamed in that (again, normally - there can be exceptions) the value doesn't "vary". So the math terminology is an historic accident to some extent, not absolute "truth."
  
  In computing, on the other hand, a variable refers to a value (also), but the value referred to CAN change over time (or with context). So in computing a variable varies.
  
  In math a variable refers to something (or a set of somethings) that doesn't change.
  
  In computing a variable can refer to different things as a computation proceeds.
  
  So, in maths = represents a relationship. But in Java = (Pascal :=, somewhere else <-, a favorite of mine to see, but harder to type on most keyboards) represents an action: changing a reference.
  
  And in java == is an imperfect representation of maths =. It is a relationship (has value true or false). (imperfect since, while 1 == 0.2 + 0.2 + 0.2 + 0.2 + 0.2 is true, 2 == 0.2 + 0.2 + 0.2 + 0.2 + 0.2 + 0.2 + 0.2 + 0.2 + 0.2 + 0.2 is false)
  
  Part of the confusion of some beginning students who "know" some math and are learning computing is that they don't understand the math well enough. So giving them a solid understanding of what $x^2 - 2x + 1 = 0$ means to a mathematician may help.
  
  I think that Ritchie and Thompson just hated Pascal's := (too many keystrokes) and so wanted to simplify it when they created C. But, in their defense, they may have been programming on a card punch where only 80 characters were permitted per card and mistakes were costly (repunch the entire card). Typing one extra character slowed down the programmer too much (unless you were lucky enough to employ a keypunch operator). The issue of thinking speed v typing speed is lessened now, but it doesn't go away. However, it is still true that typing is not what makes programming hard.
  
  It is easy (in java) to illustrate the difference between = and ==. Suppose x and y are boolean variables. Then if(x==y), and if(x=y) are both legal, but very different in effect (unless they are not, of course). Fortunately some modern development environments issue a warning when you try something legal but probably incorrect.
  
  But for the record, there are situations in java where you do need to make an assignment in the "test" of an if or while; doing I/O, for example, where certain conventions require it.
  
  ```
  String line = null; 
  while ((line = reader.readLine()) != null)
  
  ```
  
  > **Comments:**
  > **username_15**: Re "a variable is a representative for some value" - so is a constant, which is the opposite of a variable.  
  > **username_14**: Yes, of course, @username_15, but as the discussion was about variables, I didn't give a fuller exposition. In some ways a constant is more like a math variable than a variable is, though that might just confuse the situation for students rather than clarify it.  
  > **username_0**: I think the difference between a variable in algebra and a variable in CS deserves its own question and crows sourcing of ideas about how to clarify the difference for students.  
  > **username_16**: I actually can't think of any common languages where `3 == 3.0` would evaluate to false. Not that I know every common language, but it can't be all _that_ common, can it?.  
  > **username_14**: Hmmm. @username_16. Sorry, just me being too literal minded. I changed the example. Hopefully correct now.  
  > **username_16**: @username_14 Sure, I'll take your word for this one. :-) (The point is clearer now anyway)  
  > **username_2**: Mistaking rounding error for an issue with the intent of the equivalence operator could cause more confusion.  The == means the same as the algebraic =, but the unequaled being reported is a function of rounding error inherent in representing decimal fractions in a binary forum. Like fractions of 9 in base 10, many fractions of 10 are infinitely repeating series in base 2  
  
  ---
  
  ### Answer by username_17 on 2017-07-01. Score: 7
  
  > When and how should the differences be pointed out to students?
  
  When: As soon as you introduce one of those operators...
  
  On the "how" - I'd try to not beat around the bush, so to speak, but to point out straight away that only because we use the same symbol, they still have *completely* different meanings.
  
  > Are there examples producing unexpected results that are especially effective in causing students to abandon the idea that these three meanings of equality are equivalent?
  
  I'd say it is important to be very clear about the fact that these are *not* "three equalities".
  
  The `=` in these three statements are completely different (e.g.: mathematical constraint <=> assignment operation <=> equality expression) and have no particular relationship with each other except for using the same symbol and some vague, indirect and maybe confusing similarities.
  
  That is a great time to introduce related concepts:
  
  * Operations and expressions are quite different in languages, especially imperative ones. Python is one of the few languages I never touched, but this would be very clear and easy to show in Java, C, Pascal (where expressions and operations are very much separated). A different imperative example would be Ruby, where almost everything is an expression, even "if ... then ... else". Or, in the extreme, you have LISP, where only very, very few things indeed exist which are not expressions. Obviously, you'd have to take whatever languages your pupils know, for that...
  * There is no particular reason to assume that any given symbol used in a programming language needs to have any specific semantics, nor, that every semantic has the same symbol in every language. The designers of the language can more or less do whatever they want (for example `.` vs. `->` for method calls, in different languages, or check out the `===` operator in Ruby, or `=~` in Perl, etc.).
  * Depending on the knowledge of your students, you could add even more uses of the `=`, e.g. in modern functional languages like Haskell (definition, pattern matching), or in something like Prolog (where equality is complicated on a whole new level). But I guess that's a bit too much if they're concerned with the basics, still. Maybe an addendum for the over-achievers. ;)
  
  > **Comments:**
  > **username_18**: +1 for "There is no particular reason to assume that any given symbol used in a programming language needs to have any specific semantics." Just point them to some of the languages used on the codegolf stackexchage.  
  
  ---
  
  ### Answer by username_9 on 2017-07-03. Score: 6
  
  I don't teach CS in any official capacity but I have tutored a number of friends and colleagues on the basics. I find that differentiating the language I use when describing the different concepts helps a lot, for instance:
  
  * Mathematical `x=x*x` = "X equals X squared"
  * Assignment `x=x*x` = "X is made equal to X squared"
  * Comparison `x==x*x`= "Test if X is equal to X squared"
  
  Using the above terms when talking through code I have never been asked to explain the difference between assignment and comparison. YMMV
  
  I think this stemmed from the first language I was taught academically was pascal where the assignment operator is `:=` and comparison is `=`, having that difference caused me to internally narrate my code in this way, which has helped my understanding and my vocabulary when explaining to others.
  
  > **Comments:**
  > **username_10**: YMMV? what does that mean?  
  > **username_11**: Welcome to [cseducators.se]!  
  > **username_9**: @username_10 Your Mileage May Vary, meant as an acknowledgement that my experience may differ from someone else applying this technique.  
  
  ---
  
  ### Answer by username_12 on 2017-07-01. Score: 6
  
  All of the below is meant to be thoughts/analogies/illustrations to help explain the difference (meant to be said to students) but in reality, this is all dependent on the level of your students.
  
  ---
  
  The expression `x == x**2` in python is a "test" - is it true, or is it not? Much like when defining a set, you have $\{x|x>2\}$ (i.e., x is only included *iff* $x>2$) - a test to see if something should be included in the set, whereas in python, it is often used to check if something should be done. In this line of reasoning, you could write a program to define a set, expanding on what exactly the mathematical notation means:
  
  ```
  if x > 2:
      list.append(x)
  
  ```
  
  In other words, `if x > 2 is True` then `do list.append(x)`.
  
  On the other hand, the expression `x = 5` in python is an "assignment" - this is equal to this, no need for argument. \*This is much like in math assigning $x = 5$ or something of the sort. We can later use `x` to represent 5, much like in an equation $x+5$, given the above definition, we know that the result is $10$, because we are using $x$ to represent $5$. A common analogy is that of the buckets - a bucket has `x` painted on it and whatever is in the bucket is what `x` represents.
  
  However, things get more complicated from there - what if `x = x**2`? Then, you take what `x` was previously defined as, square it, and then *redefine* `x` as equal to the new thing. For example, let's "run" the following program:
  
  ```
  x = 5
  print(x)
  x = x**2
  print(x)
  x = x**2
  print(x)
  
  ```
  
  What will we see? Well, it is quite obvious that the first thing printed is "5"; I doubt there will be much confusion there. Now, the next line is squaring the previous `x`, i.e., squaring 5 ($5^2$) and then setting that equal to `x`, so now we basically have the line of code `x = 5**2` - which produces 25. All very well and good, now we do the same thing - so we basically have `x = 25**2` or 625.
  
  The difference between those two and what they mean is a very useful and important distinction, but to be honest I think it's not completely unintuitive. Once explained the former is a test and the latter a definition, I think students will catch on fairly quickly with a reminder here and there.
  
  Now we come to your last (well, first) type of equality. $x = x^2$ is a statement of definition, but it can also be manipulated. In python `x = x**2` is a definition, and it accesses the *previous definition* of `x` to "solve" it, like we did above. However, to solve $x = x^2$, we move terms around the equals sign - this is a quadratic, so we get $0 = x^2 - x$ and we can factor out the $x$ so $0 = x(x - 1)$ so using the zero product property one gets $x=0$ or $x=1$. We can solve equations like this in Python, but that's not automatically done when you define `x = x**2`, instead it uses previous definitions.
  
  I hope that conveys what you are wanting, but I really don't think you need to bring up your first definition of equality - the rules of that have been drummed into them from math class, and the new rules of equality appear distinctly enough that I doubt they will be confused.
  
  Now, all of the above is with a nice, good language like Python. *Some* other languages have `=`, `==`, and `===`. I'm not qualified to get into that, but this might not be valid for such craziness.
  
  ---
  
  As for *when* this all should be pointed out - the uses for these concepts are so fundamental, this really is almost day 1 (maybe day 2) of class - install/open the IDE/whatever, learn how to print, then start with variables. This is one of the most important things to learn in a language like python - how to use variables and manipulate them to your advantage.
  
  > **Comments:**
  > **username_13**: I get what you are trying to say in each sentence but the use of language in this post is so sloppy/fuzzy that it is the sort of writing/presentation that is why students get confused.  
  
  ---
  
  ### Answer by username_19 on 2017-07-02. Score: 4
  
  I've not tried the following, so I'm unable to vouch for their effectiveness, but if you're looking for suggestions:
  
  * Avoid assigning variables directly (i.e. x = y)
  * Hide the implementation in functions that have semantically clear names (e.g. x.Assign(y) ), although this assumes an object-oriented approach. Something like: x = Int.Create(y) uses assignment but the function may make it less likely to be confused with an equality assertion.
  * Similarly functions can be used to test for equality in favour of ==
  * Use addition assignment: +=, which is behaves as I intuitively expected an assignment to.
  
  Of course the above will only buy you some time before assignment and equality checks need to be explained properly, but it gives the students some time to adapt to programming without being overwhelmed with too many new concepts at once.
  
  Depending on the course it might be appropriate to use languages like J, which uses =:, or f# where the 'let' keyword is used to indicate the = is an assignment (though, possibly confusingly, a single = is also used to test equality).
  
  > **Comments:**
  > **username_11**: Very interesting ideas. Welcome to [cseducators.se], and I hope we hear more from you!  
  
  ---
  
  ### Answer by username_8 on 2017-07-02. Score: 4
  
  I would generalise this, because other symbols and terminology are also affected (">" or textual statements such as commands).
  
  Start by explaining that symbols and terms in **each** language have meanings specific to that language. They may overlap or have similarities, but never assume that just because a symbol or word has some meaning in one language or scenario, that it has the same meaning in another.
  
  **Then use "=" as an example of this general rule**. This *changes* "=" from "a problem affecting one situation" into "a classic example of a really important rule that applies to all of computer science" and into a memorable learning point. You can then pick a few other examples if the students need it.
  
  This will prepare them for the idea that different languages often *also* have different philosophies, approaches to, and ways of representing even superficially similar programming structures.
  
  > **Comments:**
  > **username_21**: Hi username_8! Welcome to [cseducators.se]! This is an interesting take on the problem, thanks for sharing it.  
  
  ---
  
  ### Answer by username_20 on 2017-08-25. Score: 3
  
  When introducing `=` and `==` in python, I refrain from reading them loud with a name that contains "equal" (or rather « égale » in French).
  
  Instead, I always read `x = y` as "x takes the value of y" (« x prend la valeur de y » which happens to be literally what the pseudo-language used in earlier classes mandates) and `x == y` "x has the same value as y" (« x a même valeur que y »).
  
  And this leaves "x equals y" (« x égale y ») for $x=y$ to my math colleagues, which they usually use.
  
  
  ---
  
  ### Answer by username_22 on 2017-07-05. Score: 2
  
  The way I explain it in my lectures is slightly different from what I have read in the other responses. I think there is one important piece of implicit information missing so far: the difference between imperative and declarative programming.
  Since the difference between the two is hardly ever shown explicitly but is often important to understand what's going on and avoid bugs I focus on that concept early on.
  
  In this case the issue is that the equals sign means something different in declarative code (i.e. the maths statement) and in imperative code (the two python examples).
  
  The mathematical statement "x=x\*x" doesn't do anything. It declares a rule that shall be fulfilled by the following calculations, but it doesn't perform any immediate action. It is not fixed to a certain point in time - as soon as you write it down it was practically in existence before anyone performed the first actual calculation.
  The same applies to declarative code in computer programming. When you declare a new class it simply exists whenever a line of code is run. There is no defined point in time when it is created (unlike imperative construction of object prototypes as in Lua or JavaScript).
  
  The two python lines on the other hand are imperative, they perform an action at the time when they are called. I don't know about Python, but in most languages both operators are actually just shortcuts for a "normal" function definition like
  
  ```
  bool operator==(a,b)
  
  ```
  
  so there is a clearly defined order in which the involved expressions are evaluated and there is a clear task to be performed.
  
  Once you know the difference there is basically no room for ambiguity left.
  
  
  ---
  
  ### Answer by username_23 on 2017-07-03. Score: 1
  
  The programmer's `x == x**2` is the exact same statement as the mathematics $x=x^2$: it is a statement that can be either True or False. Most people are just rather cavalier in their usage of the mathematics version: They'll just STATE that $x=x^2$ and then derive $x \in \{0,1\}$, but what they are really doing is writing a proof that if the statement $x=x^2$ is True, then the statement $x \in \{0,1\}$ is also True. Notably, the statement $x=x^2$ does NOT simply constrain $x \in \{0,1\}$, since one might very well start out a mathematical argument as "Suppose that $x=x^2$ is False...."
  
  The real difference is in the control structures surrounding the statement. `if...then` in programming is a control structure, whereas `if...then` in mathematics is simply another statement that can be either True or False.... i.e. mathematics is generally concerned with the logical implications of $x=x^2$ being either True or False, whereas programmers are generally concerned with the difference of what they will show on a screen when `x==x**2` or `x!=x**2`.
  
  > **Comments:**
  > **username_11**: Welcome to [cseducators.se]!  I'm not so sure I agree with you, though, that *"The programmer's `x == x**2` is the exact same statement as the mathematics $x=x^2$"*.  The first is a test, and the second is an axiomatic constraint.  
  > **username_23**: It's not an axiomatic constraint.  I can very well stipulate in a mathematics context that $x=x^2$ is False.  
  > **username_23**: For instance, suppose one wants to prove that "If $x \in \{0,1\}$ then $x=x^2$", one could set out to prove the contrapositive:  "Suppose that it is not the case that $x=x^2$, then $x^2-x=0$ is False, so $x(x-1)=0$ is False, so $x-1=0$, $x=0$ are False, therefore, $x \in \{0,1\}$ is False."  
  > **username_23**: Disclaimer:  I am a mathematics educator, and when I use the statement $x=x^2$, I very much mean that that is a statement that can either be True or it can be False, depending on the value of $x$.  
  
  ---
  
  