Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

The Assignment Statement

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, in Multiple Assignment Statement, we'll look at multiple assignment.

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.

Example 6.1. 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

We have an object, the number 150, which we assign to the variable shares. We have an expression 3+5.0/8.0, which creates a floating-point number, which we save in the variable price. 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.py once, after we create this file. Then, when we run this progam, we see the following.

$ 

./example3.py

543.75
$ 

Augmented Assignment

Any of the usual arithmetic operations can be combined with assignment to create an augmented 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

Example 6.2. 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 of Craps Odds in the section called “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.

Example 6.3. 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
print "first roll lose", lose

# Compute the odds of rolling a point number (4, 5, 6, 8, 9 or 10)
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.

As with many things Python, there is some additional subtlety to this, but we'll cover those topics later. For example, multiple-assignment statement is something we'll look into in more deeply in Chapter 13, Tuples .


 
 
  Published under the terms of the Open Publication License Design by Interspire