Variables and assignment
Links
- Program variety.py
- Code walkthrough snapshots
- Video walkthrough
Introduction
- There are many forms of abstraction. The one we are concerned with now is the use of symbols to represent values.
- Often when digital problem solving (think programming) we need to remember a value for future usage. The value might be something acquired (gotten) from the user of the program or it might be the result of a computation.
- To be remembered, a value must be stored in the memory of the computer running the program.
- Most programming languages support the remembering of values in computing memory with variables and assignment statements.
- A variable is a symbolic name for some computer memory being reserved by the the program to remember a particular value. In Python, a variable name is always an identifier. As you should recall, an identifier starts with an alphabetic character and can then be followed by one or more alphanumeric (alphabetic or numeric) characters.
- An assignment statement consists of two parts, separated by a single
=
. We call the=
operator, the assignment operator. The things on either side of the=
are called operands.
- The first use of a variable must be as the left operand of an assignment statement.
- When an assignment assignment is encountered (executed / ran) by a Python interpreter, the right operand is evaluated first to determine its value.
- For now, the left operand will be always be an identifier. Python will reserve memory to store the evaluation, and then associate the identifier with that memory; that is, the identifier is now a variable whose value can be used in future Python statements.
- Now take out a piece of paper and draw a horizontal lines across after the first one-third of the paper and the second one-third of the paper.
- Think of a number. In the top section, write an assignment statement that assigns a variable
n
with that number. Along with aprint()
statement that echos (prints)n
. If I was doing it, I would be using 3 as the number.
n = 3
print( "n =", n )
- Next pretend you are the computer and execute the code. To simulate the assignment statement, using the middle section of the paper draw a box and label it
n
. Store your number in the box. To simulate theprint()
statement, copy the label literal string and the value in then
box in the bottom section of the paper.
- After carrying out these two statements, my paper looks like this.
- Now below the previous code, let's write an assignment statement that assigns a variable
s
to be twice the value ofn
. And also, aprint()
statement that echos the value ofs
.
s = 2 * n
print( "s =", s )
- Next pretend you are the computer and execute the new code. To simulate the assignment statement, using the middle section of the paper draw a box and label it
s
. Get the value in then
box and multiply that value by2
and store your the product in thes
box. To simulate theprint()
statement, copy the label literal string and the value in thes
box in the bottom section of the paper.
- After carrying out these two statements, my paper looks like this.
- Now below the previous code, let's write an assignment statement that assigns a variable
t
to be value ofs
and 4. And also, aprint()
statement that echos the value oft
.
t = s + 4
print( "t =", t )
- Next pretend you are the computer and execute the new code. To simulate the assignment statement, using the middle section of the paper draw a box and label it
t
. Get the value in thes
box and add4
to that value and store your the sum in thet
box. To simulate theprint()
statement, copy the label literal string and the value in thet
box in the bottom section of the paper.
- After carrying out these two statements, my paper looks like this.
- Now below the previous code, let's write an assignment statement that assigns a variable
u
to be one half oft
using integer division. And also, aprint()
statement that echos the value ofu
.
u = t // 2
print( "u =", u )
- Next pretend you are the computer and execute the new code. To simulate the assignment statement, using the middle section of the paper draw a box and label it
u
. Get the value in thet
box and integer divide that value by2
and store your the quotient in theu
box. To simulate theprint()
statement, copy the label literal string and the value in theu
box in the bottom section of the paper.
- After carrying out these two statements, my paper looks like this.
- Now below the previous code, let's write an assignment statement that assigns a variable
v
to be the difference ofu
andn
. And also, aprint()
statement that echos the value ofu
.
v = u - n
print( "v =", v )
- Next pretend you are the computer and execute the new code. To simulate the assignment statement, using the middle section of the paper draw a box and label it
v
. Get the values in theu
andn
boxes and get their differene and store your the result in thev
box. To simulate theprint()
statement, copy the label literal string and the value in thev
box in the bottom section of the paper. I bet your value forv
is 2. Can you figure out why?
- After carrying out these two statements, my paper looks like this.
Why are variables called variables
- Variables are call variables because we are allowed to update them in other assignment states.
- Now below the previous code, let's write an assignment statement that assigns variable
u
to be-5
. And also,print()
statement that echos the value ofu
.
u = -5
print( "u =", u )
- Next pretend you are the computer and execute the new code. To simulate the assignment statement, using the middle section of the paper change the value in the
u
box to-5. To simulate the first
print()statement, copy the label literal string and the value in the
u` box in the bottom section of the paper.
- After carrying out these two statements, my paper looks like this.
- Now below the previous code, let's write a
print()
statement that echos the value ofv
.
print( "v =", v )
- To simulate the
`print()
statement, copy the label literal string and the value in thev
box in the bottom section of the paper. Observe that the most recent assignment ofu
did not affectv
. An assignment only effects the target of the assignment statement.
- After carrying out the statement, my paper looks like this.
Altogether now
- Putting it altogether, we have the below, where
???
is the number you picked. The code can be found in program variety.py. The code will be made available during class.
n = ???
print( "n =", n )
s = 2 * n
print( "s =", s )
t = s + 4
print( "t =", t )
u = t // 2
print( "u =", u )
v = u - n
print( "v =", v )
u = -5
print( "u =", u )
print( "v =", v )