Understanding data types and how to use variables are key skills in any type of programming. Python is different from most other languages in both of these areas. Python has fewer data types and auto-assigns them. Variables can be created simply by using them, and the data type of the variable is auto-assigned based on the value assigned to it.
Before discussing variables, it is helpful to understand the concept of a data type, in general. All computer languages use data types, although they sometimes vary in exactly how they are defined. A data type refers to the type of data contained in a variable, a literal, an object, etc. There are several basic types, such as string, integer and float.
A string is a value that contains alphanumeric characters. It might contain characters, words, or an entire sentence. A string is enclosed in quotes. In Python, they may be single or double quotes:
- "The rain in Spain falls mainly on the plain"
Note that the last value ("123"), although a number, is considered a string because it is enclosed in quotes. It is not considered a number in Python.
Numbers in Python are of two types: int (integer) and float. Simply put, an integer does not have decimal places and a float does have decimal places. So:
- 123 (int)
- 500789 (int)
- 1.2 (float)
- .0005 (float)
- 1234.567 (float)
Python also recognizes two other numeric types (decimal and complex) which are not important to discuss here.
Why is it important to understand data types? The primary reason is to avoid potential exceptions caused by using them incorrectly. Here is an example in code:
The example code attempts to add a string and number. That creates an error (an exception). It is not possible in Python; it causes the program to crash.
This second example does not throw an exception, but the result may not be what the programmer expected. The code adds 2 integers (11 + 40 = 51) and then performs a division of 2 integers (1100/51). The quotient result of any division is always a float. Knowing that would help the programmer prepare for displaying the result.
A variable is technically a location in memory that is reserved for future use and that is given a name for reference in the program. In most languages, creating a variable looks something like this (C#):
We say this statement declares a variable. It defines the data type (string), a name ("myName") and assigns an initial value ("Sam").
To declare a variable in Python is simpler:
Python auto-assigns the data type based on the value assigned to it (in this case, a string). If the value is enclosed in quotes, it is a string. If it is numeric without a decimal place (1,2,3), it will be an int (integer). Numbers with decimal places (123.45) will be assigned as a float.
Note that to assign a value to a variable uses a single equal sign ("="). A double equal sign ("==") is used for comparing values, but is invalid for assigning values.)
Variable names cannot start with a number. A collection of alphanumeric characters without quotes is not valid, and will throw an exception:
Variables are different from literals. A literal is a value, such as "Sam". A variable is a named storage container that may or may not contain a value. They can often be used in the same way:
The importance of knowing the difference is terminology. We normally want to avoid using literals. Understand that sentence is why it is important. If the project manager (or instructor) says "Don't use literals", it is important to understand the difference.
As a beginning Python programmer, there are only a couple of key things to understand about using variables.
First, names are case sensitive: myName and MYName and myname are not the same thing. What value did the programmer expect in the code below?
The lesson here is that Python will accept and declare new variables that are not named the same. This may be the result of typos, and the program may not work as intended because of it. So one step to take when a program runs, but does not run as expected: check the spelling of variable names!
This is also a reason that programmers should be vigilant about variable naming. Using "r", "p", and "i" as variable names makes it much harder to understand and troubleshoot a program. Using variable names such as "rate","principal", and "interest" makes the program easier to read, understand, and troubleshoot. It also helps eliminate typos that create unintended variables.
Second, there are cases where it is important to declare a variable before trying to use it. This is hard to explain without going into coding statements that have not been covered yet. But the example below illustrates this. The first print() statement adds 1 to the variable cards, and it displays the result. The second print() statement fails because total is not yet declared and the program does not know what it is.
The solution, and a good programming practice, is to declare variables with a default value before the program starts working through calculations or other processing:
Lastly, using variables wisely can substantially improve readability and maintenance of even simple programs. Take this example:
In this case, the same text is used over and over in multiple print() statements. This is hard to maintain. If a change were needed, multiple lines would have to be edited. It would be easy to make a type in one or forget to change one. A better approach is to rely on a variable to hold the text value (example below). Not only is this easier to maintain, but it is much easier to read and follow. If the text needed to be changed, only a single edit is required. The longer the program, the more important it is that is is written in a manner that is easy to read and maintain.
This is also what is meant by "don't use literals": create a variable with the needed value and use it instead of a literal.
This principle of avoiding repeated uses of the same code is called "DRY" (Don't Repeat Yourself). It is a best practice for coding. Read more about it here: Dry Coding Principles