A variable is a named storage location that holds a single value.
- A variable's value must be stored in the computer's memory.
- There are different types of values.
Most of what is said here applies to any programming language but different languages may do things a bit differently.
Two Fundamental Categories of Type
Two main types (categories) of items a variable may store are:
Strings are comprised of zero or more characters.
Characters are the usual letters, digits, and punctuation seen on any computer keyboard.
A string is a sequence of zero or more characters.
Programmers always use the term string to refer to variables/values that are comprised of characters.
Recall there are different sets of numbers:
- Integers are, for example: ..., -3, -2, -1, 0, 1, 2, 3, ...
- Real numbers are, for example: 142.98, .034, -897.1
- Complex numbers are, for example: 2 + 3i, 0 - 5i
The different types of numbers are stored in computer memory differently.
The following is typical:
- If, an operation is done on numbers of the same type the result will have that type.
For example: an integer + an integer will have an integer result.
- If, an operation is done on mixed type numbers the result will be in the type where no accuracy is lost.
For example: an integer + a float will have a float result.
For most numeric types there is a limit to the values that may be stored.
The amount of storage for a number is fixed.
Think of a car odometer, the number of digits is limited.
This allows for numeric operations to be more efficient and thus done more quickly.
If a number type allows unlimited values then the storage cannot be fixed because the number of digits is unlimited.
Although strings may seem simpler the storage required depends on the number of characters that comprise the string.
The storage for strings is handled by the language and is not a problem for the programmer.
Programming and Variable Types
Three fundamental programming types are:
- Real numbers, called floats in Python
- Strings, ex: "hello", 'greetings earthling'
- Integers do not have, and can not have, a fractional part (decimal point).
- Strings are delimited by quotes.
- Python allows either single quotes (') or double quotes (") to delimit a string.
- There is a difference between 347 and '347'
Variables are used in expressions. This is covered in an upcoming set of notes.