Type Checking and Validation
A data type is a classification that specifies what kind of value a variable holds and what operations are valid on that value.
Why Types Matter
A data type is a classification that specifies what kind of value a variable holds and what operations are valid on that value. In Python, the number 5 and the string "5" are different kinds of values. That difference affects what you can do with them: a number participates in arithmetic, while a string represents text and supports text-based operations. Understanding types is therefore essential for predicting how a program will behave.
Python's two fundamental beginner-level categories are numbers and strings. Numbers represent quantities; strings represent text. Each category has its own operations and rules.
From Assignment to Stored Value
When you assign a value to a variable, Python first evaluates the value on the right side of the equals sign and determines its type. It then creates a space in memory to hold that value and binds the variable name to that location. The variable is a label, not the value itself. The value's type tells Python how to interpret and manipulate what is stored there.
Tracing a Numeric Assignment
What does Python need to keep track of when a variable is assigned the value 5?
- 1Evaluate
Python evaluates the value 5 on the right side of the assignment.
- 2Classify
Python determines that 5 is an integer, which is a number type.
- 3Store
Python stores the value in memory.
- 4Bind
The variable name is bound to the memory location containing that value. The name is a label for the value; it is not the value itself.
Python can later use the value according to the operations supported by its type.
Numbers and Strings
Numbers include integers and floating-point numbers. They support mathematical operations. Strings are sequences of characters and support operations such as joining text and searching for substrings. Although both are values that can be assigned to variables, Python handles them according to different type rules.
| Value category | What it represents | Operations described in the source |
|---|---|---|
| Numbers | Quantities | Mathematical operations such as addition and multiplication |
| Strings | Sequences of characters | Concatenation and searching for substrings |
Numbers and strings are basic types, but their valid operations differ.
Same Symbol, Different Result
Compare adding 10 and 5 as integers with joining "10" and "5" as strings.
- 1Numbers
The operands 10 and 5 are both integers. The plus operator performs numeric addition.
- 2String values
The operands "10" and "5" are both strings. The plus operator joins their character sequences.
- 3Compare
The numeric expression produces 15, while the string expression produces "105".
The operator symbol alone does not determine the behavior. The types of the operands do.
What do you think happens?
If the operands are the strings "10" and "5", what kind of result does the plus operator produce?
Reveal answer
The string "105"
For two strings, plus performs concatenation rather than numeric addition.
Predicting Operation Outcomes
To predict an operation, inspect the types of all its operands before focusing on the operator symbol. If the operands belong to a type combination that supports the operation, Python produces a result according to that type's rules. If the combination does not make sense for the operation, Python's type enforcement catches the mistake rather than silently combining unrelated meanings.
Checking and Converting Values
Python provides the built-in function type(), which tells you the data type of a value. Passing a value or variable to type() lets you verify an assumption and investigate unexpected behavior. This is type checking: finding out which category a value belongs to before deciding how it should be used.
When a value is not in the type needed for an operation, Python provides conversion functions. int() converts to an integer, float() converts to a floating-point number, and str() converts to a string. Conversion creates a new value of the target type. It does not modify the original value.
Choosing a Type Before an Operation
A program has a value that is text but needs a numeric value for a mathematical operation. What type-related steps should the programmer consider?
- 1Check
Use
type()on the value to verify its current data type. - 2Compare
Decide whether the current type supports the intended mathematical operation.
- 3Convert
If a numeric value is needed, use
int()orfloat()to create a value of the required numeric type. - 4Continue
Use the converted value in the operation. The original value remains unchanged.
Type checking reveals what the value is; conversion creates a new value with the type required by the operation.
Immutable Values and Rebinding
Numbers and strings are immutable types, so they cannot be changed in place. If a program appears to change a number or string, Python instead creates a new value and rebinds the variable to it. The original value remains unchanged. This reinforces the distinction between a variable name and the value to which that name is bound.
Mistakes with Type Rules
Common mistake Treating the number 5 and the string "5" as interchangeable.
What it looks likeAssuming that a text value containing a digit automatically behaves like a number.
Why it failsThe number is a quantity, while the string is a sequence of characters. Their supported operations differ.
Do this insteadCheck the value's type and convert it when a different type is required.
Common mistake Assuming the plus operator always performs arithmetic.
What it looks likeExpecting the strings "10" and "5" to produce the integer 15.
Why it failsFor strings, plus performs concatenation, producing "105".
Do this insteadInspect the operand types before predicting the result.
Common mistake Thinking a variable is the value stored in memory.
What it looks likeTreating a variable name as if it permanently contains one particular type of value.
Why it failsA variable is a label bound to a value and can be rebound to another value.
Do this insteadTrace the variable's current binding and the type of the value at that location.
Common mistake Expecting conversion to modify the original value.
What it looks likeAssuming that converting a string to an integer changes the original string in place.
Why it failsConversion creates a new value of the target type, and numbers and strings are immutable.
Do this insteadUse the converted value and remember that the original remains unchanged.
Practice the Trace
For each pair, identify the type of each operand and predict what kind of behavior the plus operator follows: the integers 10 and 5; the strings "10" and "5". Then explain why the results differ.
Hints
- Separate the numeric values from the character sequences.
- Ask whether plus is performing arithmetic or joining text.
- State both the resulting value and its type.
A variable currently refers to an immutable number. Describe what must happen if the program needs the variable to refer to a different number. Include the roles of the original value, the new value, and the variable name.
Hints
- Numbers cannot be changed in place.
- The variable is a label rather than the value itself.
- Use the idea of rebinding.
Key Takeaways
- 1A data type classifies a value and determines which operations are valid for it.
- 2Numbers and strings behave differently even when the same operator is used.
- 3Python evaluates a value, determines its type, stores it in memory, and binds a variable name to that location.
- 4The type of an operand helps you predict whether an operation produces a result and what that result means.
- 5
type()checks a value's type, whileint(),float(), andstr()create new values of specified types.
At a glance
- A data type is a classification that specifies what kind of value exists and which operations are valid.
- Numbers support mathematical behavior, while strings support text behavior such as concatenation and searching.
- A variable is a label bound to a stored value; reassignment changes the binding rather than changing an immutable value in place.
- Type checking and conversion make it possible to verify a value and prepare a new value for the operation a program needs.