Concepts / Data Types in Python

Data Types in Python

A data type is a classification that specifies what kind of value a variable holds and what operations are valid on that value.

  • Programming

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.

binds tostoresassociated withsupportsscorevariable nameA memory location5stored valueArithmetic operationsvalid operations depend ontypeIntegervalue classification
What does a variable name point to, and how is the value's type associated with it?

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.

FeatureNumbersStrings
What they representQuantitiesText or sequences of characters
Basic forms named hereIntegers and floating-point numbersCharacter sequences
Typical supported operationsMathematical operations such as addition and multiplicationText operations such as concatenation and substring searching
Meaning of 5 versus "5"5 is a number"5" is text containing a character

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

operandoperandtext operandtext operand5numberAdditionmathematical operation"5"stringConcatenationtext operation2number"2"string
What changes when an operation is applied to numbers rather than strings?

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.

Classify the first value: 5 is a number. It represents a quantity, so number-oriented operations such as arithmetic are appropriate.

Classify the second value: "5" is a string. It represents text, so text-oriented operations such as concatenation or searching for text are appropriate.

Compare 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?

  • They have the same type because they contain the same visible character
  • The first is a number and the second is a string
  • Both are strings because they are written in a program
Reveal answer

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.

bound torebound toscorevariable name5original valuescorevariable name10new value
What changes when a variable holding an immutable value is reassigned?

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

  • Treating 5 and "5" as the same value

    5 is a number, while "5" is a string. Their types determine different valid operations.

    Fix: Classify the value before predicting its behavior. Check whether it represents a quantity or text.

  • Assuming every operation has the same meaning for every type

    Numbers support mathematical operations, while strings support text-based operations such as concatenation and substring searching.

    Fix: Inspect the operand types and ask which operation is meaningful for that category.

  • Thinking the variable is the value

    A variable is a label bound to a memory location. Numbers and strings are immutable, so reassignment creates a new value and rebinds the name.

    Fix: Describe reassignment as moving the variable name's binding from the original value to a new value.

Type Prediction Practice

EASY

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.
  1. First classify every operand as a number or a string.
  2. Next identify what the values represent: a quantity or text.
  3. Then choose the operation category that fits the type.
  4. If 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

  1. A data type classifies a value and tells Python which operations are valid for it.
  2. Numbers include integers and floating-point values; strings represent sequences of characters.
  3. Numbers support mathematical operations, while strings support text-based operations such as concatenation and substring searching.
  4. A variable is a label bound to a memory location containing a value and its type classification.
  5. Numbers and strings are immutable, so reassignment creates a new value and rebinds the variable name.

Key Takeaways

  • 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.