As a program executes, it manipulates data. How does it keep track of the data that it is manipulating? We say that it stores the data in variables. You might be comfortable with the term variable from algebra. A variable is simply a placeholder for a value that can change or vary over time. For example, each MHC course has variables associated with it in our registration system, such as the course’s instructor and capacity, enrolled, available and waitlist statistics as well as whether the course is by permission of instructor only (per-i). You can think of variables and their values as entries in a spreadsheet or table. For example, the values of variables for CS 151 could be represented by the table to the right.
We refer to a variable using an identifier. In Python, there are certain syntax rules an identifier must follow:
It must start with an uppercase or lowercase letter or an underscore (_)
The remainder must consist of uppercase or lowercase letters, underscores (_) or digits
It must not conflict with keywords that Python has reserved for its own use (we’ll learn more about these keywords throughout the course)
Note: identifiers are case sensitive in Python! That means that courseName and coursename would refer to two different variables. A common programming error is due to typos or changes in capitalization, so watch out for these! (Remember, computers are stupid!)
Programming tip: To avoid errors due to capitalization, it is good to be consistent in how you name your variables. I like to use camelCase. camelCase is so named because the variables have “humps” in the middle. If my variable name consists of multiple words stuffed together, all the interior words have their first letter capitalized. So, I would always say courseName or numberOfCards. By sticking to a naming convention, you will not need to remember how a specific variable is capitalized; you will naturally always capitalize it the same way.
Example valid identifiers:
courseName
number_of_cards
happy2
Example invalid identifiers:
2happy
count#s
so wrong
number-of-cards
The assignment operator, denoted by the = symbol, will assign a value to a variable. The variable name being assigned is to the left of the = symbol. To the right of the = symbol is an expression. So, the syntax of an assignment statement is:
*variable* = *expression*
*variable* can be any variable, and *expression* can be any expression, such as a constant, an arithmetic expression, or some other kinds of expressions we will see later.
The semantics of the assignment statement is as follows. Python evaluates the expression and assigns it to the variable. If the variable has not been used before, an assignment statement creates a new variable and assigns it the value. For example, the statement
enrolled = 28
creates a new variable called enrolled and assigns it the value 28.
However, if the variable has already been used, the assignment statement updates the value. Note that the update effectively erases the previous value, and you cannot “rewind the clock” to get it back. For example, after the statements
enrolled = 28
enrolled = 29
the variable enrolled has the value 29.