Variables are one of the most important concepts in programming, and you will certainly be using them in every useful program you write. Variables are like labels, or names, for the numbers and other values in your programs. Variables are used for keeping track of all the facts and figures you use throughout your code.
Variables can be given any name you choose (although there are some rules to that!). Choosing good variable names makes it easier to write your code, to change your code later and to help others understand your code.
Choosing good variable names can make a program easier to read and understand. A variable can generally be any name you want, as long as it contains only:
Uppercase (A through Z) or lowercase (a through z) letters
Digits (0 through 9)
The underscore character ("_")
And, it can't start with a digit, either.
So, for example:
Variable Name |
Allowed? |
---|---|
fred |
YES |
Fred |
YES |
FRED |
YES |
Fred.Schmed |
NO — Can't use periods |
this_is_a_really_long_variable_name |
YES |
thisisareallylongvariablenametoo |
YES — Although it sure is hard to read |
Fred's_house |
NO — Can't use apostrophe |
Freds house |
NO — Can't use spaces |
Freds_house |
YES |
Fred123 |
YES |
123Fred |
NO — Can't start with a digit |
You Don't Say? |
NO — Can't use apostrophe, question mark, or spaces |
#firstworldproblems |
NO — Can't use hashtag |
Even though you can name variables any way you want, it's common to write variable names that are longer than one word by separating the words with underscores. That's the way we'll generally do it throughout this guide, with one exception: for variables that don't change values throughout the program, we'll generally use ALL_CAPS for those variable names.
A "variable" is a value that can vary (change), and that's what program variables do throughout a program. To assign a variable a value, you use the equals sign (=) in what is called an "assignment statement," which looks like:
To change it to a different value, you use another assignment statement:
A variable can be many different types of values, including numbers, phrases (called "strings"), or lists. The value of a variable can change many times in a program.
As an example, we'll calculate the number of seconds in a day. To do
this, we'll start by creating a variable called sec_in_minute
that will be the number of seconds in a minute (which is 60):
Now, let's create two more variables:
We can now compute (and print) the number of seconds in a day with:
The asterisk ('*') is a multiplication symbol — see the project for more information.
Here is what the output would look like if you ran that code:
86400
Remember we said above that variables can be almost any type of data –
that includes data from other variables. We can take our program one step
further by creating a variable called sec_in_day
and using it to
be our calculation for the number of seconds in a day:
Now, if you want to output the number of seconds in a day, you can simply
print the number being held by the sec_in_day
variable:
This is what the output would look like:
86400
Variables can also be "strings". In programming, strings are just sequences of characters — generally normal text. For example, we could have a variable that holds one of the output message we're using above:
We could then print that message to the output, along with our calculation:
Here is what the code and output would look like using a variable for the text string:
Number of seconds in a day: 86400
Note that because message
is a variable, we don't put quotation marks
around it.
In this tutorial, we've discussed two types of variable values: numbers and strings. There are many other types of data values that can be used as well:
Lists — Lists are ordered sequences of other values. We'll discuss them more in and will be using them frequently in future projects.
Booleans - Booleans are a funny name for the values
True
and False
. We'll discuss them more in
and you'll experience the concept of booleans lots
of different places.
None — None
is a special value which appropriately indicates
that there is no value at all.
Those are the most common types of values, and they cover the ones you'll see in the core set of Ready Set STEM projects. But there are more, including types you can even define yourself.
One last note: in Python, as in some other programming languages, all values are called objects, and we'll discuss objects more throughout this guide.