1. Python Basics1. Python Basics\1.2 How Programs Are Written\1.2.1 Variables

1.2.1 Variables

 

In the last section, you wrote a loan payment calculator with some specific numbers, and then you changed those numbers to make a different calculation. It was annoying to change the same numbers in six or seven different places, wasn’t it?

 

In order to reduce this annoying work, programs have variables.

 

DEFINITION: Variables let you give names to your numbers, strings, and other data, and work with these names instead of repeatedly using the data itself.

 

 

Create a new Python file, type the following and run it:

 

  1. number_of_slices = 8  
  2.   
  3. print "Half of a pie:", number_of_slices / 2  
  4. print "One quarter of a pie:", number_of_slices / 4  

 

You should see the output:

 

Half of a pie: 4

One quarter of a pie: 2

 

This program has a variable called number_of_slices, which we use to keep track of the number of slices in a pie.

 

Line 1 creates this variable and assigns it the value 8. In all the lines after this, whenever Python sees number_of_slices, it automatically uses the number 8.

 

If we want to change the number of slices of pie to 16, we only need to change it in one place. Modify your previous program as follows and run it:

 

  1. number_of_slices = 16  
  2.   
  3. print "Half of a pie:", number_of_slices / 2  
  4. print "One quarter of a pie:", number_of_slices / 4  

 

You should see the output:

 

Half of a pie: 8

One quarter of a pie: 4

 

Variables have their name because they can vary. You can begin a program by assigning a variable one number, and then re-assign it another number later. Modify your previous program as follows and run it:

 

  1. number_of_slices = 8  
  2.   
  3. print "Half of a pie:", number_of_slices / 2  
  4. print "One quarter of a pie:", number_of_slices / 4  
  5.   
  6. number_of_slices = 32  
  7.   
  8. print "Half of a pie:", number_of_slices / 2  
  9. print "One quarter of a pie:", number_of_slices / 4  

 

You should see the output:

 

Half of a pie: 4

One quarter of a pie: 2

Half of a pie: 16

One quarter of a pie: 8

 

In line 1, number_of_slices is given the value 8. So, in lines 3 and 4, when Python sees number_of_slices, it uses the value 8. But then, in line 6, number_of_slices is given the new value 32. So in lines 8 and 9, when Python sees number_of_slices, it uses the value 32.

 

We are going to go back and rewrite the loan program to use variables. Create a new Python file, type the following and run it:

 

01.  rate = 5.25  

02.  principal = 10000  

03.  months = 72  

04.    

05.  print "This program calculates how much money you will pay on a loan."  

06.    

07.  print "Annual Rate:", rate  

08.  print "Principal:", principal  

09.  print "Months:", months  

10.    

11.  monthly_payment = rate / 1200 * pow(1 + rate / 1200, months) * principal / (pow(1 + rate / 1200, months) - 1)  

12.    

13.  print "Your monthly payment will be:", monthly_payment  

14.  print "Your total payable will be:", monthly_payment * months  

 

You should see the output:

 

This program calculates how much money you will pay on a loan.

Annual Rate: 5.25

Principal: 10000

Months: 72

Your monthly payment will be: 162.211537477

Your total payable will be: 11679.2306983

 

The first three lines create variables for the rate, the principal (or, amount of money), and the number of months, and assign them values. Python remembers the values of each of these variables and uses them in later calculations. In fact, in line 11, when we use these variables to calculate the monthly payment, we assign the answer to another variable called monthly_payment.

 

With this program, you can change the rate, or the amount of money to borrow, or the number of months, just by changing the values assigned to the variables.

 

Note that monthly_payment is named with an underscore instead of a space. Python has syntax (remember, language rules, like grammar) for variables.

 

 

SYNTAX: Variable names must start with a letter, and are only allowed to have letters, numbers, and underscores.

 

 

So  hornet_buzzing_volume is an acceptable variable name, but highest-win-% is not, because it has dashes and a % symbol, and neither of these symbols is allowed.

 

Variables can also contain strings. Create a new Python file, type the following and run it:

 

  1. name = "Timmy"  
  2.   
  3. print "My name is", name  

 

You should see the output:

 

My name is Timmy

 

Variables can also contain many other complicated types of data, which you will learn about later in the course.

Using the equals sign for variables can be confusing. In traditional math, people have the expectation that equality is permanent.

 

  1. volume = 3  
  2. volume = 4  

 

Since the equals sign in traditional math is permanent, it might look like we have set 3 equal to 4.

 

But Python is performing instructions sequentially. This program is saying "set the volume to 3" and then it is saying "set the volume to 4".

 

This kind of equals is called assignment equals. It assigns the value on the right side to the variable on the left side.

 

This is the only way to create new variable names in Python. When a new variable name shows up on the left side of an assignment equals, Python interprets this to mean, "create a new variable with this name, and assign it the value on the right side". If you try to use a variable before creating it, the Python interpreter will give an error.

 

The most bizarre property of assignment equals is that we can use the variable on both sides of an assignment equals statement. Create a new Python file, type this very strange program, and run it:

 

  1. x = 17  
  2. x = x + 5  
  3. print x  

 

You should see the output:

 

22

 

The first line makes a variable called x, and assigns it the value 17.

 

The second line looks like it is saying "x is equal to itself plus 5", which would not be true in traditional arithmetic. But it is actually saying "the new value of x is the old value of x plus 5". So, start with 17 and add 5. That is the new value of x: 22.

 

Top of Page