Send Close Add comments: (status displays here)
Got it!  This site "robinsnyder.com" uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.  Note: This appears on each machine/browser from which this site is accessed.
Python: Assignment statements
by RS  admin@robinsnyder.com : 1024 x 640


1. Simple IPO model
input process outputA computer takes input, processes the input, and produces output. Here we look at processing in terms of assignment statements.

2. Assignment statement
An assignment is a task that must be done.


3. Assignment statements
An assignment statement is used to move data within memory.

The assignment statement has the following form. An assignment statement has a variable on the LHS (Left Hand Side), an expression on the RHS (Right Hand Side), and is executed in two steps. This operation is known as a destructive update as the value is gone forever. The assignment statement has the following form. The assignment statement can be read in any of the following ways.

4. Equality
You should not say (or think) the following. Consider the following assignment statement.
x = x + 1

The value of x can never equal x+1 so saying "equals" does not make sense.

The operator symbol for "equals" is "==", not "=".

5. Statements and expressions
What is the difference between and

The former is an assignment statement that assigns variable x the value of x+1. The side effect is that the value of x is changed. The latter is a relational expression that evaluates to false (x can never be equal to x+1).

A crucial distinction is that


6. Destructive update
Warning: A variable can only have one value at any one time. Updating the value destroys the previous value. This destructive update of a memory storage location is called a side-effect.

A destructive update is sometimes called a mutation. A changed variable is said to be mutated.

Note that the LHS must be a variable, while the RHS is an expression. A variable is an expression, but an expression is not a variable. For now, the LHS and RHS should be of the same type.

Explain the destructive update principle in terms of an assignment statement.

7. Valid assignment statements
The following are valid assignment statements that initialize the variables on the LHS to the literal values on the RHS (using the previous declarations).
i = 2 x = 3.5 c = 'A'


8. Invalid assignment statements
Here are some invalid assignment statements.
2 = i 3.5 = x 'A' = c


9. Shortcuts
Here are some of the many shortcut ways that Lua has to write assignment statements.
x += y # is the same as x = x + y x-= y # is the same as x = x - y x *= y # is the same as x = x * y


10. Increment and decrement
An increment is the adding of 1 to a value.

A decrement is the subtraction of 1 from a value.

Here are some of the many shortcut ways that Lua has to write increment and decrement statements.
x += 1 # is the same as x = x + 1 x -= 1 # is the same as x = x - 1

Note that the "++" and "--" operators, when used in expressions, have a more complicated semantics that we will ignore for now. Explain the primary differences between a statement and an expression. Give a specific example of each.

11. Changing computer memory
There are only two ways in which computer memory can be changed. Of course, there are variations of input statements and assignment statements. What are the two types of statements that can change the contents of memory at run-time? Give a specific example of each.


12. Assignment
The assignment statement evaluates the expression on the right side of the assignment operator and places that value into the memory location specified by the variable on the left part of the assignment statement. Whatever value was there before is gone forever.

13. Conditional
Consider the following problem.

The hours worked (for a week) is in variable hoursWorked.

The hourly rate is in variable hourlyRate.

How would you compute the amount paid for the week and put in into variable weeklyPay?

14. One way
Here is just one way to calculate the weeklyPay.
weeklyPay = hoursWorked * hourlyRate

Note that hoursWorked is changed using a destructive update by an assignment statement.

15. End of page

by RS  admin@robinsnyder.com : 1024 x 640