Data types are basically the categories that Python uses to classify and organize data, take a library for example. Just as a library categorizes books into fiction, non-fiction, biographies, and more, Python categorizes data into different types such as integers, floats, strings, and lists.
Unlike some other programming languages, in Python the data type is not declared in the variable definition. Instead, the data type of a variable is inferred automatically from the value it holds.
When this sample above is executed it will return the following result:
Common Data Types in Python
There are many built-in data types in Python. Each is designed to represent a specific category of data, such as numbers (integers, floats, and complex numbers), text (strings), and collections of values (lists, tuples, sets, and dictionaries). Let's take a look at some of these data types.
| Data Type | Description | Example |
|---|---|---|
| int | Whole numbers | 10 |
| float | Decimal numbers | 3.14 |
| str | Text values | "Hello" |
| bool | True or False values | True |
| list | Ordered collection of items | [1, 2, 3] |
| tuple | Immutable ordered collection | (1, 2, 3) |
| set | Unordered collection of unique items | {1, 2, 3} |
| dict | Key-value pairs | {"website_name": "PyRepo"} |
Numeric Data Types
Numeric data types are used to represent numbers in Python. These include whole numbers, decimal numbers, and complex numbers. The built-in numeric types are:
intfloatcomplex
Will return:
These data types allow Python to work with whole numbers, decimal numbers, and complex numbers.
Text Data Type
The built-in text data type in Python is:
str
Will return:
The str data type is used to represent textual data such as names, messages, sentences, and other sequences of characters.
Boolean Data Type
The built-in Boolean data type in Python is:
bool
Will return:
The bool data type is used to represent logical values and can hold either True or False.
Sequence Data Types
Sequence data types are used to store ordered collections of values. The built-in sequence types are:
listtuplerange
Will return:
These data types allow multiple values to be stored and accessed in a specific order.
Set Data Type
The built-in set data type in Python is:
set
Will return:
The set data type is used to store unique values, meaning duplicate items are automatically removed.
Mapping Data Type
The built-in mapping data type in Python is:
dict
Will return:
The dict data type stores data as key-value pairs, allowing values to be accessed through their associated keys.
We have looked at the built-in data types available in Python, including numeric, text, Boolean, sequence, set, and mapping data types. Each of these data types serves a specific purpose and will be explored in greater detail in its own dedicated tutorial. Continue with the next tutorials to learn how to create, manipulate, and work with each data type effectively.
