Login
📚 Python for Introductory Statistics
Chapters ▾

1.3 Python Variables

Watch demo video

A variable is a location that stores temporary data within a program. Variables can be modified, stored or displayed whenever needed. Variables that you define in one cell can later be used in other cells.

In Python, variables are defined, created, and declared once you assign a value to it. Variable names can be arbitrarily long and can contain both letters and numbers, but they have to begin with a letter. Avoid using one of Python's keywords (reserved words) such as print, input, and, or, def, if etc. Here is a link to the complete list.

The equal sign = is called the assignment operator. It takes the value of the right-hand side of the equal sign and assigns it to the variable name on the left side of the equal sign.

Example: Find the average of three test scores: 75,80 and 90? The average is found by adding the three test scores first and then dividing the total by 3.

  1. Click + Code to add a code cell
  2. Type total = 75+80+90
  3. Type total/3
  4. 81.66666666666667 is the result
total = 75 + 80 + 90
total/3
Show expected output
81.66666666666667

Example: Find the average of three test scores: 75,80 and 90? The average is found by adding the three test scores first and then dividing the total by 3. Save the average in a variable named ave

  1. Click + Code to add a code cell
  2. Type total = 75+80+90
  3. Type ave = total/3
  4. Type print(ave)
  5. 81.66666666666667 is the result
total = 75+80+90
ave = total/3
print(ave)
Show expected output
81.66666666666667

Note:

Note: In a code-cell with multiple lines, the program executes one line at a time in sequence from top to bottom.

Note: This time, aver is not in quotation marks in the print(aver) function. Anything in quotation mark is displayed as is. Variable names are not in quotes and are separated by a comma.

Example: Evaluate 2+3+5+44 by storing the sum of the numerator in a variable named numerator_sum first. Then add another line that divides this sum by 4.

  1. Click + Code to add a code cell
  2. Type numerator_sum = 2 + 3 + 4 + 5
  3. Type numerator_sum/4 on a new line. (It will display the value of numerator_sum/4)
  4. Press the play button
  5. 3.5, the value of the last executed line is displayed
numerator_sum = 2 + 3 + 5 + 4
numerator_sum/4
Show expected output
3.5

Example: 2+524×3×(3)

radicand = 5**2 - 4*3*(-3)
-2 + radicand**(1/2)
Show expected output
5.810249675906654

Note: In Python, the same variable name can be used to store different values multiple times, but only the last assignment is stored for future use. The old value is updated every time a new value is assigned. See example below.

Note: The print function displays anything in quotes as is but variable names are not in quotes and are separated from the quote by a comma.

a = 0
print('The first value of a is',a)
a = 10
print("The updated second value of a is ",a)
a = a + 5
print( 'The last updated, third value, of a is ', a)
Show expected output
The first value of a is 0
The updated second value of a is  10
The last updated, third value, of a is  15

Note:

  1. In the first line a = 0, zero is stored in a.
  2. In the third line a = 10, 10 is assigned to a. Here the value of 0 that was stored in a is updated(replaced) by 10.
  3. In the fifth line, a = a + 5, the a + 5 on the right side of the equal sign is evaluated as 10 + 5. This is because 10 was stored in a from line 3. Then 10 + 5 is stored in a replacing the previous value of 10.
  4. The last line, a is displayed with its last updated value of 15.

Deleting a Variable

Use the keyword del to delete a variable. See example below.

seconds_in_week = 60*60*24*7
del seconds_in_week
print(seconds_in_week)
Show error output
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-403-cf943aec5ccc> in <module>()
      1 seconds_in_week = 60*60*24*7
      2 del seconds_in_week
----> 3 print(seconds_in_week)

NameError: name 'seconds_in_week' is not defined

Note: Both the value of 60*60*24*7 and the variable name seconds_in_a_week are deleted from the memory space in line 2. Asking Python to print seconds_in_a_week after the delete yields an error. NameError: name seconds_in_a_week is not defined. Collab provides a link to the popular question and answer website https://stackoverflow.com to help you look for any possible fixes to the error.

Example: A line that passes through two ordered-points  (x1,y1) and  (x2,y2) has slope, m=y1y2x1x2 where x1x2. Use this formula to find the slope of a line that passes through  (2,5) and  (4,7). See code below.

x1 = 2
y1 = 5
x2 = 4
y2 = 7

m = (y1-y2)/(x1-x2)
m
Show expected output
1.0

Adapted from Python for Introductory Statistics, by Simon Aman (Truman College, City Colleges of Chicago), licensed under CC BY 4.0. Changes were made: reformatted as an accessible XYZ web edition with live in-browser code cells. License: CC-BY-4.0.