Varying very versatile variables C’mon Mr. Hastings… that much alliteration is just unnecessary.
What is a variable? As in algebra and mathematics, a variable stands in for an unknown or a temporary value Variables can change. In CS, a variable stores a value. A variable can ONLY hold one value at a time. A variable is given a name The names of a variables are in lowercase, have no spaces, cannot use keywords (like input or print) CS computer science ( I will use this abbreviation often to save space) A variable can be named anything, from a single letter (like: x, y, a, b, or c), to words connected by an underscore (like: game_points, or time_allowed). When creating variable names, try to make the name mean something. We use the same variables often in a program, so having a letter like x may not help us remember what x stands for. A variable like “name” or “favorite_color” helps us know what its meaning is.
Storing information Think of a variable as a “memory location” The first time we set a variable, we are Initializing the variable If we reassign a value to a variable, we are destroying the previous value. (oh no!) Numerical values can be represented as either a float or an integer. Float floating decimal point 72.9812 will not be in same location as 7.29 Integer a whole number (no decimal) Initializing a variable is the same as ‘setting a variable equal to something’. In Python we type: variable_name = value The value is now recorded or stored in the variable_name. Variables can, and often do change. So, if we need a new value to replace what we initialized, we would simply enter again: variable_name = newvalue When this happens, the old value it destroyed / lost and the new value is now set. We can save numbers in our variables, but when we calculate or compare them to other numbers we have to differentiate them as either a integer (a whole number), or a float (like pi 3.14159). This is because the language need to know if you mean the value 10 or a string (just the one and zero key) A string is a ‘string’ of characters the computer does not calculate or evaluate them.
Let’s rank Wicomico County Soccer Current top two teams: 1) Parkside 2) Mardela How would we create variables to represent the top two teams? Yes, this is just pretend. Using a variable name that makes sense will help us when we use the variables later on. What would you use as variable names? Why would you use those?
Swap rankings Imagine tonight – the teams swap rankings (#1 becomes #2 and vice versa) How would we swap variables without destroying values? What would need to happen? If we just replace one team variable with the other, the value is lost.