Working with Python Data Types
There is no separate char data type in Python. There is no real need for it and I am sure you won't miss it.
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.
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
One name, different kinds of values
Trace what happens when the variable named item is assigned a number and then assigned a string.
First assignment: The name item is assigned a number. At this point, item holds a numeric value.
Second assignment: The name item is assigned a string. The value associated with item is now a string rather than the earlier number.
What this shows: Python does not require a separate declaration or data type definition before these assignments. Variables are used by assigning them values.
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
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 kind | Python representation | Data type idea |
|---|---|---|
| Single character | A string containing one character | String |
| Text with several characters | A string | String |
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.
Tracing a mutable list
Follow the state of a list as its contents change.
Create: A list is created with a set of items enclosed in square brackets.
Add: An item can be added to the list, changing the collection's contents.
Remove: An item can be removed, changing the contents again.
Classify: Because the list can be altered after creation, it is described as a mutable data type.
A list is mutable because its items can be added or removed after the list is created.
Mutable means that the data type can be altered. For lists, adding and removing items are examples of altering the list.
Mistakes to Avoid
Assuming Python has a separate char data type.
Python has no separate char data type.
Fix:
Treat a single character as a string.Trying to declare a variable's type before assigning a value.
Python variables are used by assigning them a value, without a declaration or data type definition.
Fix:
Create the variable through assignment.Leaving the square brackets out when specifying a list.
List items should be enclosed in square brackets so Python understands that a list is being specified.
Fix:
Enclose the list items in square brackets.Assuming a list cannot change after creation.
Lists are mutable data types.
Fix:
Remember that a list can be altered by adding or removing items.
Check Your Understanding
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.
- 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.