Skip to Content
PythonPython Data Types1. What is a Data Type?

Introduction to Python Data Types

Welcome to data types in Python! Everything you work with in Python, from a simple number to a massive collection of information, has a specific type.

A data type is like a category. It tells Python what kind of value a variable is holding, how much memory to reserve, and what operations are allowed on that value.

Think of it like this: in the real world, you know you can perform mathematics with numbers (like 2 + 2), but it does not make sense to perform mathematics on a word (like dividing “hello” by 3). Python needs to follow these same logical rules, and data types are how it enforces them!


Why are Data Types Important?

Understanding data types is essential for several reasons:

  • Enforces Operations: The data type determines what you can do with it. For example, you can add two numbers together, but trying to add a number to text will make Python stop and raise a warning.
  • Controls Memory: Python uses data types to figure out how much computer memory to allocate to store your variables.
  • Prevents Bugs: Knowing your data types helps you write robust programs and avoid the dreaded TypeError.

Python’s Core Data Types

Python comes with a rich set of built-in data types. Let us take a friendly, high-level overview. We will explore each in detail in the pages ahead!

Text: The str Type

  • Purpose: Used for storing characters, words, sentences, or paragraphs.
  • Examples: "Hello, Python!" or 'Coding is fun'

Checking the Type of any Variable

If you ever feel confused about what data type a variable is holding, Python provides a wonderful built-in tool called type().

When you pass a variable or value into type(), Python will tell you its exact classification. Let us see this in action! Run the interactive example below to check the types of six different variables:


Example

Output:

The data type of 'a' is: <class 'int'>
The data type of 'b' is: <class 'str'>
The data type of 'c' is: <class 'float'>
The data type of 'd' is: <class 'list'>
The data type of 'e' is: <class 'dict'>
The data type of 'f' is: <class 'bool'>

In Python, you never need to declare what type a variable is before you create it. Python figures it out automatically the exact moment you assign it a value! This friendly feature is called Dynamic Typing, and we will learn all about it in the sections to come.

Last updated on