Python has several built-in data types. Choosing the right type determines which operations make sense for a value.
Understanding the Concept
Common types include int for whole numbers, float for approximate decimal numbers, bool for True/False values, str for Unicode text, and NoneType for the singleton None. Collections such as list, tuple, dict, and set hold groups of values; they will be taught later. Python can determine the type of an object with type(), but using that function is not required for every operation. A type affects behavior: numbers can be added arithmetically, while strings can be joined as text.
When and Why You Would Use This
Build a small product record with a name, count, price, and availability. Inspect the types of those values and compare them to what each value represents in real life.
Practical Example
product_name = "Notebook"
product_quantity = 3
unit_price = 4.5
is_available = True
print(type(product_name).__name__)
print(type(product_quantity).__name__)
print(type(unit_price).__name__)
print(type(is_available).__name__)
Understanding the Example
The output is str, int, float, and bool. The values do not need explicit type declarations. The types correspond to text, whole-item counts, approximate numeric price, and a truth value.
Example Output
str
int
float
bool
Try It Yourself
Create four variables representing a distance, an item count, a name, and a yes/no flag. Predict their types before using type().
Common Beginner Mistakes
- Choosing str for a number that must participate in arithmetic.
- Assuming float stores every decimal fraction exactly.
- Confusing None with the text "None" or the number zero.
Key Takeaways
- Values have built-in types that determine supported operations.
- Common beginner types are int, float, bool, str, and NoneType.
- Inspect values with type() when learning or debugging.