3.1. Assigning a Name¶
Learning Objectives:
Understand the concept of a variable.
Assign a value to a variable.
Use assignment in calculations.
Understand the ways that students get assignments wrong.
Reuse variables across different assignment statements.
A computer can associate a name with a value. It does this by creating a variable, which is space in computer memory that can represent a value. An example of a variable is a score in a computer game. The score usually starts at 0 and increases as you play the game. The score can change or vary during the game, which is why we call it a variable. You also associate a name with a value when you enter a new contact name and phone number in your cell phone. When you tell your phone to call “Alexa” it will look up the phone number associated with that name and call it.
Think of a variable as a box that has a label on it and you can store a value in the box. The value can be anything that can be represented on a computer and stored in a computer’s memory. A computer’s memory is only made up of numbers (really, just patterns of voltages, but we can think about them as numbers). Everything that a computer can remember in its memory is translated into these numbers – but don’t worry about how this works right now.
In programming languages, setting a variable’s value is also called assignment. A statement like a = 4
means that the symbol a
refers to space (in the computer’s memory) that is assigned the value 4
. When we use the symbol a
in a program the computer will substitute the value 4
. If we later change the value stored at a
, say by doing a = 7.2
then we say that the variable a
now has the value 7.2
meaning that the value in the box (memory) associated with the name a
is changed to 7.2
.
Click on the right arrow below to play the following video.
3.1.1. Legal Variable Names¶
There are restrictions on what you can use as a variable name.
It must start with a letter (uppercase like
A
or lowercase likea
) or an underscore_
It can also contain digits, like
1
or9
, just not as the first characterIt can’t be a Python keyword such as
and
,def
,elif
,else
,for
,if
,import
,in
,not
,or
,return
, orwhile
. These have special meaning in Python and are part of the language.Case does matter. A variable named
result
is not the same as one namedResult
.
Since you can’t have spaces in a variable name you can either join words together by uppercasing the first letter of each new word like heightInInches
or use underscores between words height_in_inches
. Uppercasing the first letter of each new word is called camel-case or mixed-case.
- _a1
- You can use an underscore as the first character in a variable name
- my_name
- You can use an underscore between words in a variable name.
- amountOfStuff
- You can use both uppercase and lowercase letters in a variable name.
- BMP
- You can use only uppercase letters in a variable name.
- 1A
- You can't use a digit as the first letter in a variable name.
Which of the following is not a legal variable name?
- _my_name
- This is legal, but you don't usually start a variable name with an underscore.
- my name
- You can't have a space in a variable name.
- myname
- This may be hard to read, but it is legal.
- myName
- Since you can't have spaces in names, one way to make variable names easier to read is to use camel case (uppercase the first letter of each new word).
- my_name
- Since you can't have spaces in names, one way to make variable names easier to read is to use an underscore between two words.
Which of the following is not a legal variable name?