Data Types in Python
The characters used to write a value are not the whole story. The value's type determines how Python interprets and processes it.
A Number or Text?
Consider the values 5 and "5". They look closely related, but Python does not treat them as the same kind of value. The first is a number; the second is a string containing a character. That distinction affects which operations make sense. A number can participate in arithmetic, while a string supports text-based operations such as combining text or searching for text within a larger string. Data types give Python the information it needs to handle these values predictably.
A data type is a classification that specifies what kind of value a variable holds and what operations are valid on that value.
The characters used to write a value are not the whole story. The value's type determines how Python interprets and processes it.
Names, Values, and Types
When you assign a value to a variable, Python stores the value in memory and binds the variable name to that location. The variable is a label, not the value itself. The stored value belongs to a type, and Python remembers that classification so it can determine which operations are appropriate later.
The diagram shows a useful mental model: score is only a label connected to a memory location. The location contains a value, and that value has a classification such as integer. Python uses that classification when deciding how an operation should treat the value. The diagram does not mean that a variable is itself a container holding a value; the source distinction is important: the variable is a label bound to the location.
Numbers and Strings
Python's two most fundamental beginner-level categories are numbers and strings. Numbers represent quantities. The basic numeric types named in this material are integers and floating-point numbers. Numbers support mathematical operations such as addition and multiplication. Strings represent text: sequences of characters. Strings support text-based operations such as concatenation and searching for substrings.
| Feature | Numbers | Strings |
|---|---|---|
| What they represent | Quantities | Text or sequences of characters |
| Basic forms named here | Integers and floating-point numbers | Character sequences |
| Typical supported operations | Mathematical operations such as addition and multiplication | Text operations such as concatenation and substring searching |
| Meaning of 5 versus "5" | 5 is a number | "5" is text containing a character |
Numbers and strings are different categories with different operation rules.
Suppose a program stores a quantity and a label. The quantity 5 can be treated as part of a mathematical calculation. The label "5" can be treated as text, for example as part of a larger textual value or as text to search for. Although both contain the visible character 5 when written in these examples, their types give them different meanings and available operations.
Operators Follow the Types
An operator does not have one universal meaning independent of its operands. Python considers the types of the values involved and applies an operation that makes sense for those types. With numeric operands, an addition operation is mathematical. With string operands, the corresponding text operation is concatenation: joining text together. The key prediction skill is therefore to inspect the operands' types before predicting the behavior of an operation.
Classifying Two Similar-Looking Values
Predict how Python should treat 5 and "5" when each is used in an expression.
- 1Classify the first value
5 is a number. It represents a quantity, so number-oriented operations such as arithmetic are appropriate.
- 2Classify the second value
"5" is a string. It represents text, so text-oriented operations such as concatenation or searching for text are appropriate.
- 3Compare their behavior
The visible character is similar, but the classifications differ. Python therefore does not treat the two values as interchangeable.
Before evaluating an expression, identify whether each operand is a number or a string. That classification gives the strongest clue about which operation Python will apply.
What do you think happens?
You see two operands written as 5 and "5". Before thinking about the operator, what should you predict first?
Reveal answer
The first is a number and the second is a string.
The quotation marks identify the second value as text in this example. Their different types lead to different valid operations and different behavior in expressions.
Reassignment and Immutability
Numbers and strings are immutable types. They cannot be changed in place. If a variable is reassigned, Python creates a new value and rebinds the variable to that new value; the original value remains unchanged. This is another reason to separate the variable name from the value itself. The name can be redirected even though the original number or string cannot be edited in place.
In the before-and-after view, the value 5 has not been edited into 10. Instead, the name score is first bound to 5 and later rebound to a new value, 10. The original value is left unchanged. Thinking in terms of rebinding prevents the common mistake of imagining that an immutable number or string is modified inside its memory location.
Mistakes with Value Categories
Common mistake Treating 5 and "5" as the same value
What it looks likeA learner sees the same visible character and assumes both values can be used in the same expressions.
Why it fails5 is a number, while "5" is a string. Their types determine different valid operations.
Do this insteadClassify the value before predicting its behavior. Check whether it represents a quantity or text.
Common mistake Assuming every operation has the same meaning for every type
What it looks likeA learner expects an operator applied to numeric operands to behave identically when applied to string operands.
Why it failsNumbers support mathematical operations, while strings support text-based operations such as concatenation and substring searching.
Do this insteadInspect the operand types and ask which operation is meaningful for that category.
Common mistake Thinking the variable is the value
What it looks likeA learner imagines that changing the variable directly edits the original number or string.
Why it failsA variable is a label bound to a memory location. Numbers and strings are immutable, so reassignment creates a new value and rebinds the name.
Do this insteadDescribe reassignment as moving the variable name's binding from the original value to a new value.
Type Prediction Practice
For each pair, identify whether the values are numbers or strings. Then state whether you would expect a number-oriented operation or a text-oriented operation to be appropriate: 8 and 3; "8" and "3"; 2.5 and 1.5; "cat" and "catalog".
Hints
- Integers and floating-point values are numbers.
- Quotation marks in these examples indicate strings.
- Use the operation categories from the lesson: mathematical operations for numbers and text operations such as concatenation or substring searching for strings.
- 1First classify every operand as a number or a string.
- 2Next identify what the values represent: a quantity or text.
- 3Then choose the operation category that fits the type.
- 4If a variable is reassigned, decide whether the name was rebound to a new value rather than treating the original immutable value as changed.
Key Takeaways
- 1A data type classifies a value and tells Python which operations are valid for it.
- 2Numbers include integers and floating-point values; strings represent sequences of characters.
- 3Numbers support mathematical operations, while strings support text-based operations such as concatenation and substring searching.
- 4A variable is a label bound to a memory location containing a value and its type classification.
- 5Numbers and strings are immutable, so reassignment creates a new value and rebinds the variable name.
At a glance
- A data type identifies what kind of value is stored and what operations make sense for that value.
- Numbers and strings may look similar in some examples, but they represent different categories and behave differently.
- Python binds variable names to memory locations associated with typed values; the variable name is not the value itself.
- Numbers and strings are immutable, so reassignment binds a name to a new value rather than changing the original in place.
- To predict an expression, classify its operands before deciding what the operation will mean.