1.3 Python Variables
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: and ? The average is found by adding the three test scores first and then dividing the total by 3.
- Click
+ Codeto add a code cell - Type
total = 75+80+90 - Type
total/3 81.66666666666667is the result
total = 75 + 80 + 90
total/3
Show expected output
81.66666666666667Example: Find the average of three test scores: and ? 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
- Click
+ Codeto add a code cell - Type
total = 75+80+90 - Type
ave = total/3 - Type
print(ave) 81.66666666666667is the result
total = 75+80+90
ave = total/3
print(ave)
Show expected output
81.66666666666667Note:
- The value of
75+80+90is evaluated and stored intotal - The value of
totalfrom line one is used to evaluatetotal/3in the second line and the new value oftotal/3is stored inave. print(ave)is used to display the value stored inave
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 by storing the sum of the numerator in a variable named numerator_sum first. Then add another line that divides this sum by 4.
- Click
+ Codeto add a code cell - Type
numerator_sum = 2 + 3 + 4 + 5 - Type
numerator_sum/4on a new line. (It will display the value ofnumerator_sum/4) - Press the
playbutton 3.5, the value of the last executed line is displayed
numerator_sum = 2 + 3 + 5 + 4
numerator_sum/4
Show expected output
3.5Example:
radicand = 5**2 - 4*3*(-3)
-2 + radicand**(1/2)
Show expected output
5.810249675906654Note: 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 15Note:
- In the first line
a = 0, zero is stored ina. - In the third line
a = 10, 10 is assigned toa. Here the value of0that was stored inais updated(replaced) by10. - In the fifth line,
a = a + 5, thea + 5on the right side of the equal sign is evaluated as10 + 5. This is because10was stored inafrom line 3. Then10 + 5is stored inareplacing the previous value of10. - The last line,
ais displayed with its last updated value of15.
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 definedNote: 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 and has slope, where . Use this formula to find the slope of a line that passes through and . See code below.
x1 = 2
y1 = 5
x2 = 4
y2 = 7
m = (y1-y2)/(x1-x2)
m
Show expected output
1.0Adapted 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.