Variables in Python
Variables are an important part of coding as they allow the programmer to store and use information. This activity gets us started with variables.
Turn In: Variables.py
A variable is a named storage location that can store one value at a time. It is variable because you can change the stored value.
Picture a variable as a box with a name on it. To store the name of the group you are going to see and the number of tickets to purchase, you would set up two variables and name them accordingly. Each variable would hold one value, the group would be a string and the number of tickets would be an integer. Here is that representation in picture form and then the Python code.
| Cathedrals |
| 5 |
group = 'The Cathedrals'
tickets = 5
Assuming twice as many tickets are needed, a calculation can be done to double that number and a different calculation done to calculate the cost of those tickets (assume $15.75 per ticket). Those calculations would look like ...
tickets = tickets * 2
totalcost = tickets * 15.75
| 10 |
| 157.5 |
In the first statement, the right side is calculated by taking the 5 in tickets and multiplying it by 2 then putting the answer (10) into the tickets variable replacing the 5 that was in there.
Print can be used to display the information in the variables such as ...
print(tickets, "tickets to see", group, "costs", totalcost, "dollars")
Python allows variable names to start with either a letter or an underscore and contain combinations of letter, underscore, or digit. Examples of invalid variable names would include 1player because it starts with a number and player#1 would be invalid because it has a character (the #) other than a letter, underscore, or digit.
Variable names cannot be Python reserved words such as for, if, while, and int.
To make code more readable, use meaningful names of a reasonable size. The case of the variable matters. Consider ...
player1 = "Sue"
Player1 = "Sam"
When run, "Sue Sam" is displayed. If case did not matter than "Sam Sam" would have been displayed.
Variable Types and Why It Matters
When a value is first assigned to a variable, Python creates that variable and determines the type of variable that is being created. In the examples above, the group variable is of type string while tickets is of type integer and totalcost of type float (number with digits to the right of the decimal point).
There are certain operations where it is necessary to know what type the variable is. To determine the variable's type, use the type function with the variable name in parenthesis directly after type. To determine the type of the three variables used above, do the following ...
print(type(group), type(tickets), type(totalcost))
The result will look something like this ...
<class 'str'> <class 'int'> <class 'float'>
Sometimes it is necessary to combine the contents of two variables that are of different type. Python provides conversion functions to do this. To convert a number to a string use str and use int to convert to an integer. Here are two examples:
print("Tickets: " + str(tickets))
print("Ticket total rounded: ", int(totalcost))
Note: Variables.py should contain the examples above. As you read through the information, type in those statements and run the program. If you are curious about how something works, change the statements and try it. If not able to figure out how something works, ask your instructor.
Copyright © 2022 Eric Schumm. Permission granted to freely use this in your classroom.