Variables are place holders for information, the information might be in one of two main types; text or numeric.
- Variable names cannot use special characters which includes spaces.
- A string or text must be placed between a single or double quotation mark. The quotation marks must be used in pairs, so one quote is used at the beginning of the words and another at the end. If single quotes are used, then the same type of quote must be used to finish the expression.
- If the variable is to be a number only numeric characters can be stored in the variable, a number can be text or numeric, but non-numeric characters can only be text. It is very important to understand that text number characters cannot be used in mathematical operations.
- The designer can use the plus sign to combine variables of the same type. Numerical items and text items cannot be combined together unless the string function is used. The string function (str) is used to make the number into a text variable in a specific operation. The str must be lower case letters. For example, the format using the str function would be:
text variable = str(numeric variable)
The text variable and numeric variable are not true variable names since they have spaces in the naming and that is illegal in Python.
- A variable can also be a list, thus populated by more than one element, also known as an array. At this point only lists of one dimension will be considered. A square bracket ([ ]) is used to make a list.
- While naming the variable it is recommended that both upper and lower case letters be used to make the variable more descriptive. In general, make the first letter of the variable lower case, Python is case sensitive and thus a change in capitalization will create a new variable. Therefore, the string written above might be written like the following as a true variable.
text variable = str(numericVariable)
The difference is no spaces are used and capitalization has been added to make the name easier to understand.
- The comment line has been previously defined and they will not be discussed again, but do note how the comments are used along with the functional components.
- myName and myName2 are both string variables with the same content, each using a different method to denote the string function.
- myAge and myGrade are both numeric variables, one being a whole number and the other a decimal, no specification on the number of digits is required.
A list variable contains more than one item and all items must be of the same type, such as all string or all numeric. It is suggested that the naming of the list variable use a format with a name so that the user knows it is a list, for example, addressList or listAddress. As the user becomes more familiar with the operational components of Python, this may become less important, but initially it can be extremely valuable.
- There are two defined lists, listAddresses and listGrades.
- The first list is a string, note the quotes around the numbers and the text. The list starts with a square bracket and items in the list are separate by commas. Note the text is in a different color, not all editors will change the text colors due to variable type.
- In the numeric list, all elements are separated by commas but there are no quotation marks used. The square brackets are used again and the numbers are a different color.