Concepts / Working with Python Data Types

Working with Python Data Types

Python does not require a separate declaration or data type definition before a variable is used. You create a variable by assigning it a value.

  • Programming

Why Types Matter

A data type describes the kind of value a variable holds. Python variables can hold values of different types, including numbers and strings. Python also provides lists, which can hold multiple items and can be changed after they are created.

Key point

Python does not require a separate declaration or data type definition before a variable is used. You create a variable by assigning it a value.

Following a Variable's Type

Worked example

One name, different kinds of values

Trace what happens when the variable named item is assigned a number and then assigned a string.

  1. 1
    First assignment

    The name item is assigned a number. At this point, item holds a numeric value.

  2. 2
    Second assignment

    The name item is assigned a string. The value associated with item is now a string rather than the earlier number.

  3. 3
    What this shows

    Python does not require a separate declaration or data type definition before these assignments. Variables are used by assigning them values.

Result

A Python variable can be used with values of different types through assignment.

The important idea is to focus on the value currently assigned to a variable. Numbers and strings are both basic data types, but they represent different kinds of values. Python's assignment-based approach means that a separate type declaration is not part of creating the variable.

Strings Include Single Characters

Definition

Python has no separate char data type. A single character is represented using the string type, so there is no need to learn or use a distinct character type in Python.

Value kindPython representationData type idea
Single characterA string containing one characterString
Text with several charactersA stringString

Python uses the string type for both one-character text and longer text.

Lists That Can Change

A list represents a collection of items. Its items are enclosed in square brackets so that Python understands that a list is being specified. After creating a list, you can add items, remove items, or search for items.

Worked example

Tracing a mutable list

Follow the state of a list as its contents change.

  1. 1
    Create

    A list is created with a set of items enclosed in square brackets.

  2. 2
    Add

    An item can be added to the list, changing the collection's contents.

  3. 3
    Remove

    An item can be removed, changing the contents again.

  4. 4
    Classify

    Because the list can be altered after creation, it is described as a mutable data type.

Result

A list is mutable because its items can be added or removed after the list is created.

Key point

Mutable means that the data type can be altered. For lists, adding and removing items are examples of altering the list.

Mistakes to Avoid

  • Common mistake

    Assuming Python has a separate char data type.

    What it looks like

    Treating a single character as a category different from a string.

    Why it fails

    Python has no separate char data type.

    Do this instead

    Treat a single character as a string.

  • Common mistake

    Trying to declare a variable's type before assigning a value.

    What it looks like

    Expecting a separate declaration or data type definition to be required first.

    Why it fails

    Python variables are used by assigning them a value, without a declaration or data type definition.

    Do this instead

    Create the variable through assignment.

  • Common mistake

    Leaving the square brackets out when specifying a list.

    What it looks like

    Writing a collection of items without the notation that identifies it as a list.

    Why it fails

    List items should be enclosed in square brackets so Python understands that a list is being specified.

    Do this instead

    Enclose the list items in square brackets.

  • Common mistake

    Assuming a list cannot change after creation.

    What it looks like

    Treating a list as fixed even though items may be added or removed.

    Why it fails

    Lists are mutable data types.

    Do this instead

    Remember that a list can be altered by adding or removing items.

Check Your Understanding

EASY

For each statement, identify the relevant Python data-type idea: a variable is created through assignment, a single character is a string, or a list is mutable. Then explain what can change in the list case.

Hints
  • Python does not require a separate declaration or data type definition for a variable.
  • Python has no separate char data type.
  • A list can have items added or removed after it is created.
  1. 1
    Python variables hold values of different data types, including numbers and strings. Variables are created through assignment rather than a separate declaration. Python does not have a separate char data type; a single character is represented as a string. Lists use square brackets, and lists are mutable because their items can be added or removed.

Key Takeaways

  • Numbers and strings are basic Python data types.
  • Python variables are created by assigning values, without a separate declaration or data type definition.
  • Python has no separate char data type; a single character is a string.
  • Lists are written with square brackets and are mutable because their items can be added or removed.

Related concepts

More concepts starting with W