3.9. FUNDAMENTO Y SUSTENTO LEGAL QUE FACULTAN A LAS
3.9.1. Descripción de los componentes del Orgánico Funcional
Assignment is fundamental to Python; it is how the objects created by an expression are preserved. We’ll look at the basic assignment statement, plus the augmented assignment statement. Later, inMultiple Assignment Statement, we’ll look at multiple assignment.
7.2.1 Basic Assignment
We create and change variables primarily with the assignment statement. This statement provides an expression and a variable name which will be used to label the value of the expression.
variable = expression
Here’s a short script that contains some examples of assignment statements.
example3.py
#!/usr/bin/env python
# Computer the value of a block of stock
shares= 150
price= 3 + 5.0/8.0
value= shares * price print value
1. We have an object, the number150, which we assign to the variableshares.
2. We have an expression ‘3+5.0/8.0’, which creates a floating-point number, which we save in the variableprice.
3. We have another expression, ‘shares * price’, which creates a floating-point number; we save this in value so that we can print it. This script created three new variables.
Since this file is new, we’ll need to do the chmod +x example3.pyonce, after we create this file. Then, when we run this progam, we see the following.
$ ./example3.py 543.75
7.2.2 Augmented Assignment
Any of the usual arithmetic operations can be combined with assignment to create anaugmented assignment statement.
For example, look at this augmented assignment statement: a += v
This statement is a shorthand that means the same thing as the following: a = a + v
Here’s a larger example
portfolio.py
#!/usr/bin/env python
# Total value of a portfolio made up of two blocks of stock
portfolio = 0
portfolio += 150 * 2 + 1/4.0
portfolio += 75 * 1 + 7/8.0
print portfolio
First, we’ll do the chmod +x portfolio.py on this file. Then, when we run this progam, we see the following.
$ ./portfolio.py 376.125
The other basic math operations can be used similarly, although the purpose gets obscure for some operations. These include ‘-=’, ‘*=’, ‘/=’, ‘%=’, ‘&=’, ‘^=’, ‘|=’, ‘<<=’ and ‘>>=’.
Here’s a lengthy example. This is an extension ofCraps Oddsin Numeric Types and Expressions.
In craps, the first roll of the dice is called the “come out roll”. This roll can be won immediately if one rolls 7 or 11. It can be lost immediately if one roll 2, 3 or 12. The remaining numbers establish a point and the game continues.
craps.py
#!/usr/bin/env python
# Compute the odds of winning on the first roll
win = 0
win += 6/36.0 # ways to roll a 7
win += 2/36.0 # ways to roll an 11
print "first roll win", win
# Compute the odds of losing on the first roll
lose = 0
lose += 1/36.0 # ways to roll 2
lose += 2/36.0 # ways to roll 3
lose += 1/36.0 # ways to roll 12
point = 1 # odds must total to 1
point -= win # remove odds of winning
point -= lose # remove odds of losting
print "first roll establishes a point", point
There’s a 22.2% chance of winning, and a 11.1% chance of losing. What’s the chance of establishing a point? One way is to figure that it’s what’s left after winning or loosing. The total of all probabilities always add to 1. Subtract the odds of winning and the odds of losing and what’s left is the odds of setting a point. Here’s another way to figure the odds of rolling 4, 5, 6, 8, 9 or 10.
point = 0
point += 2*3/36.0 # ways to roll 4 or 10
point += 2*4/36.0 # ways to roll 5 or 9
point += 2*5/36.0 # ways to roll 6 or 8
print point
By the way, you can add the statement ‘print win + lose + point’ to confirm that these odds all add to 1. This means that we have defined all possible outcomes for the come out roll in craps.
Tip: Tracing Execution
We can trace the execution of a program by simply following the changes of value of all the variables in the program.
We can step through the planned execution of our Python source statements, writing down the variables and their values on a sheet of paper. From this, we can see the state of our calculation evolve.
When we encounter an assignment statement, we look on our paper for the variable. If we find the variable, we put a line through the old value and write down the new value. If we don’t find the variable, we add it to our page with the initial value.
Here’s our example from craps.py scriptthrough the first part of the script. The winvariable was created and set to ‘0’, then the value was replaced with ‘0.16’, and then replaced with ‘0.22’. The lose variable was then created and set to ‘0’. This is what our trace looks like so far.
win: 0.0 0.16 0.22 lose: 0
Here’s our example whencraps.py script is finished. We changed the variablelose several times. We also added and changed the variablepoint.
win: 0.0 0.16 0.22
lose: 0.0 0.027 0.083 0.111 point: 1.0 0.77 0.66
We can use this trace technique to understand what a program means and how it proceeds from its initial state to its final state.
As with many things Python, there is some additional subtlety to assignment, but we’ll cover those topics later. For example,multiple-assignment statement is something we’ll look into in more deeply in Tuples.