Variables

Variables are temporary areas in memory that can be used to store data such as numbers or text (aka strings). Variables are not constant, and can be changed by the program.

To assign a value to a variable, the assignment operator ("=") is used, as in:

numCars = 7

In the example, the variable "numCars" contains the value 7, and is declared as a number. Compare this to:

numCars = "7"

...where numCars is declared as a string (note the quotation symbols). The differences between numbers and strings will become more apparent in future lessons.

Variables can contain characters, digits, or underscores, and should always be named something appropriate to make your program more "readable". A common practice is to use "Camel Case", and will be the practice used in this class. Note: my own preference is to specifically use lower camel case, meaning that the first character is always lower case, as in "numCars" (as opposed to "NumCars").

Variables are case-dependent, meaning that numcars is not the same as numCars.

Note that Python has a number of pre-defined keywords that cannot be used as variable names. These are: and, as, assert, break, class, continue, def, del, elif, else, except, exec, finally, for, from, global, if, import, in, is, lambda, not, or, pass, print, raise, return, try, while, with, and yield.

Assignment

Create a program that assigns the value 7 to a variable called "luckyNumber", then prints “The value of luckyNumber is <value of luckyNumber>”.

Save as "015.py".

Assignment

Modify program 015 so that it also prints the square of the number, the cube of the number, the square root of the number, and the number times 2.

Save as "016.py".

Assignment

Modify program 015 so that the program prints "LuckyNumber + 2 = 9", where "9" is calculated by adding luckyNumber and 2.

Save as "017.py".